br.com.objectos.way.etc.MappingPojo Maven / Gradle / Ivy
The newest version!
/*
* SnakeYamlMappgin.java criado em 07/09/2012
*
* Propriedade de Objectos Fábrica de Software LTDA.
* Reprodução parcial ou total proibida.
*/
package br.com.objectos.way.etc;
import static com.google.common.collect.Lists.transform;
import java.util.List;
import java.util.Map;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableList;
/**
* @author [email protected] (Marcio Endo)
*/
class MappingPojo implements Mapping {
private final Map map;
public MappingPojo(Map map) {
this.map = map;
}
@SuppressWarnings("unchecked")
@Override
public Mapping getMapping(String key) {
Mapping res = MappingEmpty.INSTANCE;
Object object = map.get(key);
if (object != null) {
res = new MappingPojo((Map) object);
}
return res;
}
@Override
public List getSequence(String key) {
List res = ImmutableList.of();
Object object = map.get(key);
if (object != null) {
res = ToMapping.load(object);
}
return res;
}
@Override
public String getString(String key) {
Object object = map.get(key);
if (object == null) {
object = "";
}
return object.toString();
}
@Override
public boolean getBoolean(String key) {
boolean res = false;
Object object = map.get(key);
if (object != null) {
res = Boolean.class.cast(object).booleanValue();
}
return res;
}
@Override
public int getInt(String key) {
int res = 0;
Object object = map.get(key);
if (object != null) {
res = Integer.class.cast(object).intValue();
}
return res;
}
@Override
public long getLong(String key) {
long res = 0;
Object object = map.get(key);
if (object != null) {
res = Long.class.cast(object).longValue();
}
return res;
}
@Override
public float getFloat(String key) {
float res = 0;
Object object = map.get(key);
if (object != null) {
res = Double.class.cast(object).floatValue();
}
return res;
}
@Override
public double getDouble(String key) {
double res = 0;
Object object = map.get(key);
if (object != null) {
res = Double.class.cast(object).doubleValue();
}
return res;
}
@Override
public List loadSequence(String key, EtcLoader loader) {
List mappings;
mappings = getSequence(key);
List loaded;
loaded = transform(mappings, new LoaderWrapper(loader));
return ImmutableList.copyOf(loaded);
}
private static class LoaderWrapper implements Function {
private final EtcLoader loader;
public LoaderWrapper(EtcLoader loader) {
this.loader = loader;
}
@Override
public T apply(Mapping input) {
return loader.load(input);
}
}
}