Class Commons

java.lang.Object
com.github.ngoanh2n.Commons

@CanIgnoreReturnValue public final class Commons extends Object
Common helpers.

Repository:
Since:
2019
  • Method Details

    • timestamp

      public static String timestamp()
      Create a timestamp.
      Returns:
      timestamp as string.
    • createDir

      public static File createDir(@Nonnull File file)
      Create recursively directory from File.
      Parameters:
      file - is directory as File.
      Returns:
      directory as a file.
    • createDir

      public static Path createDir(@Nonnull Path path)
      Create recursively directory from Path.
      Parameters:
      path - is directory as Path.
      Returns:
      directory as a path.
    • getRelative

      public static File getRelative(@Nonnull File file)
      Get relative path of file against to current user directory.
      Parameters:
      file - to get relative path.
      Returns:
      relative path.
    • getRelative

      public static Path getRelative(@Nonnull Path path)
      Get relative path of path against to current user directory.
      Parameters:
      path - to get relative path.
      Returns:
      relative path.
    • writeProps

      public static File writeProps(Properties props, File file)
      Write Properties to file.
      Parameters:
      file - to be stored.
      props - to be written.
      Returns:
      output file.
    • readProps

      public static Properties readProps(@Nonnull String resourceName)
      Read Properties from given Java resource name.
      Parameters:
      resourceName - Java resource name to read.
      Returns:
      Properties object.
    • readProps

      public static Properties readProps(@Nonnull File file, String charset)
      Read Properties from given properties file.
      Parameters:
      file - to read.
      charset - The name of a supported charset.
      Returns:
      Properties object.
    • detectCharset

      public static String detectCharset(File file) throws IOException
      Get the charset of a file.
      Method to mark UniversalDetector 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

      public static <T> T readField(Object target, String name)
      Read value of the Field. 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 be null.
      name - The field name to obtain.
      Returns:
      The field value.
    • readField

      public static <T> T readField(Class<?> target, String name)
      Read value of the Field. Its parents will be considered.
      • private static final Type aField
      Type Parameters:
      T - Type of result will be returned.
      Parameters:
      target - The object class to reflect, must not be null.
      name - The field name to obtain.
      Returns:
      The field value.
    • writeField

      public static void writeField(Object target, String name, Object value)
      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 be null.
      name - The field name to obtain.
      value - The new value for the field of object being modified.
    • writeField

      public static void writeField(Class<?> target, String name, Object value)
      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 be null.
      name - The field name to obtain.
      value - The new value for the field of object being modified.
    • convertValue

      public static <T> T convertValue(Class<T> type, String value)
      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

      public static <T> Optional<T> buildEnum(Class<T> type, String name)
      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.