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 */
017package 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 1436770 2013-01-22 07:09:45Z ggregory $
027 */
028public 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(final 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(final 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(final 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    @Override
087    public Long getValue() {
088        return Long.valueOf(this.value);
089    }
090
091    /**
092     * Sets the value.
093     * 
094     * @param value  the value to set
095     */
096    public void setValue(final long value) {
097        this.value = value;
098    }
099
100    /**
101     * Sets the value from any Number instance.
102     * 
103     * @param value  the value to set, not null
104     * @throws NullPointerException if the object is null
105     */
106    @Override
107    public void setValue(final Number value) {
108        this.value = value.longValue();
109    }
110
111    //-----------------------------------------------------------------------
112    /**
113     * Increments the value.
114     *
115     * @since Commons Lang 2.2
116     */
117    public void increment() {
118        value++;
119    }
120
121    /**
122     * Decrements the value.
123     *
124     * @since Commons Lang 2.2
125     */
126    public void decrement() {
127        value--;
128    }
129
130    //-----------------------------------------------------------------------
131    /**
132     * Adds a value to the value of this instance.
133     * 
134     * @param operand  the value to add, not null
135     * @since Commons Lang 2.2
136     */
137    public void add(final long operand) {
138        this.value += operand;
139    }
140
141    /**
142     * Adds a value to the value of this instance.
143     * 
144     * @param operand  the value to add, not null
145     * @throws NullPointerException if the object is null
146     * @since Commons Lang 2.2
147     */
148    public void add(final Number operand) {
149        this.value += operand.longValue();
150    }
151
152    /**
153     * Subtracts a value from the value of this instance.
154     * 
155     * @param operand  the value to subtract, not null
156     * @since Commons Lang 2.2
157     */
158    public void subtract(final long operand) {
159        this.value -= operand;
160    }
161
162    /**
163     * Subtracts a value from the value of this instance.
164     * 
165     * @param operand  the value to subtract, not null
166     * @throws NullPointerException if the object is null
167     * @since Commons Lang 2.2
168     */
169    public void subtract(final Number operand) {
170        this.value -= operand.longValue();
171    }
172
173    //-----------------------------------------------------------------------
174    // shortValue and byteValue rely on Number implementation
175    /**
176     * Returns the value of this MutableLong as an int.
177     *
178     * @return the numeric value represented by this object after conversion to type int.
179     */
180    @Override
181    public int intValue() {
182        return (int) value;
183    }
184
185    /**
186     * Returns the value of this MutableLong as a long.
187     *
188     * @return the numeric value represented by this object after conversion to type long.
189     */
190    @Override
191    public long longValue() {
192        return value;
193    }
194
195    /**
196     * Returns the value of this MutableLong as a float.
197     *
198     * @return the numeric value represented by this object after conversion to type float.
199     */
200    @Override
201    public float floatValue() {
202        return value;
203    }
204
205    /**
206     * Returns the value of this MutableLong as a double.
207     *
208     * @return the numeric value represented by this object after conversion to type double.
209     */
210    @Override
211    public double doubleValue() {
212        return value;
213    }
214
215    //-----------------------------------------------------------------------
216    /**
217     * Gets this mutable as an instance of Long.
218     *
219     * @return a Long instance containing the value from this mutable, never null
220     */
221    public Long toLong() {
222        return Long.valueOf(longValue());
223    }
224
225    //-----------------------------------------------------------------------
226    /**
227     * Compares this object to the specified object. The result is <code>true</code> if and only if the argument
228     * is not <code>null</code> and is a <code>MutableLong</code> object that contains the same <code>long</code>
229     * value as this object.
230     * 
231     * @param obj  the object to compare with, null returns false
232     * @return <code>true</code> if the objects are the same; <code>false</code> otherwise.
233     */
234    @Override
235    public boolean equals(final Object obj) {
236        if (obj instanceof MutableLong) {
237            return value == ((MutableLong) obj).longValue();
238        }
239        return false;
240    }
241
242    /**
243     * Returns a suitable hash code for this mutable.
244     * 
245     * @return a suitable hash code
246     */
247    @Override
248    public int hashCode() {
249        return (int) (value ^ (value >>> 32));
250    }
251
252    //-----------------------------------------------------------------------
253    /**
254     * Compares this mutable to another in ascending order.
255     * 
256     * @param other  the other mutable to compare to, not null
257     * @return negative if this is less, zero if equal, positive if greater
258     */
259    @Override
260    public int compareTo(final MutableLong other) {
261        final long anotherVal = other.value;
262        return value < anotherVal ? -1 : (value == anotherVal ? 0 : 1);
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}