0324e78f8353b1dd58598e49b66ecca786b6552d
[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 // TODO think real hard about the prefixing. We want a consistent view for clients. 
30 // perhaps only provide a method to add items and hide all access to the ids. 
31
32 /**
33  * Default implementation of the {@link Config} interface.
34  * 
35  * @author Erik Brakkee
36  * 
37  * @param <T>
38  */
39 // TODO make sure that each item inside this config is prefixed with the id of
40 // the config.
41 public abstract class ConfigImpl<T extends Identifiable<T>> implements
42     ExtendedConfig<T> {
43
44     private Id<Config> id;
45     private List<T> registered;
46
47     /**
48      * Constructs the object.
49      */
50     public ConfigImpl(Id<Config> aId) {
51         // TODO test for null.
52         id = aId;
53         registered = new ArrayList<T>();
54     }
55
56     @Override
57     public Id<Config> getId() {
58         return id;
59     }
60
61     /*
62      * (non-Javadoc)
63      * 
64      * @see org.wamblee.xmlrouter.config.Config#add(T)
65      */
66     @Override
67     public synchronized void add(T aT) {
68         notNull("aT", aT);
69         registered.add(wrap(id.getId() + ".", aT));
70     }
71
72     /**
73      * This is called to wrap the given object by a safer version.
74      * 
75      * @param aT
76      *            Object to wrap.
77      * @return Wrapped object.
78      */
79     public abstract T wrap(String aPrefix, T aT);
80
81     /*
82      * (non-Javadoc)
83      * 
84      * @see
85      * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
86      * .Id)
87      */
88     @Override
89     public synchronized boolean remove(Id<T> aId) {
90         notNull("aId", aId);
91         Iterator<T> i = registered.iterator();
92         while (i.hasNext()) {
93             T t = i.next();
94             if (t.getId().equals(aId)) {
95                 i.remove();
96                 return true;
97             }
98         }
99         return false;
100     }
101
102     @Override
103     public List<T> values() {
104         return Collections.unmodifiableList(registered);
105     }
106 }