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.string;
19 import fulmine.util.reference.AutoCreatingStore;
20 import fulmine.util.reference.IAutoCreatingStore;
21 import fulmine.util.reference.IObjectBuilder;
22 import fulmine.util.reference.is;
23
24 /**
25 * A standard {@link IDomain} implementation backed by an
26 * {@link IAutoCreatingStore} that returns canonical {@link IDomain} instances.
27 * <p>
28 * Use the {@link Domain#get(byte)} factory method to obtain an instance.
29 *
30 * @author Ramon Servadei
31 */
32 public final class Domain implements IDomain
33 {
34 /** The builder for {@link Domain} objects */
35 private static final class DomainBuilder implements
36 IObjectBuilder<Byte, Domain>
37 {
38 public Domain create(Byte key)
39 {
40 return new Domain(key.byteValue());
41 }
42 }
43
44 /** The store of domain instances against domain code */
45 private final static IAutoCreatingStore<Byte, Domain> store =
46 new AutoCreatingStore<Byte, Domain>(new DomainBuilder());
47
48 /**
49 * Factory method to obtain a sharable, immutable {@link Domain} instance.
50 *
51 * @param code
52 * the code for the type (a byte)
53 * @param name
54 * the name for the type, <b>optional</b>. This is only set on
55 * the first call for each code. If no name is supplied on the
56 * first call, the domain will have no name, irrespective of
57 * subsequent calls.
58 * @return a sharable, immutable {@link IDomain} instance representing the
59 * code.
60 */
61 public static final IDomain get(int code, String... name)
62 {
63 final Byte byteCode = Byte.valueOf((byte) code);
64 if (store.containsKey(byteCode))
65 {
66 return store.get(byteCode);
67 }
68 final Domain domain = store.get(byteCode);
69 if (name.length == 1)
70 {
71 domain.setName(name[0]);
72 }
73 return domain;
74 }
75
76 /** The code for the domain */
77 private final byte code;
78
79 /** The name for the domain */
80 private String name;
81
82 /** The domain for framework entities */
83 public static final IDomain FRAMEWORK = get(0, "framework");
84
85 private Domain(byte code)
86 {
87 this.code = code;
88 }
89
90 public byte value()
91 {
92 return this.code;
93 }
94
95 public String getName()
96 {
97 return this.name;
98 }
99
100 void setName(String name)
101 {
102 this.name = name;
103 }
104
105 @Override
106 public final String toString()
107 {
108 return string(this, this.code + "," + this.name);
109 }
110
111 @Override
112 public int hashCode()
113 {
114 return this.code;
115 }
116
117 @Override
118 public boolean equals(Object obj)
119 {
120 if (is.same(this, obj))
121 {
122 return true;
123 }
124 if (is.differentClass(this, obj))
125 {
126 return false;
127 }
128 final Domain other = (Domain) obj;
129 return is.eq(this.code, other.code);
130 }
131 }