Class MapConverter<K,T>

java.lang.Object
org.fastnate.data.properties.MapConverter<K,T>
Type Parameters:
K - the type of the key
T - the type of the value
All Implemented Interfaces:
Function<String,T>, PropertyConverter<T>

public class MapConverter<K,T> extends Object implements PropertyConverter<T>, Function<String,T>
Converts a string from an import file to an object by using an explicit mapping.
Author:
Tobias Liefke
  • Constructor Details

    • MapConverter

      public MapConverter(Map<K,T> map, Function<String,K> keyConverter)
      Creates a new MapConverter instance.
      Parameters:
      map - The mapping from the keys in the import file to the target value.
      keyConverter - Converts the string in the import file to the key in the mapping.
  • Method Details

    • create

      public static <N extends Number, V> MapConverter<N,V> create(Map<N,V> map, Class<N> numberClass)
      Creates a new instance of a MapConverter that uses numbers to lookup the values.
      Parameters:
      map - mapping from the number to the values
      numberClass - the concrete class of the numbers
      Returns:
      the created converter
    • create

      public static <V> MapConverter<String,V> create(Map<String,V> map)
      Creates a new instance of a MapConverter that uses strings to lookup the values.
      Parameters:
      map - mapping from the string to the value
      Returns:
      the created converter
    • apply

      public T apply(String value)
      Specified by:
      apply in interface Function<K,T>
    • convert

      public T convert(Class<? extends T> targetType, String value)
      Description copied from interface: PropertyConverter
      Converts a value from import file to Java.
      Specified by:
      convert in interface PropertyConverter<K>
      Parameters:
      targetType - the type of the target property
      value - the value in the import file
      Returns:
      the value in Java