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