001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.commons.lang3.mutable;
018
019 /**
020 * A mutable <code>byte</code> wrapper.
021 *
022 * @see Byte
023 * @since 2.1
024 * @author Apache Software Foundation
025 * @version $Id: MutableByte.java 916081 2010-02-25 01:28:13Z niallp $
026 */
027 public class MutableByte extends Number implements Comparable<MutableByte>, Mutable<Number> {
028
029 /**
030 * Required for serialization support.
031 *
032 * @see java.io.Serializable
033 */
034 private static final long serialVersionUID = -1585823265L;
035
036 /** The mutable value. */
037 private byte value;
038
039 /**
040 * Constructs a new MutableByte with the default value of zero.
041 */
042 public MutableByte() {
043 super();
044 }
045
046 /**
047 * Constructs a new MutableByte with the specified value.
048 *
049 * @param value the initial value to store
050 */
051 public MutableByte(byte value) {
052 super();
053 this.value = value;
054 }
055
056 /**
057 * Constructs a new MutableByte with the specified value.
058 *
059 * @param value the initial value to store, not null
060 * @throws NullPointerException if the object is null
061 */
062 public MutableByte(Number value) {
063 super();
064 this.value = value.byteValue();
065 }
066
067 /**
068 * Constructs a new MutableByte parsing the given string.
069 *
070 * @param value the string to parse, not null
071 * @throws NumberFormatException if the string cannot be parsed into a byte
072 * @since 2.5
073 */
074 public MutableByte(String value) throws NumberFormatException {
075 super();
076 this.value = Byte.parseByte(value);
077 }
078
079 //-----------------------------------------------------------------------
080 /**
081 * Gets the value as a Byte instance.
082 *
083 * @return the value as a Byte, never null
084 */
085 public Byte getValue() {
086 return Byte.valueOf(this.value);
087 }
088
089 /**
090 * Sets the value.
091 *
092 * @param value the value to set
093 */
094 public void setValue(byte value) {
095 this.value = value;
096 }
097
098 /**
099 * 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 */
104 public void setValue(Number value) {
105 this.value = value.byteValue();
106 }
107
108 //-----------------------------------------------------------------------
109 /**
110 * Increments the value.
111 *
112 * @since Commons Lang 2.2
113 */
114 public void increment() {
115 value++;
116 }
117
118 /**
119 * Decrements the value.
120 *
121 * @since Commons Lang 2.2
122 */
123 public void decrement() {
124 value--;
125 }
126
127 //-----------------------------------------------------------------------
128 /**
129 * Adds a value to the value of this instance.
130 *
131 * @param operand the value to add, not null
132 * @since Commons Lang 2.2
133 */
134 public void add(byte operand) {
135 this.value += operand;
136 }
137
138 /**
139 * Adds a value to the value of this instance.
140 *
141 * @param operand the value to add, not null
142 * @throws NullPointerException if the object is null
143 * @since Commons Lang 2.2
144 */
145 public void add(Number operand) {
146 this.value += operand.byteValue();
147 }
148
149 /**
150 * Subtracts a value from the value of this instance.
151 *
152 * @param operand the value to subtract, not null
153 * @since Commons Lang 2.2
154 */
155 public void subtract(byte operand) {
156 this.value -= operand;
157 }
158
159 /**
160 * Subtracts a value from the value of this instance.
161 *
162 * @param operand the value to subtract, not null
163 * @throws NullPointerException if the object is null
164 * @since Commons Lang 2.2
165 */
166 public void subtract(Number operand) {
167 this.value -= operand.byteValue();
168 }
169
170 //-----------------------------------------------------------------------
171 // shortValue relies on Number implementation
172 /**
173 * Returns the value of this MutableByte as a byte.
174 *
175 * @return the numeric value represented by this object after conversion to type byte.
176 */
177 @Override
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 @Override
188 public int intValue() {
189 return value;
190 }
191
192 /**
193 * Returns the value of this MutableByte as a long.
194 *
195 * @return the numeric value represented by this object after conversion to type long.
196 */
197 @Override
198 public long longValue() {
199 return value;
200 }
201
202 /**
203 * Returns the value of this MutableByte as a float.
204 *
205 * @return the numeric value represented by this object after conversion to type float.
206 */
207 @Override
208 public float floatValue() {
209 return value;
210 }
211
212 /**
213 * Returns the value of this MutableByte as a double.
214 *
215 * @return the numeric value represented by this object after conversion to type double.
216 */
217 @Override
218 public double doubleValue() {
219 return value;
220 }
221
222 //-----------------------------------------------------------------------
223 /**
224 * Gets this mutable as an instance of Byte.
225 *
226 * @return a Byte instance containing the value from this mutable
227 */
228 public Byte toByte() {
229 return Byte.valueOf(byteValue());
230 }
231
232 //-----------------------------------------------------------------------
233 /**
234 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
235 * not <code>null</code> and is a <code>MutableByte</code> object that contains the same <code>byte</code> value
236 * as this object.
237 *
238 * @param obj the object to compare with, null returns false
239 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
240 */
241 @Override
242 public boolean equals(Object obj) {
243 if (obj instanceof MutableByte) {
244 return value == ((MutableByte) obj).byteValue();
245 }
246 return false;
247 }
248
249 /**
250 * Returns a suitable hash code for this mutable.
251 *
252 * @return a suitable hash code
253 */
254 @Override
255 public int hashCode() {
256 return value;
257 }
258
259 //-----------------------------------------------------------------------
260 /**
261 * Compares this mutable to another in ascending order.
262 *
263 * @param other the other mutable to compare to, not null
264 * @return negative if this is less, zero if equal, positive if greater
265 */
266 public int compareTo(MutableByte other) {
267 byte anotherVal = other.value;
268 return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
269 }
270
271 //-----------------------------------------------------------------------
272 /**
273 * Returns the String value of this mutable.
274 *
275 * @return the mutable value as a string
276 */
277 @Override
278 public String toString() {
279 return String.valueOf(value);
280 }
281
282 }