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