001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.lang3;
018
019/**
020 * Operations regarding the classpath.
021 *
022 * <p>The methods of this class do not allow {@code null} inputs.</p>
023 *
024 * @since 3.3
025 */
026//@Immutable
027public class ClassPathUtils {
028
029    /**
030     * <p>{@code ClassPathUtils} instances should NOT be constructed in
031     * standard programming. Instead, the class should be used as
032     * {@code ClassPathUtils.toFullyQualifiedName(MyClass.class, "MyClass.properties");}.</p>
033     *
034     * <p>This constructor is public to permit tools that require a JavaBean
035     * instance to operate.</p>
036     */
037    public ClassPathUtils() {
038    }
039
040    /**
041     * Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context.
042     *
043     * <p>Note that this method does not check whether the resource actually exists.
044     * It only constructs the name.
045     * Null inputs are not allowed.</p>
046     *
047     * <pre>
048     * ClassPathUtils.toFullyQualifiedName(StringUtils.class, "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"
049     * </pre>
050     *
051     * @param context The context for constructing the name.
052     * @param resourceName the resource name to construct the fully qualified name for.
053     * @return the fully qualified name of the resource with name {@code resourceName}.
054     * @throws java.lang.NullPointerException if either {@code context} or {@code resourceName} is null.
055     */
056    public static String toFullyQualifiedName(final Class<?> context, final String resourceName) {
057        Validate.notNull(context, "context" );
058        Validate.notNull(resourceName, "resourceName");
059        return toFullyQualifiedName(context.getPackage(), resourceName);
060    }
061
062    /**
063     * Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context.
064     *
065     * <p>Note that this method does not check whether the resource actually exists.
066     * It only constructs the name.
067     * Null inputs are not allowed.</p>
068     *
069     * <pre>
070     * ClassPathUtils.toFullyQualifiedName(StringUtils.class.getPackage(), "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"
071     * </pre>
072     *
073     * @param context The context for constructing the name.
074     * @param resourceName the resource name to construct the fully qualified name for.
075     * @return the fully qualified name of the resource with name {@code resourceName}.
076     * @throws java.lang.NullPointerException if either {@code context} or {@code resourceName} is null.
077     */
078    public static String toFullyQualifiedName(final Package context, final String resourceName) {
079        Validate.notNull(context, "context" );
080        Validate.notNull(resourceName, "resourceName");
081        return context.getName() + "." + resourceName;
082    }
083
084    /**
085     * Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context.
086     *
087     * <p>Note that this method does not check whether the resource actually exists.
088     * It only constructs the path.
089     * Null inputs are not allowed.</p>
090     *
091     * <pre>
092     * ClassPathUtils.toFullyQualifiedPath(StringUtils.class, "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"
093     * </pre>
094     *
095     * @param context The context for constructing the path.
096     * @param resourceName the resource name to construct the fully qualified path for.
097     * @return the fully qualified path of the resource with name {@code resourceName}.
098     * @throws java.lang.NullPointerException if either {@code context} or {@code resourceName} is null.
099     */
100    public static String toFullyQualifiedPath(final Class<?> context, final String resourceName) {
101        Validate.notNull(context, "context" );
102        Validate.notNull(resourceName, "resourceName");
103        return toFullyQualifiedPath(context.getPackage(), resourceName);
104    }
105
106
107    /**
108     * Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context.
109     *
110     * <p>Note that this method does not check whether the resource actually exists.
111     * It only constructs the path.
112     * Null inputs are not allowed.</p>
113     *
114     * <pre>
115     * ClassPathUtils.toFullyQualifiedPath(StringUtils.class.getPackage(), "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"
116     * </pre>
117     *
118     * @param context The context for constructing the path.
119     * @param resourceName the resource name to construct the fully qualified path for.
120     * @return the fully qualified path of the resource with name {@code resourceName}.
121     * @throws java.lang.NullPointerException if either {@code context} or {@code resourceName} is null.
122     */
123    public static String toFullyQualifiedPath(final Package context, final String resourceName) {
124        Validate.notNull(context, "context" );
125        Validate.notNull(resourceName, "resourceName");
126        return context.getName().replace('.', '/') + "/" + resourceName;
127    }
128
129}