Restructured the project creating a router directory for the router bundle implementa...
[xmlrouter] / router / 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.Identifiable;
25
26 /**
27  * Robust identifiable provides robustness for identifiable objects. It adds
28  * equality based on the id.
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     public RobustIdentifiable(Identifiable<T> aIdentifiable) {
41         notNull("identifiable", aIdentifiable);
42         try {
43             id = aIdentifiable.getId();
44             if (id == null) {
45                 throwConfigException("identifiable.getId() returned null", null);
46             }
47         } catch (Exception e) {
48             throwConfigException("identifiable.getId() threw exception", e);
49         }
50
51     }
52
53     private void throwConfigException(String aMsg, Exception aException) {
54         LOGGER.log(Level.WARNING, aMsg, aException);
55         throw new ConfigException(aMsg);
56     }
57
58     @Override
59     public Id<T> getId() {
60         return id;
61     }
62
63     @Override
64     public int hashCode() {
65         return id.hashCode();
66     }
67
68     @Override
69     public boolean equals(Object aObj) {
70         if (aObj == null) {
71             return false;
72         }
73         if (!getClass().isInstance(aObj)) {
74             return false;
75         }
76         RobustIdentifiable<T> obj = (RobustIdentifiable<T>) aObj;
77         return id.equals(obj.getId());
78     }
79 }