ljf0030 / gwt-pectin

Automatically exported from code.google.com/p/gwt-pectin
0 stars 0 forks source link

EnumToIntegerConverter #28

Open GoogleCodeExporter opened 8 years ago

GoogleCodeExporter commented 8 years ago
Don't know whether it is generally useful but if you think so, feel free to 
include:

import com.pietschy.gwt.pectin.client.value.Converter;

/**
 * Converts the value of an {@link Enum} type to its {@link Enum#ordinal()} integer.
 * 
 * @param <E>
 *          the enum type
 */
public class EnumToIntegerConverter<E extends Enum<E>> implements Converter<E, 
Integer>
{
  private final Class<E> clazz;

  public EnumToIntegerConverter(Class<E> clazz)
  {
    this.clazz = clazz;
  }

  @Override
  public E fromSource(Integer value)
  {
    return clazz.getEnumConstants()[value];
  }

  @Override
  public Integer toSource(E value)
  {
    return value.ordinal();
  }
}

Original issue reported on code.google.com by kaspar.f...@gtempaccount.com on 15 Jul 2010 at 5:37