/* * 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 java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; import org.wamblee.xmlrouter.common.Id; import org.wamblee.xmlrouter.config.Config; import org.wamblee.xmlrouter.config.Identifiable; /** * Default implementation of the {@link Config} interface. * * @author Erik Brakkee * * @param */ public abstract class ConfigImpl implements ExtendedConfig { private Id id; private List registered; /** * Constructs the object. */ public ConfigImpl(Id aId) { // TODO test for null. id = aId; registered = new ArrayList(); } @Override public Id getId() { return id; } /* * (non-Javadoc) * * @see org.wamblee.xmlrouter.config.Config#add(T) */ @Override public synchronized void add(T aT) { // TODO test duplicate ids. notNull(aT); registered.add(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 aId) { notNull(aId); Iterator i = registered.iterator(); while (i.hasNext()) { T t = i.next(); if (t.getId().equals(aId)) { i.remove(); return true; } } return false; } @Override public List values() { return Collections.unmodifiableList(registered); } private void notNull(T aT) { if (aT == null) { throw new NullPointerException("Object is null"); } } private void notNull(Id aId) { if (aId == null) { throw new NullPointerException("Id is null"); } } }