Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
SerialNumber |
|
| 1.4545454545454546;1.455 | ||||
SerialNumber$1 |
|
| 1.4545454545454546;1.455 | ||||
SerialNumber$Context |
|
| 1.4545454545454546;1.455 |
1 | /* | |
2 | * Copyright (c) 2009, Swiss Federal Department of Defence Civil Protection and Sport | |
3 | * (http://www.vbs.admin.ch) | |
4 | * Copyright (c) 2009, Ergon Informatik AG (http://www.ergon.ch) | |
5 | * All rights reserved. | |
6 | * | |
7 | * Licensed under the Open Permis License which accompanies this distribution, | |
8 | * and is available at http://www.openpermis.org/BSDlicenceKent.txt | |
9 | */ | |
10 | package org.openpermis.policy.bean; | |
11 | ||
12 | import java.io.Serializable; | |
13 | ||
14 | /** | |
15 | * Serial number of a part. | |
16 | * <p>Serial numbers are unique with respect to their context:</p> | |
17 | * <ul> | |
18 | * <li>{@link SerialNumber#create()} creates a new serial number with a new context.</li> | |
19 | * <li>{@link SerialNumber#next()} creates a new serial number in the same context.</li> | |
20 | * </ul> | |
21 | * <p>The serial number {@link #UNDEFINED} is a special serial number that does not have a | |
22 | * context. Use this serial number for parts that do not need to have a well-defined serial | |
23 | * number. Note that undefined serial numbers return an undefined serial number if they are | |
24 | * asked to create the {@link #next()} serial number. | |
25 | * @since 0.1.0 | |
26 | */ | |
27 | 9265 | public final class SerialNumber |
28 | implements Serializable | |
29 | { | |
30 | ||
31 | //---- Static | |
32 | ||
33 | /** | |
34 | * @since 0.1.0 | |
35 | */ | |
36 | private static final long serialVersionUID = -1342243947699108706L; | |
37 | ||
38 | /** | |
39 | * Virtual serial number for parts that do not have a well-defined serial number. | |
40 | * <p>This serial number cannot create new unique serial numbers, instead newly created | |
41 | * serial numbers are undefined again.</p> | |
42 | * @see #next() | |
43 | * @since 0.1.0 | |
44 | */ | |
45 | 1 | public static final SerialNumber UNDEFINED = new SerialNumber(); |
46 | ||
47 | /** | |
48 | * Creates a new serial number which has its own numbering scheme. | |
49 | * <p>All serial numbers are unique in respect to the initial serial number created.</p> | |
50 | * @return the serial number with a new context requested. | |
51 | * @since 0.1.0 | |
52 | */ | |
53 | public static final SerialNumber create () { | |
54 | 448 | return new Context().create(); |
55 | } | |
56 | ||
57 | //---- State | |
58 | ||
59 | /** | |
60 | * The value of this serial number. | |
61 | * @since 0.1.0 | |
62 | */ | |
63 | private final long value; | |
64 | ||
65 | /** | |
66 | * The context in which the serial number was created. | |
67 | * @since 0.1.0 | |
68 | */ | |
69 | private final Context context; | |
70 | ||
71 | //---- Constructors | |
72 | ||
73 | /** | |
74 | * Creates a new undefined serial number. | |
75 | * @since 0.1.0 | |
76 | */ | |
77 | private SerialNumber () { | |
78 | 1 | this(0, null); |
79 | 1 | } |
80 | ||
81 | /** | |
82 | * Creates an serial number with the specified value. | |
83 | * @param value the value of this serial number. | |
84 | * @since 0.1.0 | |
85 | */ | |
86 | 9266 | private SerialNumber (long value, Context context) { |
87 | 9266 | this.value = value; |
88 | 9266 | this.context = context; |
89 | 9266 | } |
90 | ||
91 | //---- Methods | |
92 | ||
93 | /** | |
94 | * Returns the next free serial number unique with respect to the context of this serial number. | |
95 | * <p>Undefined serial numbers return an undefined serial number as they have no context.</p> | |
96 | * @return the serial number requested. | |
97 | * @since 0.1.0 | |
98 | */ | |
99 | public SerialNumber next () { | |
100 | 9066 | return this.context == null ? this : this.context.create(); |
101 | } | |
102 | ||
103 | /** | |
104 | * Returns the value of this serial number. | |
105 | * @return the value of this serial number. | |
106 | * @since 0.1.0 | |
107 | */ | |
108 | public long value () { | |
109 | 0 | return this.value; |
110 | } | |
111 | ||
112 | /** | |
113 | * Checks if the value of this serial number is undefined. | |
114 | * @return {@code true} if the value of this serial number is undefined. | |
115 | * @since 0.1.0 | |
116 | */ | |
117 | public boolean isUndefined () { | |
118 | 2 | return this.context == null; |
119 | } | |
120 | ||
121 | /** | |
122 | * Checks if this serial number lies in the same context as the one specified. | |
123 | * @param serial the serial number whose context is to be compared. | |
124 | * @return {@code true} if the serial number specified and this serial number lie in the same | |
125 | * context, {@code false} if they do not share the same context. | |
126 | * @since 0.1.0 | |
127 | */ | |
128 | public boolean equalContext (SerialNumber serial) { | |
129 | 0 | return serial != null && this.context == serial.context; |
130 | } | |
131 | ||
132 | //---- Object | |
133 | ||
134 | /** | |
135 | * Return a hash code based on the value of this serial number. | |
136 | * @return the hash code of this serial number. | |
137 | * @since 0.1.0 | |
138 | */ | |
139 | @Override | |
140 | public int hashCode () { | |
141 | 0 | return (int) (this.value ^ (this.value >>> Integer.SIZE)); |
142 | } | |
143 | ||
144 | /** | |
145 | * Compares another serial number to this one. | |
146 | * @param obj the serial number to compare to this one. | |
147 | * @return {@code true} if both serial numbers have the same context and value. | |
148 | * @since 0.1.0 | |
149 | */ | |
150 | @Override | |
151 | public boolean equals (Object obj) { | |
152 | 317 | if (obj instanceof SerialNumber) { |
153 | 317 | final SerialNumber serial = (SerialNumber) obj; |
154 | 317 | return this.context == serial.context && this.value == serial.value; |
155 | } | |
156 | 0 | return false; |
157 | } | |
158 | ||
159 | /** | |
160 | * Returns the hex representation of the value of this serial number. | |
161 | * @return the string representation of this serial number. | |
162 | * @since 0.1.0 | |
163 | */ | |
164 | @Override | |
165 | public String toString () { | |
166 | 43 | return "0x" + Long.toHexString(this.value); |
167 | } | |
168 | ||
169 | //---- Context | |
170 | ||
171 | /** | |
172 | * Context for creating serial numbers. | |
173 | * @since 0.1.0 | |
174 | */ | |
175 | 896 | private static final class Context |
176 | implements Serializable | |
177 | { | |
178 | ||
179 | //---- Static | |
180 | ||
181 | /** | |
182 | * @since 0.1.0 | |
183 | */ | |
184 | private static final long serialVersionUID = 529851258047190889L; | |
185 | ||
186 | //---- State | |
187 | ||
188 | /** | |
189 | * The last value created by this context. | |
190 | * @since 0.1.0 | |
191 | */ | |
192 | private volatile long lastValue; | |
193 | ||
194 | //---- Methods | |
195 | ||
196 | /** | |
197 | * Creates a new serial number that is unique to this context. | |
198 | * @return the new serial number requested. | |
199 | * @since 0.1.0 | |
200 | */ | |
201 | public SerialNumber create () { | |
202 | 9265 | return new SerialNumber(++this.lastValue, this); |
203 | } | |
204 | ||
205 | } | |
206 | ||
207 | } |