Package com.github.ngoanh2n
Class Commons
java.lang.Object
com.github.ngoanh2n.Commons
- Since:
- 2019
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T> Optional<T>
Build enum from enum class and enum constant name.static <T> T
convertValue
(Class<T> type, String value) Convert String value to a specific object.static File
Create recursively directory fromFile
.static Path
Create recursively directory fromPath
.static String
detectCharset
(File file) Get the charset of a file.static File
getRelative
(File file) Get relative path of file against to current user directory.static Path
getRelative
(Path path) Get relative path of path against to current user directory.static <T> T
Read value of theField
.static <T> T
Read value of theField
.static Properties
ReadProperties
from given properties file.static Properties
ReadProperties
from given Java resource name.static String
Create a timestamp.static void
writeField
(Class<?> target, String name, Object value) Write value to the field with modifiers: Target object has fields:static void
writeField
(Object target, String name, Object value) Write value to the field with modifiers: Target object has fields:static File
writeProps
(Properties props, File file) WriteProperties
to file.
-
Method Details
-
timestamp
Create a timestamp.- Returns:
- timestamp as string.
-
createDir
Create recursively directory fromFile
.- Parameters:
file
- is directory as File.- Returns:
- directory as a file.
-
createDir
Create recursively directory fromPath
.- Parameters:
path
- is directory as Path.- Returns:
- directory as a path.
-
getRelative
Get relative path of file against to current user directory.- Parameters:
file
- to get relative path.- Returns:
- relative path.
-
getRelative
Get relative path of path against to current user directory.- Parameters:
path
- to get relative path.- Returns:
- relative path.
-
writeProps
WriteProperties
to file.- Parameters:
file
- to be stored.props
- to be written.- Returns:
- output file.
-
readProps
ReadProperties
from given Java resource name.- Parameters:
resourceName
- Java resource name to read.- Returns:
Properties
object.
-
readProps
ReadProperties
from given properties file.- Parameters:
file
- to read.charset
- The name of a supported charset.- Returns:
Properties
object.
-
detectCharset
Get the charset of a file.
Method to markUniversalDetector
for reusing.- Parameters:
file
- The file to check charset for.- Returns:
- The charset of the file, null when could not be determined.
- Throws:
IOException
- if some IO error occurs.
-
readField
Read value of theField
. Its parents will be considered.
private Type aField
private final Type aField
- Type Parameters:
T
- Type of result will be returned.- Parameters:
target
- The object instance to reflect, must not benull
.name
- The field name to obtain.- Returns:
- The field value.
-
readField
- Type Parameters:
T
- Type of result will be returned.- Parameters:
target
- The object class to reflect, must not benull
.name
- The field name to obtain.- Returns:
- The field value.
-
writeField
Write value to the field with modifiers:- Target object has fields:
private Type aField
private final Type aField
- Target object's parents have fields:
private Type aField
private final Type aField
private static Type aField
private static final Type aField
- Parameters:
target
- The object instance to reflect, must not benull
.name
- The field name to obtain.value
- The new value for the field of object being modified.
- Target object has fields:
-
writeField
Write value to the field with modifiers:- Target object has fields:
private static Type aField
private static final Type aField
- Target object's parents have fields:
private static Type aField
private static final Type aField
- Parameters:
target
- The object class to reflect, must not benull
.name
- The field name to obtain.value
- The new value for the field of object being modified.
- Target object has fields:
-
convertValue
Convert String value to a specific object.- Type Parameters:
T
- The type of target object.- Parameters:
type
- The Class object which to return an object.value
- String value to convert.- Returns:
- The target object.
-
buildEnum
Build enum from enum class and enum constant name.- Type Parameters:
T
- The type of enum object.- Parameters:
type
- The Class object of the enum type from which to return a constant.name
- The name of enum constant to return, exactly as declared in its enum declaration.- Returns:
- Optional of the enum constant of the specified enum type with the specified name.
-