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        super();
039    }
040
041    /**
042     * Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context.
043     *
044     * <p>Note that this method does not check whether the resource actually exists.
045     * It only constructs the name.
046     * Null inputs are not allowed.</p>
047     *
048     * <pre>
049     * ClassPathUtils.toFullyQualifiedName(StringUtils.class, "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"
050     * </pre>
051     *
052     * @param context The context for constructing the name.
053     * @param resourceName the resource name to construct the fully qualified name for.
054     * @return the fully qualified name of the resource with name {@code resourceName}.
055     * @throws java.lang.NullPointerException if either {@code context} or {@code resourceName} is null.
056     */
057    public static String toFullyQualifiedName(final Class<?> context, final String resourceName) {
058        Validate.notNull(context, "Parameter '%s' must not be null!", "context" );
059        Validate.notNull(resourceName, "Parameter '%s' must not be null!", "resourceName");
060        return toFullyQualifiedName(context.getPackage(), resourceName);
061    }
062
063    /**
064     * Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context.
065     *
066     * <p>Note that this method does not check whether the resource actually exists.
067     * It only constructs the name.
068     * Null inputs are not allowed.</p>
069     *
070     * <pre>
071     * ClassPathUtils.toFullyQualifiedName(StringUtils.class.getPackage(), "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"
072     * </pre>
073     *
074     * @param context The context for constructing the name.
075     * @param resourceName the resource name to construct the fully qualified name for.
076     * @return the fully qualified name of the resource with name {@code resourceName}.
077     * @throws java.lang.NullPointerException if either {@code context} or {@code resourceName} is null.
078     */
079    public static String toFullyQualifiedName(final Package context, final String resourceName) {
080        Validate.notNull(context, "Parameter '%s' must not be null!", "context" );
081        Validate.notNull(resourceName, "Parameter '%s' must not be null!", "resourceName");
082        return context.getName() + "." + resourceName;
083    }
084
085    /**
086     * Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context.
087     *
088     * <p>Note that this method does not check whether the resource actually exists.
089     * It only constructs the path.
090     * Null inputs are not allowed.</p>
091     *
092     * <pre>
093     * ClassPathUtils.toFullyQualifiedPath(StringUtils.class, "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"
094     * </pre>
095     *
096     * @param context The context for constructing the path.
097     * @param resourceName the resource name to construct the fully qualified path for.
098     * @return the fully qualified path of the resource with name {@code resourceName}.
099     * @throws java.lang.NullPointerException if either {@code context} or {@code resourceName} is null.
100     */
101    public static String toFullyQualifiedPath(final Class<?> context, final String resourceName) {
102        Validate.notNull(context, "Parameter '%s' must not be null!", "context" );
103        Validate.notNull(resourceName, "Parameter '%s' must not be null!", "resourceName");
104        return toFullyQualifiedPath(context.getPackage(), resourceName);
105    }
106
107
108    /**
109     * Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context.
110     *
111     * <p>Note that this method does not check whether the resource actually exists.
112     * It only constructs the path.
113     * Null inputs are not allowed.</p>
114     *
115     * <pre>
116     * ClassPathUtils.toFullyQualifiedPath(StringUtils.class.getPackage(), "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"
117     * </pre>
118     *
119     * @param context The context for constructing the path.
120     * @param resourceName the resource name to construct the fully qualified path for.
121     * @return the fully qualified path of the resource with name {@code resourceName}.
122     * @throws java.lang.NullPointerException if either {@code context} or {@code resourceName} is null.
123     */
124    public static String toFullyQualifiedPath(final Package context, final String resourceName) {
125        Validate.notNull(context, "Parameter '%s' must not be null!", "context" );
126        Validate.notNull(resourceName, "Parameter '%s' must not be null!", "resourceName");
127        return context.getName().replace('.', '/') + "/" + resourceName;
128    }
129
130}