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

fitnesse.slim.converters.GenericEnumConverter Maven / Gradle / Ivy

There is a newer version: 20181217
Show newest version
package fitnesse.slim.converters;

import fitnesse.slim.SlimError;
import fitnesse.util.StringUtils;

import fitnesse.slim.Converter;

public class GenericEnumConverter> implements Converter {

  private final Class enumClass;

  public GenericEnumConverter(Class enumClass) {
    this.enumClass = enumClass;
  }

  @Override
  public String toString(T o) {
    return o != null ? o.name() : NULL_VALUE;
  }

  @Override
  public T fromString(String name) {
    if (StringUtils.isBlank(name))
      return null;

    try {
      return Enum.valueOf(enumClass, name);
    } catch (IllegalArgumentException e) {
      for (T value : enumClass.getEnumConstants()) {
        if (value.name().equalsIgnoreCase(name)) {
          return value;
        }
      }
      throw new SlimError(String.format("message:<>", name, enumClass.getName()), e);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy