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     * 
022     * @see Long
023     * @since 2.1
024     * @version $Id: MutableLong.java 1096472 2011-04-25 13:28:06Z mbenson $
025     */
026    public class MutableLong extends Number implements Comparable<MutableLong>, Mutable<Number> {
027    
028        /**
029         * Required for serialization support.
030         * 
031         * @see java.io.Serializable
032         */
033        private static final long serialVersionUID = 62986528375L;
034    
035        /** The mutable value. */
036        private long value;
037    
038        /**
039         * Constructs a new MutableLong with the default value of zero.
040         */
041        public MutableLong() {
042            super();
043        }
044    
045        /**
046         * Constructs a new MutableLong with the specified value.
047         * 
048         * @param value  the initial value to store
049         */
050        public MutableLong(long value) {
051            super();
052            this.value = value;
053        }
054    
055        /**
056         * Constructs a new MutableLong with the specified value.
057         * 
058         * @param value  the initial value to store, not null
059         * @throws NullPointerException if the object is null
060         */
061        public MutableLong(Number value) {
062            super();
063            this.value = value.longValue();
064        }
065    
066        /**
067         * Constructs a new MutableLong parsing the given string.
068         * 
069         * @param value  the string to parse, not null
070         * @throws NumberFormatException if the string cannot be parsed into a long
071         * @since 2.5
072         */
073        public MutableLong(String value) throws NumberFormatException {
074            super();
075            this.value = Long.parseLong(value);
076        }
077    
078        //-----------------------------------------------------------------------
079        /**
080         * Gets the value as a Long instance.
081         * 
082         * @return the value as a Long, never null
083         */
084        public Long getValue() {
085            return new Long(this.value);
086        }
087    
088        /**
089         * Sets the value.
090         * 
091         * @param value  the value to set
092         */
093        public void setValue(long value) {
094            this.value = value;
095        }
096    
097        /**
098         * Sets the value from any Number instance.
099         * 
100         * @param value  the value to set, not null
101         * @throws NullPointerException if the object is null
102         */
103        public void setValue(Number value) {
104            this.value = value.longValue();
105        }
106    
107        //-----------------------------------------------------------------------
108        /**
109         * Increments the value.
110         *
111         * @since Commons Lang 2.2
112         */
113        public void increment() {
114            value++;
115        }
116    
117        /**
118         * Decrements the value.
119         *
120         * @since Commons Lang 2.2
121         */
122        public void decrement() {
123            value--;
124        }
125    
126        //-----------------------------------------------------------------------
127        /**
128         * Adds a value to the value of this instance.
129         * 
130         * @param operand  the value to add, not null
131         * @since Commons Lang 2.2
132         */
133        public void add(long operand) {
134            this.value += operand;
135        }
136    
137        /**
138         * Adds a value to the value of this instance.
139         * 
140         * @param operand  the value to add, not null
141         * @throws NullPointerException if the object is null
142         * @since Commons Lang 2.2
143         */
144        public void add(Number operand) {
145            this.value += operand.longValue();
146        }
147    
148        /**
149         * Subtracts a value from the value of this instance.
150         * 
151         * @param operand  the value to subtract, not null
152         * @since Commons Lang 2.2
153         */
154        public void subtract(long operand) {
155            this.value -= operand;
156        }
157    
158        /**
159         * Subtracts a value from the value of this instance.
160         * 
161         * @param operand  the value to subtract, not null
162         * @throws NullPointerException if the object is null
163         * @since Commons Lang 2.2
164         */
165        public void subtract(Number operand) {
166            this.value -= operand.longValue();
167        }
168    
169        //-----------------------------------------------------------------------
170        // shortValue and byteValue rely on Number implementation
171        /**
172         * Returns the value of this MutableLong as an int.
173         *
174         * @return the numeric value represented by this object after conversion to type int.
175         */
176        @Override
177        public int intValue() {
178            return (int) value;
179        }
180    
181        /**
182         * Returns the value of this MutableLong as a long.
183         *
184         * @return the numeric value represented by this object after conversion to type long.
185         */
186        @Override
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        @Override
197        public float floatValue() {
198            return value;
199        }
200    
201        /**
202         * Returns the value of this MutableLong as a double.
203         *
204         * @return the numeric value represented by this object after conversion to type double.
205         */
206        @Override
207        public double doubleValue() {
208            return value;
209        }
210    
211        //-----------------------------------------------------------------------
212        /**
213         * Gets this mutable as an instance of Long.
214         *
215         * @return a Long instance containing the value from this mutable, never null
216         */
217        public Long toLong() {
218            return Long.valueOf(longValue());
219        }
220    
221        //-----------------------------------------------------------------------
222        /**
223         * Compares this object to the specified object. The result is <code>true</code> if and only if the argument
224         * is not <code>null</code> and is a <code>MutableLong</code> object that contains the same <code>long</code>
225         * value as this object.
226         * 
227         * @param obj  the object to compare with, null returns false
228         * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
229         */
230        @Override
231        public boolean equals(Object obj) {
232            if (obj instanceof MutableLong) {
233                return value == ((MutableLong) obj).longValue();
234            }
235            return false;
236        }
237    
238        /**
239         * Returns a suitable hash code for this mutable.
240         * 
241         * @return a suitable hash code
242         */
243        @Override
244        public int hashCode() {
245            return (int) (value ^ (value >>> 32));
246        }
247    
248        //-----------------------------------------------------------------------
249        /**
250         * Compares this mutable to another in ascending order.
251         * 
252         * @param other  the other mutable to compare to, not null
253         * @return negative if this is less, zero if equal, positive if greater
254         */
255        public int compareTo(MutableLong other) {
256            long anotherVal = other.value;
257            return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
258        }
259    
260        //-----------------------------------------------------------------------
261        /**
262         * Returns the String value of this mutable.
263         * 
264         * @return the mutable value as a string
265         */
266        @Override
267        public String toString() {
268            return String.valueOf(value);
269        }
270    
271    }