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>short</code> wrapper.
23   * <p>
24   * Note that as MutableShort does not extend Short, it is not treated by String.format as a Short parameter. 
25   * 
26   * @see Short
27   * @since 2.1
28   */
29  public class MutableShort extends Number implements Comparable<MutableShort>, Mutable<Number> {
30  
31      /**
32       * Required for serialization support.
33       * 
34       * @see java.io.Serializable
35       */
36      private static final long serialVersionUID = -2135791679L;
37  
38      /** The mutable value. */
39      private short value;
40  
41      /**
42       * Constructs a new MutableShort with the default value of zero.
43       */
44      public MutableShort() {
45          super();
46      }
47  
48      /**
49       * Constructs a new MutableShort with the specified value.
50       * 
51       * @param value  the initial value to store
52       */
53      public MutableShort(final short value) {
54          super();
55          this.value = value;
56      }
57  
58      /**
59       * Constructs a new MutableShort with the specified value.
60       * 
61       * @param value  the initial value to store, not null
62       * @throws NullPointerException if the object is null
63       */
64      public MutableShort(final Number value) {
65          super();
66          this.value = value.shortValue();
67      }
68  
69      /**
70       * Constructs a new MutableShort parsing the given string.
71       * 
72       * @param value  the string to parse, not null
73       * @throws NumberFormatException if the string cannot be parsed into a short
74       * @since 2.5
75       */
76      public MutableShort(final String value) throws NumberFormatException {
77          super();
78          this.value = Short.parseShort(value);
79      }
80  
81      //-----------------------------------------------------------------------
82      /**
83       * Gets the value as a Short instance.
84       * 
85       * @return the value as a Short, never null
86       */
87      @Override
88      public Short getValue() {
89          return Short.valueOf(this.value);
90      }
91  
92      /**
93       * Sets the value.
94       * 
95       * @param value  the value to set
96       */
97      public void setValue(final short value) {
98          this.value = value;
99      }
100 
101     /**
102      * Sets the value from any Number instance.
103      * 
104      * @param value  the value to set, not null
105      * @throws NullPointerException if the object is null
106      */
107     @Override
108     public void setValue(final Number value) {
109         this.value = value.shortValue();
110     }
111 
112     //-----------------------------------------------------------------------
113     /**
114      * Increments the value.
115      *
116      * @since Commons Lang 2.2
117      */
118     public void increment() {
119         value++;
120     }
121 
122     /**
123      * Increments this instance's value by 1; this method returns the value associated with the instance
124      * immediately prior to the increment operation. This method is not thread safe.
125      *
126      * @return the value associated with the instance before it was incremented
127      * @since 3.5
128      */
129     public short getAndIncrement() {
130         short last = value;
131         value++;
132         return last;
133     }
134 
135     /**
136      * Increments this instance's value by 1; this method returns the value associated with the instance
137      * immediately after the increment operation. This method is not thread safe.
138      *
139      * @return the value associated with the instance after it is incremented
140      * @since 3.5
141      */
142     public short incrementAndGet() {
143         value++;
144         return value;
145     }
146 
147     /**
148      * Decrements the value.
149      *
150      * @since Commons Lang 2.2
151      */
152     public void decrement() {
153         value--;
154     }
155 
156     /**
157      * Decrements this instance's value by 1; this method returns the value associated with the instance
158      * immediately prior to the decrement operation. This method is not thread safe.
159      *
160      * @return the value associated with the instance before it was decremented
161      * @since 3.5
162      */
163     public short getAndDecrement() {
164         short last = value;
165         value--;
166         return last;
167     }
168 
169     /**
170      * Decrements this instance's value by 1; this method returns the value associated with the instance
171      * immediately after the decrement operation. This method is not thread safe.
172      *
173      * @return the value associated with the instance after it is decremented
174      * @since 3.5
175      */
176     public short decrementAndGet() {
177         value--;
178         return value;
179     }
180 
181     //-----------------------------------------------------------------------
182     /**
183      * Adds a value to the value of this instance.
184      * 
185      * @param operand  the value to add, not null
186      * @since Commons Lang 2.2
187      */
188     public void add(final short operand) {
189         this.value += operand;
190     }
191 
192     /**
193      * Adds a value to the value of this instance.
194      * 
195      * @param operand  the value to add, not null
196      * @throws NullPointerException if the object is null
197      * @since Commons Lang 2.2
198      */
199     public void add(final Number operand) {
200         this.value += operand.shortValue();
201     }
202 
203     /**
204      * Subtracts a value from the value of this instance.
205      * 
206      * @param operand  the value to subtract, not null
207      * @since Commons Lang 2.2
208      */
209     public void subtract(final short operand) {
210         this.value -= operand;
211     }
212 
213     /**
214      * Subtracts a value from the value of this instance.
215      * 
216      * @param operand  the value to subtract, not null
217      * @throws NullPointerException if the object is null
218      * @since Commons Lang 2.2
219      */
220     public void subtract(final Number operand) {
221         this.value -= operand.shortValue();
222     }
223 
224     /**
225      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
226      * immediately after the addition operation. This method is not thread safe.
227      *
228      * @param operand the quantity to add, not null
229      * @return the value associated with this instance after adding the operand
230      */
231     public short addAndGet(final short operand) {
232         this.value += operand;
233         return value;
234     }
235 
236     /**
237      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
238      * immediately after the addition operation. This method is not thread safe.
239      *
240      * @param operand the quantity to add, not null
241      * @throws NullPointerException if {@code operand} is null
242      * @return the value associated with this instance after adding the operand
243      */
244     public short addAndGet(final Number operand) {
245         this.value += operand.shortValue();
246         return value;
247     }
248 
249     /**
250      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
251      * immediately prior to the addition operation. This method is not thread safe.
252      *
253      * @param operand the quantity to add, not null
254      * @return the value associated with this instance immediately before the operand was added
255      */
256     public short getAndAdd(final short operand) {
257         short last = value;
258         this.value += operand;
259         return last;
260     }
261 
262     /**
263      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
264      * immediately prior to the addition operation. This method is not thread safe.
265      *
266      * @param operand the quantity to add, not null
267      * @throws NullPointerException if {@code operand} is null
268      * @return the value associated with this instance immediately before the operand was added
269      */
270     public short getAndAdd(final Number operand) {
271         short last = value;
272         this.value += operand.shortValue();
273         return last;
274     }
275 
276     //-----------------------------------------------------------------------
277     // byteValue relies on Number implementation
278     /**
279      * Returns the value of this MutableShort as a short.
280      *
281      * @return the numeric value represented by this object after conversion to type short.
282      */
283     @Override
284     public short shortValue() {
285         return value;
286     }
287 
288     /**
289      * Returns the value of this MutableShort as an int.
290      *
291      * @return the numeric value represented by this object after conversion to type int.
292      */
293     @Override
294     public int intValue() {
295         return value;
296     }
297 
298     /**
299      * Returns the value of this MutableShort as a long.
300      *
301      * @return the numeric value represented by this object after conversion to type long.
302      */
303     @Override
304     public long longValue() {
305         return value;
306     }
307 
308     /**
309      * Returns the value of this MutableShort as a float.
310      *
311      * @return the numeric value represented by this object after conversion to type float.
312      */
313     @Override
314     public float floatValue() {
315         return value;
316     }
317 
318     /**
319      * Returns the value of this MutableShort as a double.
320      *
321      * @return the numeric value represented by this object after conversion to type double.
322      */
323     @Override
324     public double doubleValue() {
325         return value;
326     }
327 
328     //-----------------------------------------------------------------------
329     /**
330      * Gets this mutable as an instance of Short.
331      *
332      * @return a Short instance containing the value from this mutable, never null
333      */
334     public Short toShort() {
335         return Short.valueOf(shortValue());
336     }
337 
338     //-----------------------------------------------------------------------
339     /**
340      * Compares this object to the specified object. The result is <code>true</code> if and only if the argument
341      * is not <code>null</code> and is a <code>MutableShort</code> object that contains the same <code>short</code>
342      * value as this object.
343      * 
344      * @param obj  the object to compare with, null returns false
345      * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
346      */
347     @Override
348     public boolean equals(final Object obj) {
349         if (obj instanceof MutableShort) {
350             return value == ((MutableShort) obj).shortValue();
351         }
352         return false;
353     }
354 
355     /**
356      * Returns a suitable hash code for this mutable.
357      * 
358      * @return a suitable hash code
359      */
360     @Override
361     public int hashCode() {
362         return value;
363     }
364 
365     //-----------------------------------------------------------------------
366     /**
367      * Compares this mutable to another in ascending order.
368      * 
369      * @param other  the other mutable to compare to, not null
370      * @return negative if this is less, zero if equal, positive if greater
371      */
372     @Override
373     public int compareTo(final MutableShort other) {
374         return NumberUtils.compare(this.value, other.value);
375     }
376 
377     //-----------------------------------------------------------------------
378     /**
379      * Returns the String value of this mutable.
380      * 
381      * @return the mutable value as a string
382      */
383     @Override
384     public String toString() {
385         return String.valueOf(value);
386     }
387 
388 }