br.com.objectos.way.etc.EtcMap Maven / Gradle / Ivy
The newest version!
/*
* EtcMap.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.newHashMapWithExpectedSize;
import java.util.Map;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableMap;
import com.google.inject.Inject;
import com.google.inject.Singleton;
/**
* @author [email protected] (Marcio Endo)
*/
@Singleton
class EtcMap {
private final Map model;
private final Map nameMap;
@Inject
public EtcMap(Map model) {
this.model = model;
Map nameMap = newHashMapWithExpectedSize(model.size());
for (Map.Entry entry : model.entrySet()) {
EtcKey key = entry.getKey();
Class> type = key.get();
String name = type.getSimpleName();
Etc value = entry.getValue();
nameMap.put(name.toLowerCase(), value);
}
this.nameMap = ImmutableMap.copyOf(nameMap);
}
public Etc get(Class> type) {
return checkType(type);
}
public Etc get(EtcProperty property) {
String type = property.getType();
String name = type.toLowerCase();
Preconditions.checkArgument(nameMap.containsKey(name), "No mapping found for " + type);
return nameMap.get(name);
}
private Etc checkType(Class> type) {
EtcKey key = new EtcKey(type);
Preconditions.checkArgument(model.containsKey(key), "No mapping found for " + type);
return model.get(key);
}
}