com.aegisql.java_path.StringConverter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of java-path Show documentation
Show all versions of java-path Show documentation
Deep access to Java object hierarchy
package com.aegisql.java_path;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.function.Function;
/**
* The interface String converter.
* Converts String to any object that can be derived from it
* Example: "10" -- int 10
*
* @param the type parameter
*/
@FunctionalInterface
public interface StringConverter extends Function {
/**
* Identity string converter returns passed parameter without modification
*
* @return the string converter
*/
static StringConverter identity() {
return str->str;
}
/**
* Creates new object using a static factory method with a single String parameter
*
* @param the type parameter
* @param aClass the a class
* @param methodName the method name
* @return the string converter
*/
static StringConverter factory(Class aClass, String methodName) {
final Method valueOf;
try {
valueOf = aClass.getMethod(methodName, String.class);
} catch (NoSuchMethodException e) {
return null;
}
return str->{
try {
valueOf.setAccessible(true);
return (X)valueOf.invoke(null,str);
} catch (Exception e) {
throw new JavaPathRuntimeException(e);
}
};
}
/**
* Creates new object using static valueOf(String s) method
* Many Java classes implement it, such as Integer, all enum's
*
* @param the type parameter
* @param aClass the a class
* @return the string converter
*/
static StringConverter valueOf(Class aClass) {
return factory(aClass,"valueOf");
}
/**
* Creates new object using constructor with a single String parameter.
*
* @param the type parameter
* @param aClass the a class
* @return the string converter
*/
static StringConverter constructor(Class aClass) {
final Constructor> constructor;
try {
constructor = aClass.getConstructor(String.class);
} catch (NoSuchMethodException e) {
return null;
}
return str->{
try {
constructor.setAccessible(true);
return (X)constructor.newInstance(str);
} catch (Exception e) {
throw new JavaPathRuntimeException(e);
}
};
}
}