br.com.objectos.way.etc.EtcProperty Maven / Gradle / Ivy
The newest version!
/*
* EtcProperty.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 static com.google.common.collect.Maps.newHashMap;
import java.lang.reflect.Field;
import java.util.List;
import java.util.Map;
import com.google.common.base.Preconditions;
import com.google.common.base.Splitter;
/**
* @author [email protected] (Marcio Endo)
*/
class EtcProperty {
private final String type;
private final String property;
private final Object value;
private EtcProperty(String type, String property, Object value) {
this.type = type;
this.property = property;
this.value = value;
}
public static EtcProperty fromString(String string) {
List parts = Splitter.on('.').splitToList(string);
Preconditions.checkArgument(parts.size() == 2);
String type = parts.get(0);
String property = parts.get(1);
return new EtcProperty(type, property, "");
}
public EtcProperty set(Class> clazz, String value) {
try {
Field field = clazz.getDeclaredField(property);
Class> fieldType = field.getType();
Object val = EtcPropertyConvMap.INSTANCE.convert(fieldType, value);
return new EtcProperty(type, property, val);
} catch (SecurityException e) {
throw new WayEtcException("Field " + property + " could not be accessed in type "
+ clazz.getSimpleName());
} catch (NoSuchFieldException e) {
throw new WayEtcException("Field " + property + " does not exist in type "
+ clazz.getSimpleName());
}
}
public String getType() {
return type;
}
public String getProperty() {
return property;
}
public Map write(Map map) {
Map mutable = newHashMap(map);
mutable.put(property, value);
return mutable;
}
}