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 * @version $Id: MutableByte.java 1088899 2011-04-05 05:31:27Z bayard $
025 */
026 public class MutableByte extends Number implements Comparable<MutableByte>, Mutable<Number> {
027
028 /**
029 * Required for serialization support.
030 *
031 * @see java.io.Serializable
032 */
033 private static final long serialVersionUID = -1585823265L;
034
035 /** The mutable value. */
036 private byte value;
037
038 /**
039 * Constructs a new MutableByte with the default value of zero.
040 */
041 public MutableByte() {
042 super();
043 }
044
045 /**
046 * Constructs a new MutableByte with the specified value.
047 *
048 * @param value the initial value to store
049 */
050 public MutableByte(byte value) {
051 super();
052 this.value = value;
053 }
054
055 /**
056 * Constructs a new MutableByte with the specified value.
057 *
058 * @param value the initial value to store, not null
059 * @throws NullPointerException if the object is null
060 */
061 public MutableByte(Number value) {
062 super();
063 this.value = value.byteValue();
064 }
065
066 /**
067 * Constructs a new MutableByte parsing the given string.
068 *
069 * @param value the string to parse, not null
070 * @throws NumberFormatException if the string cannot be parsed into a byte
071 * @since 2.5
072 */
073 public MutableByte(String value) throws NumberFormatException {
074 super();
075 this.value = Byte.parseByte(value);
076 }
077
078 //-----------------------------------------------------------------------
079 /**
080 * Gets the value as a Byte instance.
081 *
082 * @return the value as a Byte, never null
083 */
084 public Byte getValue() {
085 return Byte.valueOf(this.value);
086 }
087
088 /**
089 * Sets the value.
090 *
091 * @param value the value to set
092 */
093 public void setValue(byte value) {
094 this.value = value;
095 }
096
097 /**
098 * Sets the value from any Number instance.
099 *
100 * @param value the value to set, not null
101 * @throws NullPointerException if the object is null
102 */
103 public void setValue(Number value) {
104 this.value = value.byteValue();
105 }
106
107 //-----------------------------------------------------------------------
108 /**
109 * Increments the value.
110 *
111 * @since Commons Lang 2.2
112 */
113 public void increment() {
114 value++;
115 }
116
117 /**
118 * Decrements the value.
119 *
120 * @since Commons Lang 2.2
121 */
122 public void decrement() {
123 value--;
124 }
125
126 //-----------------------------------------------------------------------
127 /**
128 * Adds a value to the value of this instance.
129 *
130 * @param operand the value to add, not null
131 * @since Commons Lang 2.2
132 */
133 public void add(byte operand) {
134 this.value += operand;
135 }
136
137 /**
138 * Adds a value to the value of this instance.
139 *
140 * @param operand the value to add, not null
141 * @throws NullPointerException if the object is null
142 * @since Commons Lang 2.2
143 */
144 public void add(Number operand) {
145 this.value += operand.byteValue();
146 }
147
148 /**
149 * Subtracts a value from the value of this instance.
150 *
151 * @param operand the value to subtract, not null
152 * @since Commons Lang 2.2
153 */
154 public void subtract(byte operand) {
155 this.value -= operand;
156 }
157
158 /**
159 * Subtracts a value from the value of this instance.
160 *
161 * @param operand the value to subtract, not null
162 * @throws NullPointerException if the object is null
163 * @since Commons Lang 2.2
164 */
165 public void subtract(Number operand) {
166 this.value -= operand.byteValue();
167 }
168
169 //-----------------------------------------------------------------------
170 // shortValue relies on Number implementation
171 /**
172 * Returns the value of this MutableByte as a byte.
173 *
174 * @return the numeric value represented by this object after conversion to type byte.
175 */
176 @Override
177 public byte byteValue() {
178 return value;
179 }
180
181 /**
182 * Returns the value of this MutableByte as an int.
183 *
184 * @return the numeric value represented by this object after conversion to type int.
185 */
186 @Override
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 @Override
197 public long longValue() {
198 return value;
199 }
200
201 /**
202 * Returns the value of this MutableByte as a float.
203 *
204 * @return the numeric value represented by this object after conversion to type float.
205 */
206 @Override
207 public float floatValue() {
208 return value;
209 }
210
211 /**
212 * Returns the value of this MutableByte as a double.
213 *
214 * @return the numeric value represented by this object after conversion to type double.
215 */
216 @Override
217 public double doubleValue() {
218 return value;
219 }
220
221 //-----------------------------------------------------------------------
222 /**
223 * Gets this mutable as an instance of Byte.
224 *
225 * @return a Byte instance containing the value from this mutable
226 */
227 public Byte toByte() {
228 return Byte.valueOf(byteValue());
229 }
230
231 //-----------------------------------------------------------------------
232 /**
233 * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
234 * not <code>null</code> and is a <code>MutableByte</code> object that contains the same <code>byte</code> value
235 * as this object.
236 *
237 * @param obj the object to compare with, null returns false
238 * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
239 */
240 @Override
241 public boolean equals(Object obj) {
242 if (obj instanceof MutableByte) {
243 return value == ((MutableByte) obj).byteValue();
244 }
245 return false;
246 }
247
248 /**
249 * Returns a suitable hash code for this mutable.
250 *
251 * @return a suitable hash code
252 */
253 @Override
254 public int hashCode() {
255 return value;
256 }
257
258 //-----------------------------------------------------------------------
259 /**
260 * Compares this mutable to another in ascending order.
261 *
262 * @param other the other mutable to compare to, not null
263 * @return negative if this is less, zero if equal, positive if greater
264 */
265 public int compareTo(MutableByte other) {
266 byte anotherVal = other.value;
267 return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
268 }
269
270 //-----------------------------------------------------------------------
271 /**
272 * Returns the String value of this mutable.
273 *
274 * @return the mutable value as a string
275 */
276 @Override
277 public String toString() {
278 return String.valueOf(value);
279 }
280
281 }