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