View Javadoc

1   /*
2      Copyright 2008 Ramon Servadei
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 fulmine;
17  
18  import static fulmine.util.Utils.COLON;
19  import static fulmine.util.Utils.nullCheck;
20  import static fulmine.util.Utils.string;
21  
22  import org.apache.commons.lang.builder.HashCodeBuilder;
23  
24  import fulmine.util.reference.is;
25  
26  /**
27   * Basic implementation of an {@link IAddressable} object
28   * 
29   * @author Ramon Servadei
30   */
31  public class Addressable implements IAddressable
32  {
33      /** The type for the addressable */
34      private final IType type;
35  
36      /** The domain for the addressable */
37      private final IDomain domain;
38  
39      /** The identity for the addressable */
40      private final String identity;
41  
42      /** The hashcode for this */
43      private final int hashCode;
44  
45      /** The unique address for this */
46      private final String address;
47  
48      /**
49       * Standard clone constructor
50       * 
51       * @param id
52       *            the addressable instance to clone from
53       */
54      public Addressable(IAddressable id)
55      {
56          this(id.getIdentity(), id.getType(), id.getDomain());
57      }
58  
59      /**
60       * Standard constructor
61       * 
62       * @param identity
63       *            the identity
64       * @param type
65       *            the type
66       * @param domain
67       *            the domain
68       */
69      public Addressable(String identity, IType type, IDomain domain)
70      {
71          super();
72          nullCheck(identity, "No identity");
73          this.type = type;
74          this.domain = domain;
75          this.identity = identity;
76          this.hashCode =
77              new HashCodeBuilder().append(getIdentity()).append(getType()).append(
78                  getDomain()).toHashCode();
79          this.address = getIdentity() + COLON + getType() + COLON + getDomain();
80      }
81  
82      public final IType getType()
83      {
84          return this.type;
85      }
86  
87      public final IDomain getDomain()
88      {
89          return this.domain;
90      }
91  
92      public final String getIdentity()
93      {
94          return this.identity;
95      }
96  
97      @Override
98      public final String toString()
99      {
100         return string(this, getAddress());
101     }
102 
103     @Override
104     public int hashCode()
105     {
106         return this.hashCode;
107     }
108 
109     @Override
110     public boolean equals(Object obj)
111     {
112         if (is.same(this, obj))
113         {
114             return true;
115         }
116         if (obj == null)
117         {
118             return false;
119         }
120         if (!(obj instanceof IAddressable))
121         {
122             return false;
123         }
124         final IAddressable other = (IAddressable) obj;
125         return is.eq(getAddress(), other.getAddress());
126     }
127 
128     public String getAddress()
129     {
130         return this.address;
131     }
132 }