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