simplifications.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / ConfigImpl.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import static org.wamblee.xmlrouter.impl.MessageUtil.*;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.Iterator;
23 import java.util.List;
24
25 import org.wamblee.xmlrouter.common.Id;
26 import org.wamblee.xmlrouter.config.Config;
27 import org.wamblee.xmlrouter.config.Identifiable;
28
29 /**
30  * Default implementation of the {@link Config} interface.
31  * 
32  * @author Erik Brakkee
33  * 
34  * @param <T>
35  */
36 public abstract class ConfigImpl<T extends Identifiable<T>> implements
37     ExtendedConfig<T> {
38
39     private Id<Config> id;
40     private List<T> registered;
41
42     /**
43      * Constructs the object.
44      */
45     public ConfigImpl(Id<Config> aId) {
46         notNull("id", aId);
47         id = aId;
48         registered = new ArrayList<T>();
49     }
50
51     @Override
52     public Id<Config> getId() {
53         return id;
54     }
55
56     /*
57      * (non-Javadoc)
58      * 
59      * @see org.wamblee.xmlrouter.config.Config#add(T)
60      */
61     @Override
62     public synchronized void add(T aT) {
63         notNull("aT", aT);
64         registered.add(wrap(id.getId() + ".", aT));
65     }
66
67     /**
68      * This is called to wrap the given object by a safer version.
69      * 
70      * @param aT
71      *            Object to wrap.
72      * @return Wrapped object.
73      */
74     public abstract T wrap(String aPrefix, T aT);
75
76     /*
77      * (non-Javadoc)
78      * 
79      * @see
80      * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
81      * .Id)
82      */
83     @Override
84     public synchronized boolean remove(Id<T> aId) {
85         notNull("aId", aId);
86         Iterator<T> i = registered.iterator();
87         while (i.hasNext()) {
88             T t = i.next();
89             if (t.getId().equals(aId)) {
90                 i.remove();
91                 return true;
92             }
93         }
94         return false;
95     }
96
97     @Override
98     public List<T> values() {
99         return Collections.unmodifiableList(registered);
100     }
101 }