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

br.com.objectos.way.etc.EtcPropertyConvMap Maven / Gradle / Ivy

The newest version!
/*
 * EtcPropertyConv.java criado em 31/12/2013
 * 
 * Propriedade de Objectos Fábrica de Software LTDA.
 * Reprodução parcial ou total proibida.
 */
package br.com.objectos.way.etc;

import java.util.Map;

import com.google.common.collect.ImmutableMap;

/**
 * @author [email protected] (Marcio Endo)
 */
enum EtcPropertyConvMap {

  INSTANCE;

  private final Map, EtcPropertyConv> map = ImmutableMap
      ., EtcPropertyConv> builder()
      .put(Boolean.class, BooleanConv.INSTANCE)
      .put(boolean.class, BooleanConv.INSTANCE)
      .put(Integer.class, IntegerConv.INSTANCE)
      .put(int.class, IntegerConv.INSTANCE)
      .put(Long.class, LongConv.INSTANCE)
      .put(long.class, LongConv.INSTANCE)
      .put(Float.class, FloatConv.INSTANCE)
      .put(float.class, FloatConv.INSTANCE)
      .put(Double.class, DoubleConv.INSTANCE)
      .put(double.class, DoubleConv.INSTANCE)
      .build();

  public Object convert(Class fieldType, String value) {
    EtcPropertyConv conv = map.get(fieldType);

    if (conv == null) {
      conv = Noop.INSTANCE;
    }

    return conv.convert(value);
  }

  private enum Noop implements EtcPropertyConv {
    INSTANCE;
    @Override
    public Object convert(String val) {
      return val;
    }
  }

  private enum BooleanConv implements EtcPropertyConv {
    INSTANCE;
    @Override
    public Object convert(String val) {
      return Boolean.valueOf(val);
    }
  }
  private enum IntegerConv implements EtcPropertyConv {
    INSTANCE;
    @Override
    public Object convert(String val) {
      return Integer.valueOf(val);
    }
  }
  private enum LongConv implements EtcPropertyConv {
    INSTANCE;
    @Override
    public Object convert(String val) {
      return Long.valueOf(val);
    }
  }
  private enum FloatConv implements EtcPropertyConv {
    INSTANCE;
    @Override
    public Object convert(String val) {
      return Float.valueOf(val);
    }
  }
  private enum DoubleConv implements EtcPropertyConv {
    INSTANCE;
    @Override
    public Object convert(String val) {
      return Double.valueOf(val);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy