1 | /*
|
---|
2 | * Licensed to the Apache Software Foundation (ASF) under one or more
|
---|
3 | * contributor license agreements. See the NOTICE file distributed with
|
---|
4 | * this work for additional information regarding copyright ownership.
|
---|
5 | * The ASF licenses this file to You under the Apache License, Version 2.0
|
---|
6 | * (the "License"); you may not use this file except in compliance with
|
---|
7 | * the License. You may obtain a copy of the License at
|
---|
8 | *
|
---|
9 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
10 | *
|
---|
11 | * Unless required by applicable law or agreed to in writing, software
|
---|
12 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
14 | * See the License for the specific language governing permissions and
|
---|
15 | * limitations under the License.
|
---|
16 | */
|
---|
17 | package agents.org.apache.commons.lang.mutable;
|
---|
18 |
|
---|
19 | /**
|
---|
20 | * A mutable <code>byte</code> wrapper.
|
---|
21 | *
|
---|
22 | * @see Byte
|
---|
23 | * @since 2.1
|
---|
24 | * @author Apache Software Foundation
|
---|
25 | * @version $Id: MutableByte.java 905707 2010-02-02 16:59:59Z niallp $
|
---|
26 | */
|
---|
27 | public class MutableByte extends Number implements Comparable, Mutable {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Required for serialization support.
|
---|
31 | *
|
---|
32 | * @see java.io.Serializable
|
---|
33 | */
|
---|
34 | private static final long serialVersionUID = -1585823265L;
|
---|
35 |
|
---|
36 | /** The mutable value. */
|
---|
37 | private byte value;
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Constructs a new MutableByte with the default value of zero.
|
---|
41 | */
|
---|
42 | public MutableByte() {
|
---|
43 | super();
|
---|
44 | }
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Constructs a new MutableByte with the specified value.
|
---|
48 | *
|
---|
49 | * @param value the initial value to store
|
---|
50 | */
|
---|
51 | public MutableByte(byte value) {
|
---|
52 | super();
|
---|
53 | this.value = value;
|
---|
54 | }
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Constructs a new MutableByte with the specified value.
|
---|
58 | *
|
---|
59 | * @param value the initial value to store, not null
|
---|
60 | * @throws NullPointerException if the object is null
|
---|
61 | */
|
---|
62 | public MutableByte(Number value) {
|
---|
63 | super();
|
---|
64 | this.value = value.byteValue();
|
---|
65 | }
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * Constructs a new MutableByte parsing the given string.
|
---|
69 | *
|
---|
70 | * @param value the string to parse, not null
|
---|
71 | * @throws NumberFormatException if the string cannot be parsed into a byte
|
---|
72 | * @since 2.5
|
---|
73 | */
|
---|
74 | public MutableByte(String value) throws NumberFormatException {
|
---|
75 | super();
|
---|
76 | this.value = Byte.parseByte(value);
|
---|
77 | }
|
---|
78 |
|
---|
79 | //-----------------------------------------------------------------------
|
---|
80 | /**
|
---|
81 | * Gets the value as a Byte instance.
|
---|
82 | *
|
---|
83 | * @return the value as a Byte, never null
|
---|
84 | */
|
---|
85 | public Object getValue() {
|
---|
86 | return new Byte(this.value);
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Sets the value.
|
---|
91 | *
|
---|
92 | * @param value the value to set
|
---|
93 | */
|
---|
94 | public void setValue(byte value) {
|
---|
95 | this.value = value;
|
---|
96 | }
|
---|
97 |
|
---|
98 | /**
|
---|
99 | * Sets the value from any Number instance.
|
---|
100 | *
|
---|
101 | * @param value the value to set, not null
|
---|
102 | * @throws NullPointerException if the object is null
|
---|
103 | * @throws ClassCastException if the type is not a {@link Number}
|
---|
104 | */
|
---|
105 | public void setValue(Object value) {
|
---|
106 | setValue(((Number) value).byteValue());
|
---|
107 | }
|
---|
108 |
|
---|
109 | //-----------------------------------------------------------------------
|
---|
110 | /**
|
---|
111 | * Increments the value.
|
---|
112 | *
|
---|
113 | * @since Commons Lang 2.2
|
---|
114 | */
|
---|
115 | public void increment() {
|
---|
116 | value++;
|
---|
117 | }
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Decrements the value.
|
---|
121 | *
|
---|
122 | * @since Commons Lang 2.2
|
---|
123 | */
|
---|
124 | public void decrement() {
|
---|
125 | value--;
|
---|
126 | }
|
---|
127 |
|
---|
128 | //-----------------------------------------------------------------------
|
---|
129 | /**
|
---|
130 | * Adds a value to the value of this instance.
|
---|
131 | *
|
---|
132 | * @param operand the value to add, not null
|
---|
133 | * @since Commons Lang 2.2
|
---|
134 | */
|
---|
135 | public void add(byte operand) {
|
---|
136 | this.value += operand;
|
---|
137 | }
|
---|
138 |
|
---|
139 | /**
|
---|
140 | * Adds a value to the value of this instance.
|
---|
141 | *
|
---|
142 | * @param operand the value to add, not null
|
---|
143 | * @throws NullPointerException if the object is null
|
---|
144 | * @since Commons Lang 2.2
|
---|
145 | */
|
---|
146 | public void add(Number operand) {
|
---|
147 | this.value += operand.byteValue();
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Subtracts a value from the value of this instance.
|
---|
152 | *
|
---|
153 | * @param operand the value to subtract, not null
|
---|
154 | * @since Commons Lang 2.2
|
---|
155 | */
|
---|
156 | public void subtract(byte operand) {
|
---|
157 | this.value -= operand;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * Subtracts a value from the value of this instance.
|
---|
162 | *
|
---|
163 | * @param operand the value to subtract, not null
|
---|
164 | * @throws NullPointerException if the object is null
|
---|
165 | * @since Commons Lang 2.2
|
---|
166 | */
|
---|
167 | public void subtract(Number operand) {
|
---|
168 | this.value -= operand.byteValue();
|
---|
169 | }
|
---|
170 |
|
---|
171 | //-----------------------------------------------------------------------
|
---|
172 | // shortValue relies on Number implementation
|
---|
173 | /**
|
---|
174 | * Returns the value of this MutableByte as a byte.
|
---|
175 | *
|
---|
176 | * @return the numeric value represented by this object after conversion to type byte.
|
---|
177 | */
|
---|
178 | public byte byteValue() {
|
---|
179 | return value;
|
---|
180 | }
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * Returns the value of this MutableByte as an int.
|
---|
184 | *
|
---|
185 | * @return the numeric value represented by this object after conversion to type int.
|
---|
186 | */
|
---|
187 | public int intValue() {
|
---|
188 | return value;
|
---|
189 | }
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Returns the value of this MutableByte as a long.
|
---|
193 | *
|
---|
194 | * @return the numeric value represented by this object after conversion to type long.
|
---|
195 | */
|
---|
196 | public long longValue() {
|
---|
197 | return value;
|
---|
198 | }
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * Returns the value of this MutableByte as a float.
|
---|
202 | *
|
---|
203 | * @return the numeric value represented by this object after conversion to type float.
|
---|
204 | */
|
---|
205 | public float floatValue() {
|
---|
206 | return value;
|
---|
207 | }
|
---|
208 |
|
---|
209 | /**
|
---|
210 | * Returns the value of this MutableByte as a double.
|
---|
211 | *
|
---|
212 | * @return the numeric value represented by this object after conversion to type double.
|
---|
213 | */
|
---|
214 | public double doubleValue() {
|
---|
215 | return value;
|
---|
216 | }
|
---|
217 |
|
---|
218 | //-----------------------------------------------------------------------
|
---|
219 | /**
|
---|
220 | * Gets this mutable as an instance of Byte.
|
---|
221 | *
|
---|
222 | * @return a Byte instance containing the value from this mutable
|
---|
223 | */
|
---|
224 | public Byte toByte() {
|
---|
225 | return new Byte(byteValue());
|
---|
226 | }
|
---|
227 |
|
---|
228 | //-----------------------------------------------------------------------
|
---|
229 | /**
|
---|
230 | * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
|
---|
231 | * not <code>null</code> and is a <code>MutableByte</code> object that contains the same <code>byte</code> value
|
---|
232 | * as this object.
|
---|
233 | *
|
---|
234 | * @param obj the object to compare with, null returns false
|
---|
235 | * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
|
---|
236 | */
|
---|
237 | public boolean equals(Object obj) {
|
---|
238 | if (obj instanceof MutableByte) {
|
---|
239 | return value == ((MutableByte) obj).byteValue();
|
---|
240 | }
|
---|
241 | return false;
|
---|
242 | }
|
---|
243 |
|
---|
244 | /**
|
---|
245 | * Returns a suitable hash code for this mutable.
|
---|
246 | *
|
---|
247 | * @return a suitable hash code
|
---|
248 | */
|
---|
249 | public int hashCode() {
|
---|
250 | return value;
|
---|
251 | }
|
---|
252 |
|
---|
253 | //-----------------------------------------------------------------------
|
---|
254 | /**
|
---|
255 | * Compares this mutable to another in ascending order.
|
---|
256 | *
|
---|
257 | * @param obj the other mutable to compare to, not null
|
---|
258 | * @return negative if this is less, zero if equal, positive if greater
|
---|
259 | * @throws ClassCastException if the argument is not a MutableByte
|
---|
260 | */
|
---|
261 | public int compareTo(Object obj) {
|
---|
262 | MutableByte other = (MutableByte) obj;
|
---|
263 | byte anotherVal = other.value;
|
---|
264 | return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
|
---|
265 | }
|
---|
266 |
|
---|
267 | //-----------------------------------------------------------------------
|
---|
268 | /**
|
---|
269 | * Returns the String value of this mutable.
|
---|
270 | *
|
---|
271 | * @return the mutable value as a string
|
---|
272 | */
|
---|
273 | public String toString() {
|
---|
274 | return String.valueOf(value);
|
---|
275 | }
|
---|
276 |
|
---|
277 | }
|
---|