    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 *      https://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
    019import org.apache.commons.lang3.math.NumberUtils;
    020
    021/**
    022 * A mutable {@code short} wrapper.
    023 * <p>
    024 * Note that as MutableShort does not extend Short, it is not treated by String.format as a Short parameter.
    025 * </p>
    026 *
    027 * @see Short
    028 * @since 2.1
    029 */
    030public class MutableShort extends Number implements Comparable<MutableShort>, Mutable<Number> {
    031
    032    /**
    033     * Required for serialization support.
    034     *
    035     * @see java.io.Serializable
    036     */
    037    private static final long serialVersionUID = -2135791679L;
    038
    039    /** The mutable value. */
    040    private short value;
    041
    042    /**
    043     * Constructs a new MutableShort with the default value of zero.
    044     */
    045    public MutableShort() {
    046    }
    047
    048    /**
    049     * Constructs a new MutableShort with the specified value.
    050     *
    051     * @param value  the initial value to store, not null
    052     * @throws NullPointerException if the object is null
    053     */
    054    public MutableShort(final Number value) {
    055        this.value = value.shortValue();
    056    }
    057
    058    /**
    059     * Constructs a new MutableShort with the specified value.
    060     *
    061     * @param value  the initial value to store
    062     */
    063    public MutableShort(final short value) {
    064        this.value = value;
    065    }
    066
    067    /**
    068     * Constructs a new MutableShort 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 short, see {@link Short#parseShort(String)}.
    072     * @since 2.5
    073     */
    074    public MutableShort(final String value) {
    075        this.value = Short.parseShort(value);
    076    }
    077
    078    /**
    079     * Adds a value to the value of this instance.
    080     *
    081     * @param operand  the value to add, not null
    082     * @throws NullPointerException if the object is null
    083     * @since 2.2
    084     */
    085    public void add(final Number operand) {
    086        this.value += operand.shortValue();
    087    }
    088
    089    /**
    090     * Adds a value to the value of this instance.
    091     *
    092     * @param operand  the value to add, not null
    093     * @since 2.2
    094     */
    095    public void add(final short operand) {
    096        this.value += operand;
    097    }
    098
    099    /**
    100     * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
    101     * immediately after the addition operation. This method is not thread safe.
    102     *
    103     * @param operand the quantity to add, not null
    104     * @throws NullPointerException if {@code operand} is null
    105     * @return the value associated with this instance after adding the operand
    106     * @since 3.5
    107     */
    108    public short addAndGet(final Number operand) {
    109        this.value += operand.shortValue();
    110        return value;
    111    }
    112
    113    /**
    114     * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
    115     * immediately after the addition operation. This method is not thread safe.
    116     *
    117     * @param operand the quantity to add, not null
    118     * @return the value associated with this instance after adding the operand
    119     * @since 3.5
    120     */
    121    public short addAndGet(final short operand) {
    122        this.value += operand;
    123        return value;
    124    }
    125
    126    /**
    127     * Compares this mutable to another in ascending order.
    128     *
    129     * @param other  the other mutable to compare to, not null
    130     * @return negative if this is less, zero if equal, positive if greater
    131     */
    132    @Override
    133    public int compareTo(final MutableShort other) {
    134        return NumberUtils.compare(this.value, other.value);
    135    }
    136
    137    /**
    138     * Decrements the value.
    139     *
    140     * @since 2.2
    141     */
    142    public void decrement() {
    143        value--;
    144    }
    145
    146    /**
    147     * Decrements this instance's value by 1; this method returns the value associated with the instance
    148     * immediately after the decrement operation. This method is not thread safe.
    149     *
    150     * @return the value associated with the instance after it is decremented
    151     * @since 3.5
    152     */
    153    public short decrementAndGet() {
    154        value--;
    155        return value;
    156    }
    157
    158    /**
    159     * Returns the value of this MutableShort as a double.
    160     *
    161     * @return the numeric value represented by this object after conversion to type double.
    162     */
    163    @Override
    164    public double doubleValue() {
    165        return value;
    166    }
    167
    168    /**
    169     * Compares this object to the specified object. The result is {@code true} if and only if the argument
    170     * is not {@code null} and is a {@link MutableShort} object that contains the same {@code short}
    171     * value as this object.
    172     *
    173     * @param obj  the object to compare with, null returns false
    174     * @return {@code true} if the objects are the same; {@code false} otherwise.
    175     */
    176    @Override
    177    public boolean equals(final Object obj) {
    178        if (obj instanceof MutableShort) {
    179            return value == ((MutableShort) obj).shortValue();
    180        }
    181        return false;
    182    }
    183
    184    /**
    185     * Returns the value of this MutableShort as a float.
    186     *
    187     * @return the numeric value represented by this object after conversion to type float.
    188     */
    189    @Override
    190    public float floatValue() {
    191        return value;
    192    }
    193
    194    /**
    195     * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
    196     * immediately prior to the addition operation. This method is not thread safe.
    197     *
    198     * @param operand the quantity to add, not null
    199     * @throws NullPointerException if {@code operand} is null
    200     * @return the value associated with this instance immediately before the operand was added
    201     * @since 3.5
    202     */
    203    public short getAndAdd(final Number operand) {
    204        final short last = value;
    205        this.value += operand.shortValue();
    206        return last;
    207    }
    208
    209    /**
    210     * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
    211     * immediately prior to the addition operation. This method is not thread safe.
    212     *
    213     * @param operand the quantity to add, not null
    214     * @return the value associated with this instance immediately before the operand was added
    215     * @since 3.5
    216     */
    217    public short getAndAdd(final short operand) {
    218        final short last = value;
    219        this.value += operand;
    220        return last;
    221    }
    222
    223    /**
    224     * Decrements this instance's value by 1; this method returns the value associated with the instance
    225     * immediately prior to the decrement operation. This method is not thread safe.
    226     *
    227     * @return the value associated with the instance before it was decremented
    228     * @since 3.5
    229     */
    230    public short getAndDecrement() {
    231        final short last = value;
    232        value--;
    233        return last;
    234    }
    235
    236    /**
    237     * Increments this instance's value by 1; this method returns the value associated with the instance
    238     * immediately prior to the increment operation. This method is not thread safe.
    239     *
    240     * @return the value associated with the instance before it was incremented
    241     * @since 3.5
    242     */
    243    public short getAndIncrement() {
    244        final short last = value;
    245        value++;
    246        return last;
    247    }
    248
    249    /**
    250     * Gets the value as a Short instance.
    251     *
    252     * @return the value as a Short, never null.
    253     * @deprecated Use {@link #get()}.
    254     */
    255    @Deprecated
    256    @Override
    257    public Short getValue() {
    258        return Short.valueOf(this.value);
    259    }
    260
    261    /**
    262     * Returns a suitable hash code for this mutable.
    263     *
    264     * @return a suitable hash code
    265     */
    266    @Override
    267    public int hashCode() {
    268        return value;
    269    }
    270
    271    /**
    272     * Increments the value.
    273     *
    274     * @since 2.2
    275     */
    276    public void increment() {
    277        value++;
    278    }
    279
    280    /**
    281     * Increments this instance's value by 1; this method returns the value associated with the instance
    282     * immediately after the increment operation. This method is not thread safe.
    283     *
    284     * @return the value associated with the instance after it is incremented
    285     * @since 3.5
    286     */
    287    public short incrementAndGet() {
    288        value++;
    289        return value;
    290    }
    291
    292    /**
    293     * Returns the value of this MutableShort as an int.
    294     *
    295     * @return the numeric value represented by this object after conversion to type int.
    296     */
    297    @Override
    298    public int intValue() {
    299        return value;
    300    }
    301
    302    /**
    303     * Returns the value of this MutableShort as a long.
    304     *
    305     * @return the numeric value represented by this object after conversion to type long.
    306     */
    307    @Override
    308    public long longValue() {
    309        return value;
    310    }
    311
    312    /**
    313     * Sets the value from any Number instance.
    314     *
    315     * @param value  the value to set, not null
    316     * @throws NullPointerException if the object is null
    317     */
    318    @Override
    319    public void setValue(final Number value) {
    320        this.value = value.shortValue();
    321    }
    322
    323    /**
    324     * Sets the value.
    325     *
    326     * @param value  the value to set
    327     */
    328    public void setValue(final short value) {
    329        this.value = value;
    330    }
    331
    332    // byteValue relies on Number implementation
    333    /**
    334     * Returns the value of this MutableShort as a short.
    335     *
    336     * @return the numeric value represented by this object after conversion to type short.
    337     */
    338    @Override
    339    public short shortValue() {
    340        return value;
    341    }
    342
    343    /**
    344     * Subtracts a value from the value of this instance.
    345     *
    346     * @param operand  the value to subtract, not null
    347     * @throws NullPointerException if the object is null
    348     * @since 2.2
    349     */
    350    public void subtract(final Number operand) {
    351        this.value -= operand.shortValue();
    352    }
    353
    354    /**
    355     * Subtracts a value from the value of this instance.
    356     *
    357     * @param operand  the value to subtract, not null
    358     * @since 2.2
    359     */
    360    public void subtract(final short operand) {
    361        this.value -= operand;
    362    }
    363
    364    /**
    365     * Gets this mutable as an instance of Short.
    366     *
    367     * @return a Short instance containing the value from this mutable, never null
    368     */
    369    public Short toShort() {
    370        return Short.valueOf(shortValue());
    371    }
    372
    373    /**
    374     * Returns the String value of this mutable.
    375     *
    376     * @return the mutable value as a string
    377     */
    378    @Override
    379    public String toString() {
    380        return String.valueOf(value);
    381    }
    382
    383}