View Javadoc
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 org.apache.commons.lang3.mutable;
18  
19  import org.apache.commons.lang3.math.NumberUtils;
20  
21  /**
22   * A mutable <code>byte</code> wrapper.
23   * <p>
24   * Note that as MutableByte does not extend Byte, it is not treated by String.format as a Byte parameter. 
25   * 
26   * @see Byte
27   * @since 2.1
28   * @version $Id: MutableByte.java 1669791 2015-03-28 15:22:59Z britter $
29   */
30  public class MutableByte extends Number implements Comparable<MutableByte>, Mutable<Number> {
31  
32      /**
33       * Required for serialization support.
34       * 
35       * @see java.io.Serializable
36       */
37      private static final long serialVersionUID = -1585823265L;
38  
39      /** The mutable value. */
40      private byte value;
41  
42      /**
43       * Constructs a new MutableByte with the default value of zero.
44       */
45      public MutableByte() {
46          super();
47      }
48  
49      /**
50       * Constructs a new MutableByte with the specified value.
51       * 
52       * @param value  the initial value to store
53       */
54      public MutableByte(final byte value) {
55          super();
56          this.value = value;
57      }
58  
59      /**
60       * Constructs a new MutableByte with the specified value.
61       * 
62       * @param value  the initial value to store, not null
63       * @throws NullPointerException if the object is null
64       */
65      public MutableByte(final Number value) {
66          super();
67          this.value = value.byteValue();
68      }
69  
70      /**
71       * Constructs a new MutableByte parsing the given string.
72       * 
73       * @param value  the string to parse, not null
74       * @throws NumberFormatException if the string cannot be parsed into a byte
75       * @since 2.5
76       */
77      public MutableByte(final String value) throws NumberFormatException {
78          super();
79          this.value = Byte.parseByte(value);
80      }
81  
82      //-----------------------------------------------------------------------
83      /**
84       * Gets the value as a Byte instance.
85       * 
86       * @return the value as a Byte, never null
87       */
88      @Override
89      public Byte getValue() {
90          return Byte.valueOf(this.value);
91      }
92  
93      /**
94       * Sets the value.
95       * 
96       * @param value  the value to set
97       */
98      public void setValue(final byte value) {
99          this.value = value;
100     }
101 
102     /**
103      * Sets the value from any Number instance.
104      * 
105      * @param value  the value to set, not null
106      * @throws NullPointerException if the object is null
107      */
108     @Override
109     public void setValue(final Number value) {
110         this.value = value.byteValue();
111     }
112 
113     //-----------------------------------------------------------------------
114     /**
115      * Increments the value.
116      *
117      * @since Commons Lang 2.2
118      */
119     public void increment() {
120         value++;
121     }
122 
123     /**
124      * Decrements the value.
125      *
126      * @since Commons Lang 2.2
127      */
128     public void decrement() {
129         value--;
130     }
131 
132     //-----------------------------------------------------------------------
133     /**
134      * Adds a value to the value of this instance.
135      * 
136      * @param operand  the value to add, not null
137      * @since Commons Lang 2.2
138      */
139     public void add(final byte operand) {
140         this.value += operand;
141     }
142 
143     /**
144      * Adds a value to the value of this instance.
145      * 
146      * @param operand  the value to add, not null
147      * @throws NullPointerException if the object is null
148      * @since Commons Lang 2.2
149      */
150     public void add(final Number operand) {
151         this.value += operand.byteValue();
152     }
153 
154     /**
155      * Subtracts a value from the value of this instance.
156      * 
157      * @param operand  the value to subtract, not null
158      * @since Commons Lang 2.2
159      */
160     public void subtract(final byte operand) {
161         this.value -= operand;
162     }
163 
164     /**
165      * Subtracts a value from the value of this instance.
166      * 
167      * @param operand  the value to subtract, not null
168      * @throws NullPointerException if the object is null
169      * @since Commons Lang 2.2
170      */
171     public void subtract(final Number operand) {
172         this.value -= operand.byteValue();
173     }
174 
175     //-----------------------------------------------------------------------
176     // shortValue relies on Number implementation
177     /**
178      * Returns the value of this MutableByte as a byte.
179      *
180      * @return the numeric value represented by this object after conversion to type byte.
181      */
182     @Override
183     public byte byteValue() {
184         return value;
185     }
186 
187     /**
188      * Returns the value of this MutableByte as an int.
189      *
190      * @return the numeric value represented by this object after conversion to type int.
191      */
192     @Override
193     public int intValue() {
194         return value;
195     }
196 
197     /**
198      * Returns the value of this MutableByte as a long.
199      *
200      * @return the numeric value represented by this object after conversion to type long.
201      */
202     @Override
203     public long longValue() {
204         return value;
205     }
206 
207     /**
208      * Returns the value of this MutableByte as a float.
209      *
210      * @return the numeric value represented by this object after conversion to type float.
211      */
212     @Override
213     public float floatValue() {
214         return value;
215     }
216 
217     /**
218      * Returns the value of this MutableByte as a double.
219      *
220      * @return the numeric value represented by this object after conversion to type double.
221      */
222     @Override
223     public double doubleValue() {
224         return value;
225     }
226 
227     //-----------------------------------------------------------------------
228     /**
229      * Gets this mutable as an instance of Byte.
230      *
231      * @return a Byte instance containing the value from this mutable
232      */
233     public Byte toByte() {
234         return Byte.valueOf(byteValue());
235     }
236 
237     //-----------------------------------------------------------------------
238     /**
239      * Compares this object to the specified object. The result is <code>true</code> if and only if the argument is
240      * not <code>null</code> and is a <code>MutableByte</code> object that contains the same <code>byte</code> value
241      * as this object.
242      * 
243      * @param obj  the object to compare with, null returns false
244      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
245      */
246     @Override
247     public boolean equals(final Object obj) {
248         if (obj instanceof MutableByte) {
249             return value == ((MutableByte) obj).byteValue();
250         }
251         return false;
252     }
253 
254     /**
255      * Returns a suitable hash code for this mutable.
256      * 
257      * @return a suitable hash code
258      */
259     @Override
260     public int hashCode() {
261         return value;
262     }
263 
264     //-----------------------------------------------------------------------
265     /**
266      * Compares this mutable to another in ascending order.
267      * 
268      * @param other  the other mutable to compare to, not null
269      * @return negative if this is less, zero if equal, positive if greater
270      */
271     @Override
272     public int compareTo(final MutableByte other) {
273         return NumberUtils.compare(this.value, other.value);
274     }
275 
276     //-----------------------------------------------------------------------
277     /**
278      * Returns the String value of this mutable.
279      * 
280      * @return the mutable value as a string
281      */
282     @Override
283     public String toString() {
284         return String.valueOf(value);
285     }
286 
287 }