+++ /dev/null
-/*
- * Copyright 2005-2011 the original author or authors.
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.wamblee.xmlrouter.impl;
-
-import static org.wamblee.xmlrouter.impl.MessageUtil.*;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Map;
-
-import org.wamblee.xmlrouter.common.Id;
-import org.wamblee.xmlrouter.config.Identifiable;
-
-/**
- * Default implementation of the {@link Config} interface.
- *
- * @author Erik Brakkee
- *
- * @param <T>
- */
-public abstract class ConfigImpl<T extends Identifiable<T>> implements
- ExtendedConfig<T> {
-
- private Class<T> type;
- private String prefix;
- private Map<Id<T>, T> registered;
-
- /**
- * Constructs the object.
- */
- public ConfigImpl(Class<T> aType, String aPrefix) {
- notNull("type", aType);
- notNull("id", aPrefix);
- type = aType;
- prefix = aPrefix;
- registered = new HashMap<Id<T>, T>();
- }
-
- @Override
- public Class<T> getType() {
- return type;
- }
-
- /**
- * Copies the config object.
- *
- * @param aConfig
- * Config to copy.
- */
- public ConfigImpl(ConfigImpl<T> aConfig) {
- notNull("config", aConfig);
- prefix = aConfig.prefix;
- registered = new HashMap<Id<T>, T>();
- for (Map.Entry<Id<T>, T> entry : aConfig.registered.entrySet()) {
- registered.put(entry.getKey(), entry.getValue());
- }
- }
-
- @Override
- public String getPrefix() {
- return prefix;
- }
-
- /*
- * (non-Javadoc)
- *
- * @see org.wamblee.xmlrouter.config.Config#add(T)
- */
- @Override
- public synchronized void add(T aT) {
- notNull("aT", aT);
- if (registered.containsKey(aT.getId())) {
- throw new ConfigException("Duplicate id '" + aT.getId() + "'");
- }
- registered.put(aT.getId(), wrap(aT));
- }
-
- /**
- * This is called to wrap the given object by a safer version.
- *
- * @param aT
- * Object to wrap.
- * @return Wrapped object.
- */
- public abstract T wrap(T aT);
-
- /*
- * (non-Javadoc)
- *
- * @see
- * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
- * .Id)
- */
- @Override
- public synchronized boolean remove(Id<T> aId) {
- notNull("aId", aId);
- T value = registered.get(aId);
- if (value != null) {
- registered.remove(aId);
- return true;
- }
- return false;
- }
-
- @Override
- public Collection<T> values() {
- return Collections.unmodifiableCollection(registered.values());
- }
-
- @Override
- public boolean equals(Object aObj) {
- if (aObj == null) {
- return false;
- }
- if (!(aObj instanceof ConfigImpl)) {
- return false;
- }
- ConfigImpl obj = (ConfigImpl) aObj;
- return registered.keySet().equals(obj.registered.keySet());
- }
-
- @Override
- public int hashCode() {
- return registered.keySet().hashCode();
- }
-}