now making sure that ids re prefixed by the config id.
[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 /**
28  * Default implementation of the {@link Config} interface.
29  * 
30  * @author Erik Brakkee
31  * 
32  * @param <T>
33  */
34 // TODO make sure that each item inside this config is prefixed with the id of
35 // the config.
36 public abstract class ConfigImpl<T extends Identifiable> 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         // TODO test for null.
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         // TODO test duplicate ids.
64         notNull(aT);
65         registered.add(wrap(id.getId() + ".", aT));
66     }
67
68     /**
69      * This is called to wrap the given object by a safer version.
70      * 
71      * @param aT
72      *            Object to wrap.
73      * @return Wrapped object.
74      */
75     public abstract T wrap(String aPrefix, T aT);
76
77     /*
78      * (non-Javadoc)
79      * 
80      * @see
81      * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
82      * .Id)
83      */
84     @Override
85     public synchronized boolean remove(Id<T> aId) {
86         notNull(aId);
87         Iterator<T> i = registered.iterator();
88         while (i.hasNext()) {
89             T t = i.next();
90             if (t.getId().equals(aId)) {
91                 i.remove();
92                 return true;
93             }
94         }
95         return false;
96     }
97
98     @Override
99     public List<T> values() {
100         return Collections.unmodifiableList(registered);
101     }
102
103     private void notNull(T aT) {
104         if (aT == null) {
105             throw new NullPointerException("Object is null");
106         }
107     }
108
109     private void notNull(Id<T> aId) {
110         if (aId == null) {
111             throw new NullPointerException("Id is null");
112         }
113     }
114 }