RobustIdentifiable implemented and tested + test impacts.
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / RobustIdentifiable.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 static org.wamblee.xmlrouter.impl.MessageUtil.*;
19
20 import java.util.logging.Level;
21 import java.util.logging.Logger;
22
23 import org.wamblee.xmlrouter.common.Id;
24 import org.wamblee.xmlrouter.config.ConfigException;
25 import org.wamblee.xmlrouter.config.Identifiable;
26
27 /**
28  * Robust identifiable provides robustness for identifiable objects.
29  * 
30  * @author Erik Brakkee
31  * 
32  * @param <T>
33  */
34 public class RobustIdentifiable<T> implements Identifiable<T> {
35     private static final Logger LOGGER = Logger
36         .getLogger(RobustIdentifiable.class.getName());
37
38     private Id<T> id;
39
40     // TODO test this class.
41     // TODO test that id is constant even though delegated changes its id.
42
43     public RobustIdentifiable(String aPrefix, Identifiable<T> aIdentifiable) {
44         notNull("prefix", aPrefix);
45         notNull("identifiable", aIdentifiable);
46         // TODO test id is null
47         // TODO getId() throws exception
48         try {
49             id = aIdentifiable.getId();
50             if (id == null) {
51                 throwConfigException("identifiable.getId() returned null", null);
52             }
53             id = new Id<T>(aPrefix + id.getId());
54         } catch (Exception e) {
55             throwConfigException("identifiable.getId() threw exception", e);
56         }
57
58     }
59
60     private void throwConfigException(String aMsg, Exception aException) {
61         LOGGER.log(Level.WARNING, aMsg, aException);
62         throw new ConfigException("id is null");
63     }
64
65     @Override
66     public Id<T> getId() {
67         return id;
68     }
69
70     // TODO test equals, hashcode.
71
72     @Override
73     public int hashCode() {
74         return id.hashCode();
75     }
76
77     @Override
78     public boolean equals(Object aObj) {
79         if (aObj == null) {
80             return false;
81         }
82         if (!getClass().isInstance(aObj)) {
83             return false;
84         }
85         RobustIdentifiable<T> obj = (RobustIdentifiable<T>) aObj;
86         return id.equals(obj.getId());
87     }
88 }