789f75c5178db2da3579c10cd14f1fd96c193932
[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.Collection;
19 import java.util.Collections;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
22 import java.util.concurrent.atomic.AtomicLong;
23
24 import org.wamblee.xmlrouter.common.Id;
25 import org.wamblee.xmlrouter.config.Config;
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> implements Config<T> {
35
36     private AtomicLong next;
37     private Map<Id<T>, T> registered;
38
39     /**
40      * Constructs the object.
41      */
42     public ConfigImpl() {
43         next = new AtomicLong(1);
44         registered = new LinkedHashMap<Id<T>, T>();
45     }
46
47     /*
48      * (non-Javadoc)
49      * 
50      * @see org.wamblee.xmlrouter.config.Config#add(T)
51      */
52     @Override
53     public Id<T> add(T aT) {
54         notNull(aT);
55         long seqno = next.incrementAndGet();
56         Id<T> id = new Id<T>(seqno);
57         registered.put(id, wrap(id, aT));
58         return id;
59     }
60
61     public abstract T wrap(Id<T> aId, T aT);
62
63     /*
64      * (non-Javadoc)
65      * 
66      * @see
67      * org.wamblee.xmlrouter.config.Config#remove(org.wamblee.xmlrouter.common
68      * .Id)
69      */
70     @Override
71     public boolean remove(Id<T> aId) {
72         notNull(aId);
73         return registered.remove(aId) != null;
74     }
75
76     /*
77      * (non-Javadoc)
78      * 
79      * @see org.wamblee.xmlrouter.config.Config#ids()
80      */
81     @Override
82     public Collection<Id<T>> ids() {
83         return Collections.unmodifiableCollection(registered.keySet());
84     }
85
86     /*
87      * (non-Javadoc)
88      * 
89      * @see
90      * org.wamblee.xmlrouter.config.Config#get(org.wamblee.xmlrouter.common.Id)
91      */
92     @Override
93     public T get(Id<T> aId) {
94         notNull(aId);
95         return registered.get(aId);
96     }
97
98     private void notNull(T aT) {
99         if (aT == null) {
100             throw new NullPointerException("Object is null");
101         }
102     }
103
104     private void notNull(Id<T> aId) {
105         if (aId == null) {
106             throw new NullPointerException("Id is null");
107         }
108     }
109 }