All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xdean.csv.CsvValueFormatter Maven / Gradle / Ivy

The newest version!
package xdean.csv;

import java.util.function.Function;

import xdean.jex.util.lang.PrimitiveTypeUtil;

/**
 * CSV value formatter.
 *
 * @apiNote The formatter must be stateless.
 * @author Dean Xu ([email protected])
 * @param  the value type.
 */
public interface CsvValueFormatter {
  /**
   * Format the value to text.
   */
  String format(T value);

  /**
   * The value type.
   */
  Class type();

  /**
   * Get default {@link CsvValueFormatter} by using {@link Object#toString()}
   */
  static  CsvValueFormatter toString(Class clz) {
    return create(clz, t -> t.toString());
  }

  /**
   * Create {@code CsvValueFormatter} from the {@code Function}
   */
  @SuppressWarnings("unchecked")
  static  CsvValueFormatter create(Class clz, Function function) {
    Class c = (Class) PrimitiveTypeUtil.toWrapper(clz);
    return new CsvValueFormatter() {
      @Override
      public String format(T value) {
        return function.apply(value);
      }

      @Override
      public Class type() {
        return c;
      }
    };
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy