import java.util.Set;
import org.wamblee.xmlrouter.common.Id;
-import org.wamblee.xmlrouter.config.Config;
-import org.wamblee.xmlrouter.config.DuplicateException;
import org.wamblee.xmlrouter.config.Identifiable;
/**
public class CompositeConfig<T extends Identifiable<T>> implements
ExtendedConfig<T> {
+ private static final String PREFIX = "compositeconfig";
private static final String READ_ONLY_INSTANCE = "read only instance";
- private Set<Id<Config>> ids;
- private Id<Config> id;
+
+ private Class<T> type;
+ private Set<String> prefixes;
private List<Id<T>> valueIds;
private List<T> values;
- public CompositeConfig(Id<Config> aId) {
- notNull("aId", aId);
- ids = new HashSet<Id<Config>>();
- id = aId;
+ public CompositeConfig(Class<T> aType) {
+ type = aType;
+ prefixes = new HashSet<String>();
valueIds = new ArrayList<Id<T>>();
values = new ArrayList<T>();
}
@Override
- public Id<Config> getId() {
- return id;
+ public Class<T> getType() {
+ return type;
+ }
+
+ @Override
+ public String getPrefix() {
+ return PREFIX;
}
public void addConfig(Config<T> aConfig) {
notNull("aConfig", aConfig);
- if (ids.contains(aConfig.getId())) {
- throw new DuplicateException(aConfig.getId().toString());
+ if (prefixes.contains(aConfig.getPrefix())) {
+ throw new ConfigException("duplicate prefix '" +
+ aConfig.getPrefix() + "'");
}
+ String prefix = aConfig.getPrefix() + ".";
for (T item : aConfig.values()) {
- if (valueIds.contains(item.getId())) {
- throw new DuplicateException(item.getId().toString());
+ Id<T> newId = new Id<T>(prefix + item.getId());
+ if (valueIds.contains(newId)) {
+ throw new ConfigException("duplicate id '" +
+ item.getId().toString() + "'");
}
}
- ids.add(aConfig.getId());
+ prefixes.add(aConfig.getPrefix());
+
for (T item : aConfig.values()) {
- valueIds.add(item.getId());
- values.add(item);
+ Id<T> newId = new Id<T>(prefix + item.getId());
+ valueIds.add(newId);
+ values.add(IdentifiablePrefixProxyFactory.getProxy(prefix, item,
+ Identifiable.class, aConfig.getType()));
}
}