75b8222c04363038f3ed289eb5386a9c7843aded
[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 // TODO make sure that each item inside this config is prefixed with the id of
37 // the config.
38 public abstract class ConfigImpl<T extends Identifiable<T>> implements
39     ExtendedConfig<T> {
40
41     private Id<Config> id;
42     private List<T> registered;
43
44     /**
45      * Constructs the object.
46      */
47     public ConfigImpl(Id<Config> aId) {
48         notNull("id", aId);
49         id = aId;
50         registered = new ArrayList<T>();
51     }
52
53     @Override
54     public Id<Config> getId() {
55         return id;
56     }
57
58     /*
59      * (non-Javadoc)
60      * 
61      * @see org.wamblee.xmlrouter.config.Config#add(T)
62      */
63     @Override
64     public synchronized void add(T aT) {
65         notNull("aT", aT);
66         registered.add(wrap(id.getId() + ".", aT));
67     }
68
69     /**
70      * This is called to wrap the given object by a safer version.
71      * 
72      * @param aT
73      *            Object to wrap.
74      * @return Wrapped object.
75      */
76     public abstract T wrap(String aPrefix, T aT);
77
78     /*
79      * (non-Javadoc)
80      * 
81      * @see
82      * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
83      * .Id)
84      */
85     @Override
86     public synchronized boolean remove(Id<T> aId) {
87         notNull("aId", aId);
88         Iterator<T> i = registered.iterator();
89         while (i.hasNext()) {
90             T t = i.next();
91             if (t.getId().equals(aId)) {
92                 i.remove();
93                 return true;
94             }
95         }
96         return false;
97     }
98
99     @Override
100     public List<T> values() {
101         return Collections.unmodifiableList(registered);
102     }
103 }