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