View Javadoc

1   /*
2      Copyright 2007 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.protocol.wire;
17  
18  import static fulmine.util.Utils.CLOSE_BRACE;
19  import static fulmine.util.Utils.OPEN_BRACE;
20  import fulmine.model.component.IComponent;
21  import fulmine.util.reference.AutoCreatingStore;
22  import fulmine.util.reference.IAutoCreatingStore;
23  import fulmine.util.reference.IObjectBuilder;
24  import fulmine.util.reference.is;
25  
26  /**
27   * The default implementation of the {@link IWireIdentity} interface.
28   * 
29   * @author Ramon Servadei
30   * 
31   */
32  public class WireIdentity implements IWireIdentity
33  {
34  
35      /**
36       * Flag to indicate if the wire format is an integer. This is stored as a
37       * member rather than determining this on each call to
38       * {@link #isIntegerWireFormat()} for efficiency as the method is invoked
39       * multiple times on the same wire identity. Also, given that multiple
40       * entities may be sharing the same wire identity (for a description of
41       * 'container access scope' see {@link IComponent}), the memory cost of
42       * holding this member is further reduced.
43       */
44      private final boolean isInteger;
45  
46      /** The wire identity */
47      private final String wireIdentity;
48  
49      /** The builder for IWF {@link WireIdentity} objects */
50      private static final class IntegerWireIdBuilder implements
51          IObjectBuilder<Integer, WireIdentity>
52      {
53          public WireIdentity create(Integer key)
54          {
55              return new WireIdentity(key.intValue());
56          }
57      }
58  
59      /** The builder for SWF {@link WireIdentity} objects */
60      private static final class StringWireIdBuilder implements
61          IObjectBuilder<String, WireIdentity>
62      {
63          public WireIdentity create(String key)
64          {
65              return new WireIdentity(key);
66          }
67      }
68  
69      /** The registry of SWF canonical wire identity objects */
70      private final static IAutoCreatingStore<String, WireIdentity> stringWireIdRegistry =
71          new AutoCreatingStore<String, WireIdentity>(new StringWireIdBuilder());
72  
73      /** The registry of IWF canonical wire identity objects */
74      private final static IAutoCreatingStore<Integer, WireIdentity> intWireIdRegistry =
75          new AutoCreatingStore<Integer, WireIdentity>(new IntegerWireIdBuilder());
76  
77      /**
78       * Get the canonical SWF wire identity that represents the identity
79       * expressed by the string argument.
80       * 
81       * @param stringWireId
82       *            the identity of the SWF wire identity to return
83       * @return a canonical instance of the SWF wire identity that represents the
84       *         identity expressed by the string argument
85       */
86      public static synchronized WireIdentity get(String stringWireId)
87      {
88          return stringWireIdRegistry.get(stringWireId);
89      }
90  
91      /**
92       * Get the canonical IWF wire identity that represents the identity
93       * expressed by the integer argument.
94       * 
95       * @param intWireId
96       *            the identity of the IWF wire identity to return
97       * @return a canonical instance of the IWF wire identity that represents the
98       *         identity expressed by the integer argument
99       */
100     public static synchronized WireIdentity get(int intWireId)
101     {
102         return intWireIdRegistry.get(Integer.valueOf(intWireId));
103     }
104 
105     /**
106      * Constructor for an SWF wire identity
107      * 
108      * @param stringWireId
109      *            the string wire identity
110      */
111     private WireIdentity(String stringWireId)
112     {
113         super();
114         this.isInteger = false;
115         this.wireIdentity = stringWireId;
116     }
117 
118     /**
119      * Constructor for an IWF wire identity
120      * 
121      * @param intWireId
122      *            the integer wire identity
123      */
124     private WireIdentity(int intWireId)
125     {
126         super();
127         this.isInteger = true;
128         this.wireIdentity = Integer.toString(intWireId);
129     }
130 
131     public boolean isIntegerWireFormat()
132     {
133         return this.isInteger;
134     }
135 
136     public int getAsInteger()
137     {
138         return Integer.parseInt(this.wireIdentity);
139     }
140 
141     public String getAsString()
142     {
143         return this.wireIdentity;
144     }
145 
146     @Override
147     public int hashCode()
148     {
149         final int prime = 31;
150         int result = 1;
151         result = prime * result + (isInteger ? 1231 : 1237);
152         result =
153             prime * result
154                 + ((wireIdentity == null) ? 0 : wireIdentity.hashCode());
155         return result;
156     }
157 
158     @Override
159     public boolean equals(Object obj)
160     {
161         if (is.same(this, obj))
162         {
163             return true;
164         }
165         if (is.differentClass(this, obj))
166         {
167             return false;
168         }
169         final WireIdentity other = (WireIdentity) obj;
170         return is.eq(this.isInteger, other.isInteger)
171             && is.eq(this.wireIdentity, other.wireIdentity);
172     }
173 
174     /**
175      * Return the string representation in the form:
176      * 
177      * <pre>
178      * identity(type)
179      * </pre>
180      */
181     @Override
182     public String toString()
183     {
184         return wireIdentity + OPEN_BRACE
185             + (isIntegerWireFormat() ? "int" : "string") + CLOSE_BRACE;
186     }
187 
188 }