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