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.lang3.mutable;
018    
019    /**
020     * A mutable <code>long</code> wrapper.
021     * <p>
022     * Note that as MutableLong does not extend Long, it is not treated by String.format as a Long parameter. 
023     * 
024     * @see Long
025     * @since 2.1
026     * @version $Id: MutableLong.java 1160571 2011-08-23 07:36:08Z bayard $
027     */
028    public class MutableLong extends Number implements Comparable<MutableLong>, Mutable<Number> {
029    
030        /**
031         * Required for serialization support.
032         * 
033         * @see java.io.Serializable
034         */
035        private static final long serialVersionUID = 62986528375L;
036    
037        /** The mutable value. */
038        private long value;
039    
040        /**
041         * Constructs a new MutableLong with the default value of zero.
042         */
043        public MutableLong() {
044            super();
045        }
046    
047        /**
048         * Constructs a new MutableLong with the specified value.
049         * 
050         * @param value  the initial value to store
051         */
052        public MutableLong(long value) {
053            super();
054            this.value = value;
055        }
056    
057        /**
058         * Constructs a new MutableLong with the specified value.
059         * 
060         * @param value  the initial value to store, not null
061         * @throws NullPointerException if the object is null
062         */
063        public MutableLong(Number value) {
064            super();
065            this.value = value.longValue();
066        }
067    
068        /**
069         * Constructs a new MutableLong parsing the given string.
070         * 
071         * @param value  the string to parse, not null
072         * @throws NumberFormatException if the string cannot be parsed into a long
073         * @since 2.5
074         */
075        public MutableLong(String value) throws NumberFormatException {
076            super();
077            this.value = Long.parseLong(value);
078        }
079    
080        //-----------------------------------------------------------------------
081        /**
082         * Gets the value as a Long instance.
083         * 
084         * @return the value as a Long, never null
085         */
086        public Long getValue() {
087            return Long.valueOf(this.value);
088        }
089    
090        /**
091         * Sets the value.
092         * 
093         * @param value  the value to set
094         */
095        public void setValue(long value) {
096            this.value = value;
097        }
098    
099        /**
100         * Sets the value from any Number instance.
101         * 
102         * @param value  the value to set, not null
103         * @throws NullPointerException if the object is null
104         */
105        public void setValue(Number value) {
106            this.value = 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 byteValue 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        @Override
179        public int intValue() {
180            return (int) value;
181        }
182    
183        /**
184         * Returns the value of this MutableLong as a long.
185         *
186         * @return the numeric value represented by this object after conversion to type long.
187         */
188        @Override
189        public long longValue() {
190            return value;
191        }
192    
193        /**
194         * Returns the value of this MutableLong as a float.
195         *
196         * @return the numeric value represented by this object after conversion to type float.
197         */
198        @Override
199        public float floatValue() {
200            return value;
201        }
202    
203        /**
204         * Returns the value of this MutableLong as a double.
205         *
206         * @return the numeric value represented by this object after conversion to type double.
207         */
208        @Override
209        public double doubleValue() {
210            return value;
211        }
212    
213        //-----------------------------------------------------------------------
214        /**
215         * Gets this mutable as an instance of Long.
216         *
217         * @return a Long instance containing the value from this mutable, never null
218         */
219        public Long toLong() {
220            return Long.valueOf(longValue());
221        }
222    
223        //-----------------------------------------------------------------------
224        /**
225         * Compares this object to the specified object. The result is <code>true</code> if and only if the argument
226         * is not <code>null</code> and is a <code>MutableLong</code> object that contains the same <code>long</code>
227         * value as this object.
228         * 
229         * @param obj  the object to compare with, null returns false
230         * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
231         */
232        @Override
233        public boolean equals(Object obj) {
234            if (obj instanceof MutableLong) {
235                return value == ((MutableLong) obj).longValue();
236            }
237            return false;
238        }
239    
240        /**
241         * Returns a suitable hash code for this mutable.
242         * 
243         * @return a suitable hash code
244         */
245        @Override
246        public int hashCode() {
247            return (int) (value ^ (value >>> 32));
248        }
249    
250        //-----------------------------------------------------------------------
251        /**
252         * Compares this mutable to another in ascending order.
253         * 
254         * @param other  the other mutable to compare to, not null
255         * @return negative if this is less, zero if equal, positive if greater
256         */
257        public int compareTo(MutableLong other) {
258            long anotherVal = other.value;
259            return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
260        }
261    
262        //-----------------------------------------------------------------------
263        /**
264         * Returns the String value of this mutable.
265         * 
266         * @return the mutable value as a string
267         */
268        @Override
269        public String toString() {
270            return String.valueOf(value);
271        }
272    
273    }