001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     * 
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     * 
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    package org.apache.commons.lang.mutable;
018    
019    /**
020     * A mutable <code>long</code> wrapper.
021     * 
022     * @see Long
023     * @since 2.1
024     * @author Apache Software Foundation
025     * @version $Id: MutableLong.java 905707 2010-02-02 16:59:59Z niallp $
026     */
027    public class MutableLong extends Number implements Comparable, Mutable {
028    
029        /**
030         * Required for serialization support.
031         * 
032         * @see java.io.Serializable
033         */
034        private static final long serialVersionUID = 62986528375L;
035    
036        /** The mutable value. */
037        private long value;
038    
039        /**
040         * Constructs a new MutableLong with the default value of zero.
041         */
042        public MutableLong() {
043            super();
044        }
045    
046        /**
047         * Constructs a new MutableLong with the specified value.
048         * 
049         * @param value  the initial value to store
050         */
051        public MutableLong(long value) {
052            super();
053            this.value = value;
054        }
055    
056        /**
057         * Constructs a new MutableLong with the specified value.
058         * 
059         * @param value  the initial value to store, not null
060         * @throws NullPointerException if the object is null
061         */
062        public MutableLong(Number value) {
063            super();
064            this.value = value.longValue();
065        }
066    
067        /**
068         * Constructs a new MutableLong parsing the given string.
069         * 
070         * @param value  the string to parse, not null
071         * @throws NumberFormatException if the string cannot be parsed into a long
072         * @since 2.5
073         */
074        public MutableLong(String value) throws NumberFormatException {
075            super();
076            this.value = Long.parseLong(value);
077        }
078    
079        //-----------------------------------------------------------------------
080        /**
081         * Gets the value as a Long instance.
082         * 
083         * @return the value as a Long, never null
084         */
085        public Object getValue() {
086            return new Long(this.value);
087        }
088    
089        /**
090         * Sets the value.
091         * 
092         * @param value  the value to set
093         */
094        public void setValue(long value) {
095            this.value = value;
096        }
097    
098        /**
099         * Sets the value from any Number instance.
100         * 
101         * @param value  the value to set, not null
102         * @throws NullPointerException if the object is null
103         * @throws ClassCastException if the type is not a {@link Number}
104         */
105        public void setValue(Object value) {
106            setValue(((Number) value).longValue());
107        }
108    
109        //-----------------------------------------------------------------------
110        /**
111         * Increments the value.
112         *
113         * @since Commons Lang 2.2
114         */
115        public void increment() {
116            value++;
117        }
118    
119        /**
120         * Decrements the value.
121         *
122         * @since Commons Lang 2.2
123         */
124        public void decrement() {
125            value--;
126        }
127    
128        //-----------------------------------------------------------------------
129        /**
130         * Adds a value to the value of this instance.
131         * 
132         * @param operand  the value to add, not null
133         * @since Commons Lang 2.2
134         */
135        public void add(long operand) {
136            this.value += operand;
137        }
138    
139        /**
140         * Adds a value to the value of this instance.
141         * 
142         * @param operand  the value to add, not null
143         * @throws NullPointerException if the object is null
144         * @since Commons Lang 2.2
145         */
146        public void add(Number operand) {
147            this.value += operand.longValue();
148        }
149    
150        /**
151         * Subtracts a value from the value of this instance.
152         * 
153         * @param operand  the value to subtract, not null
154         * @since Commons Lang 2.2
155         */
156        public void subtract(long operand) {
157            this.value -= operand;
158        }
159    
160        /**
161         * Subtracts a value from the value of this instance.
162         * 
163         * @param operand  the value to subtract, not null
164         * @throws NullPointerException if the object is null
165         * @since Commons Lang 2.2
166         */
167        public void subtract(Number operand) {
168            this.value -= operand.longValue();
169        }
170    
171        //-----------------------------------------------------------------------
172        // shortValue and bytValue rely on Number implementation
173        /**
174         * Returns the value of this MutableLong as an int.
175         *
176         * @return the numeric value represented by this object after conversion to type int.
177         */
178        public int intValue() {
179            return (int) value;
180        }
181    
182        /**
183         * Returns the value of this MutableLong as a long.
184         *
185         * @return the numeric value represented by this object after conversion to type long.
186         */
187        public long longValue() {
188            return value;
189        }
190    
191        /**
192         * Returns the value of this MutableLong as a float.
193         *
194         * @return the numeric value represented by this object after conversion to type float.
195         */
196        public float floatValue() {
197            return value;
198        }
199    
200        /**
201         * Returns the value of this MutableLong as a double.
202         *
203         * @return the numeric value represented by this object after conversion to type double.
204         */
205        public double doubleValue() {
206            return value;
207        }
208    
209        //-----------------------------------------------------------------------
210        /**
211         * Gets this mutable as an instance of Long.
212         *
213         * @return a Long instance containing the value from this mutable, never null
214         */
215        public Long toLong() {
216            return new Long(longValue());
217        }
218    
219        //-----------------------------------------------------------------------
220        /**
221         * Compares this object to the specified object. The result is <code>true</code> if and only if the argument
222         * is not <code>null</code> and is a <code>MutableLong</code> object that contains the same <code>long</code>
223         * value as this object.
224         * 
225         * @param obj  the object to compare with, null returns false
226         * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
227         */
228        public boolean equals(Object obj) {
229            if (obj instanceof MutableLong) {
230                return value == ((MutableLong) obj).longValue();
231            }
232            return false;
233        }
234    
235        /**
236         * Returns a suitable hash code for this mutable.
237         * 
238         * @return a suitable hash code
239         */
240        public int hashCode() {
241            return (int) (value ^ (value >>> 32));
242        }
243    
244        //-----------------------------------------------------------------------
245        /**
246         * Compares this mutable to another in ascending order.
247         * 
248         * @param obj the other mutable to compare to, not null
249         * @return negative if this is less, zero if equal, positive if greater
250         * @throws ClassCastException if the argument is not a MutableLong
251         */
252        public int compareTo(Object obj) {
253            MutableLong other = (MutableLong) obj;
254            long anotherVal = other.value;
255            return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
256        }
257    
258        //-----------------------------------------------------------------------
259        /**
260         * Returns the String value of this mutable.
261         * 
262         * @return the mutable value as a string
263         */
264        public String toString() {
265            return String.valueOf(value);
266        }
267    
268    }