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

org.pharmgkb.common.util.ExtendedEnumConverter Maven / Gradle / Ivy

The newest version!
package org.pharmgkb.common.util;

import org.apache.commons.beanutils.ConversionException;
import org.apache.commons.beanutils.Converter;
import org.checkerframework.checker.nullness.qual.Nullable;


/**
 * This is a BeanUtils {@link Converter} for {@link ExtendedEnum}s.
 *
 * @author Mark Woon
 */
public class ExtendedEnumConverter implements Converter {
  private static final ExtendedEnumConverter sf_converter = new ExtendedEnumConverter();


  public static ExtendedEnumConverter getConverter() {
    return sf_converter;
  }


  @Override
  public @Nullable  T convert(Class aClass, Object o) {

    if (o == null) {
      return null;
    }
    if (ExtendedEnum.class.isAssignableFrom(aClass)) {
      //noinspection unchecked
      ExtendedEnumHelper helper = ExtendedEnumHelper.getExtendedEnumHelper((Class)aClass);
      if (o instanceof String) {
        return aClass.cast(helper.fromString((String)o));

      } else if (o instanceof Number) {
        return aClass.cast(helper.lookupById(((Number)o).intValue()));

      } else {
        throw new ConversionException("Don't know how to translate " + o.getClass() + " to " + aClass);
      }
    }
    throw new ConversionException("Don't know how to convert to " + aClass);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy