added first version of configuraiton api and simple function test.
[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.Collections;
19 import java.util.LinkedHashMap;
20 import java.util.Map;
21 import java.util.concurrent.atomic.AtomicLong;
22
23 import org.wamblee.xmlrouter.common.Id;
24 import org.wamblee.xmlrouter.config.Config;
25
26 /**
27  * Default implementation of the {@link Config} interface.
28  * 
29  * @author Erik Brakkee
30  * 
31  * @param <T>
32  */
33 public abstract class ConfigImpl<T> implements ExtendedConfig<T> {
34
35     private boolean dirty;
36     private AtomicLong next;
37     private Map<Id<T>, T> registered;
38
39     /**
40      * Constructs the object.
41      */
42     public ConfigImpl(AtomicLong aNext) {
43         dirty = false;
44         next = aNext;
45         registered = new LinkedHashMap<Id<T>, T>();
46     }
47
48     /*
49      * (non-Javadoc)
50      * 
51      * @see org.wamblee.xmlrouter.config.Config#add(T)
52      */
53     @Override
54     public synchronized Id<T> add(T aT) {
55         notNull(aT);
56         long seqno = next.incrementAndGet();
57         Id<T> id = new Id<T>(seqno);
58         registered.put(id, wrap(id, aT));
59         dirty = true;
60         return id;
61     }
62
63     /**
64      * This is called to wrap the given object by a safer version.
65      * 
66      * @param aId
67      *            Id.
68      * @param aT
69      *            Object to wrap.
70      * @return Wrapped object.
71      */
72     public abstract T wrap(Id<T> aId, T aT);
73
74     /*
75      * (non-Javadoc)
76      * 
77      * @see
78      * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
79      * .Id)
80      */
81     @Override
82     public synchronized boolean remove(Id<T> aId) {
83         notNull(aId);
84         dirty = true;
85         return registered.remove(aId) != null;
86     }
87
88     @Override
89     public Map<Id<T>, T> map() {
90         return Collections.unmodifiableMap(registered);
91     }
92
93     private void notNull(T aT) {
94         if (aT == null) {
95             throw new NullPointerException("Object is null");
96         }
97     }
98
99     private void notNull(Id<T> aId) {
100         if (aId == null) {
101             throw new NullPointerException("Id is null");
102         }
103     }
104 }