Add ClassPathUtils.packageToPath(String) and pathToPackage(String)

This commit is contained in:
Gary Gregory 2022-10-07 08:57:05 -04:00
parent 32756ef89b
commit 03f74ba5a1
3 changed files with 69 additions and 29 deletions

View File

@ -179,6 +179,7 @@ The <action> type attribute can be add,update,fix,remove.
<action type="add" dev="ggregory" due-to="Gary Gregory">Add Processor.toString().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add HashCodeBuilder.equals(Object).</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add BooleanUtils.values() and forEach().</action>
<action type="add" dev="ggregory" due-to="Gary Gregory">Add ClassPathUtils.packageToPath(String) and pathToPackage(String)</action>
<!-- UPDATE -->
<action type="update" dev="ggregory" due-to="Dependabot, XenoAmess, Gary Gregory">Bump actions/cache from 2.1.4 to 3.0.9 #742, #752, #764, #833, #867, #959.</action>
<action type="update" dev="ggregory" due-to="Dependabot, Gary Gregory">Bump actions/checkout from 2 to 3.0.2 #819, #825, #859.</action>

View File

@ -16,10 +16,14 @@
*/
package org.apache.commons.lang3;
import java.util.Objects;
/**
* Operations regarding the classpath.
*
* <p>The methods of this class do not allow {@code null} inputs.</p>
* <p>
* The methods of this class do not allow {@code null} inputs.
* </p>
*
* @since 3.3
*/
@ -27,34 +31,45 @@
public class ClassPathUtils {
/**
* {@link ClassPathUtils} instances should NOT be constructed in
* standard programming. Instead, the class should be used as
* {@code ClassPathUtils.toFullyQualifiedName(MyClass.class, "MyClass.properties");}.
* Converts a package name to a Java path ('/').
*
* <p>This constructor is public to permit tools that require a JavaBean
* instance to operate.</p>
* @param path the source path.
* @return a package name.
* @since 3.13.0
*/
public ClassPathUtils() {
public static String packageToPath(final String path) {
return Objects.requireNonNull(path, "path").replace('.', '/');
}
/**
* Converts a Java path ('/') to a package name.
*
* @param path the source path.
* @return a package name.
* @since 3.13.0
*/
public static String pathToPackage(final String path) {
return Objects.requireNonNull(path, "path").replace('/', '.');
}
/**
* Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context.
*
* <p>Note that this method does not check whether the resource actually exists.
* It only constructs the name.
* Null inputs are not allowed.</p>
* <p>
* Note that this method does not check whether the resource actually exists. It only constructs the name. Null inputs are not allowed.
* </p>
*
* <pre>
* ClassPathUtils.toFullyQualifiedName(StringUtils.class, "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"
* </pre>
*
* @param context The context for constructing the name.
* @param context The context for constructing the name.
* @param resourceName the resource name to construct the fully qualified name for.
* @return the fully qualified name of the resource with name {@code resourceName}.
* @throws NullPointerException if either {@code context} or {@code resourceName} is null.
*/
public static String toFullyQualifiedName(final Class<?> context, final String resourceName) {
Validate.notNull(context, "context" );
Validate.notNull(context, "context");
Validate.notNull(resourceName, "resourceName");
return toFullyQualifiedName(context.getPackage(), resourceName);
}
@ -62,21 +77,21 @@ public static String toFullyQualifiedName(final Class<?> context, final String r
/**
* Returns the fully qualified name for the resource with name {@code resourceName} relative to the given context.
*
* <p>Note that this method does not check whether the resource actually exists.
* It only constructs the name.
* Null inputs are not allowed.</p>
* <p>
* Note that this method does not check whether the resource actually exists. It only constructs the name. Null inputs are not allowed.
* </p>
*
* <pre>
* ClassPathUtils.toFullyQualifiedName(StringUtils.class.getPackage(), "StringUtils.properties") = "org.apache.commons.lang3.StringUtils.properties"
* </pre>
*
* @param context The context for constructing the name.
* @param context The context for constructing the name.
* @param resourceName the resource name to construct the fully qualified name for.
* @return the fully qualified name of the resource with name {@code resourceName}.
* @throws NullPointerException if either {@code context} or {@code resourceName} is null.
*/
public static String toFullyQualifiedName(final Package context, final String resourceName) {
Validate.notNull(context, "context" );
Validate.notNull(context, "context");
Validate.notNull(resourceName, "resourceName");
return context.getName() + "." + resourceName;
}
@ -84,46 +99,56 @@ public static String toFullyQualifiedName(final Package context, final String re
/**
* Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context.
*
* <p>Note that this method does not check whether the resource actually exists.
* It only constructs the path.
* Null inputs are not allowed.</p>
* <p>
* Note that this method does not check whether the resource actually exists. It only constructs the path. Null inputs are not allowed.
* </p>
*
* <pre>
* ClassPathUtils.toFullyQualifiedPath(StringUtils.class, "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"
* </pre>
*
* @param context The context for constructing the path.
* @param context The context for constructing the path.
* @param resourceName the resource name to construct the fully qualified path for.
* @return the fully qualified path of the resource with name {@code resourceName}.
* @throws NullPointerException if either {@code context} or {@code resourceName} is null.
*/
public static String toFullyQualifiedPath(final Class<?> context, final String resourceName) {
Validate.notNull(context, "context" );
Validate.notNull(context, "context");
Validate.notNull(resourceName, "resourceName");
return toFullyQualifiedPath(context.getPackage(), resourceName);
}
/**
* Returns the fully qualified path for the resource with name {@code resourceName} relative to the given context.
*
* <p>Note that this method does not check whether the resource actually exists.
* It only constructs the path.
* Null inputs are not allowed.</p>
* <p>
* Note that this method does not check whether the resource actually exists. It only constructs the path. Null inputs are not allowed.
* </p>
*
* <pre>
* ClassPathUtils.toFullyQualifiedPath(StringUtils.class.getPackage(), "StringUtils.properties") = "org/apache/commons/lang3/StringUtils.properties"
* </pre>
*
* @param context The context for constructing the path.
* @param context The context for constructing the path.
* @param resourceName the resource name to construct the fully qualified path for.
* @return the fully qualified path of the resource with name {@code resourceName}.
* @throws NullPointerException if either {@code context} or {@code resourceName} is null.
*/
public static String toFullyQualifiedPath(final Package context, final String resourceName) {
Validate.notNull(context, "context" );
Validate.notNull(context, "context");
Validate.notNull(resourceName, "resourceName");
return context.getName().replace('.', '/') + "/" + resourceName;
return packageToPath(context.getName()) + "/" + resourceName;
}
/**
* {@link ClassPathUtils} instances should NOT be constructed in standard programming. Instead, the class should be used as
* {@code ClassPathUtils.toFullyQualifiedName(MyClass.class, "MyClass.properties");}.
*
* <p>
* This constructor is public to permit tools that require a JavaBean instance to operate.
* </p>
*/
public ClassPathUtils() {
}
}

View File

@ -41,6 +41,20 @@ public void testConstructor() {
assertFalse(Modifier.isFinal(ClassPathUtils.class.getModifiers()));
}
@Test
public void testPackageToPath() {
assertEquals("a", ClassPathUtils.packageToPath("a"));
assertEquals("a/b", ClassPathUtils.packageToPath("a.b"));
assertEquals("a/b/c", ClassPathUtils.packageToPath("a.b.c"));
}
@Test
public void testPathToPackage() {
assertEquals("a", ClassPathUtils.pathToPackage("a"));
assertEquals("a.b", ClassPathUtils.pathToPackage("a/b"));
assertEquals("a.b.c", ClassPathUtils.pathToPackage("a/b/c"));
}
@Test
public void testToFullyQualifiedNameNullClassString() {
assertThrows(NullPointerException.class,