MutableShort.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.apache.commons.lang3.mutable;

  18. import org.apache.commons.lang3.math.NumberUtils;

  19. /**
  20.  * A mutable {@code short} wrapper.
  21.  * <p>
  22.  * Note that as MutableShort does not extend Short, it is not treated by String.format as a Short parameter.
  23.  * </p>
  24.  *
  25.  * @see Short
  26.  * @since 2.1
  27.  */
  28. public class MutableShort extends Number implements Comparable<MutableShort>, Mutable<Number> {

  29.     /**
  30.      * Required for serialization support.
  31.      *
  32.      * @see java.io.Serializable
  33.      */
  34.     private static final long serialVersionUID = -2135791679L;

  35.     /** The mutable value. */
  36.     private short value;

  37.     /**
  38.      * Constructs a new MutableShort with the default value of zero.
  39.      */
  40.     public MutableShort() {
  41.     }

  42.     /**
  43.      * Constructs a new MutableShort with the specified value.
  44.      *
  45.      * @param value  the initial value to store, not null
  46.      * @throws NullPointerException if the object is null
  47.      */
  48.     public MutableShort(final Number value) {
  49.         this.value = value.shortValue();
  50.     }

  51.     /**
  52.      * Constructs a new MutableShort with the specified value.
  53.      *
  54.      * @param value  the initial value to store
  55.      */
  56.     public MutableShort(final short value) {
  57.         this.value = value;
  58.     }

  59.     /**
  60.      * Constructs a new MutableShort parsing the given string.
  61.      *
  62.      * @param value  the string to parse, not null
  63.      * @throws NumberFormatException if the string cannot be parsed into a short
  64.      * @since 2.5
  65.      */
  66.     public MutableShort(final String value) {
  67.         this.value = Short.parseShort(value);
  68.     }

  69.     /**
  70.      * Adds a value to the value of this instance.
  71.      *
  72.      * @param operand  the value to add, not null
  73.      * @throws NullPointerException if the object is null
  74.      * @since 2.2
  75.      */
  76.     public void add(final Number operand) {
  77.         this.value += operand.shortValue();
  78.     }

  79.     /**
  80.      * Adds a value to the value of this instance.
  81.      *
  82.      * @param operand  the value to add, not null
  83.      * @since 2.2
  84.      */
  85.     public void add(final short operand) {
  86.         this.value += operand;
  87.     }

  88.     /**
  89.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  90.      * immediately after the addition operation. This method is not thread safe.
  91.      *
  92.      * @param operand the quantity to add, not null
  93.      * @throws NullPointerException if {@code operand} is null
  94.      * @return the value associated with this instance after adding the operand
  95.      * @since 3.5
  96.      */
  97.     public short addAndGet(final Number operand) {
  98.         this.value += operand.shortValue();
  99.         return value;
  100.     }

  101.     /**
  102.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  103.      * immediately after the addition operation. This method is not thread safe.
  104.      *
  105.      * @param operand the quantity to add, not null
  106.      * @return the value associated with this instance after adding the operand
  107.      * @since 3.5
  108.      */
  109.     public short addAndGet(final short operand) {
  110.         this.value += operand;
  111.         return value;
  112.     }

  113.     /**
  114.      * Compares this mutable to another in ascending order.
  115.      *
  116.      * @param other  the other mutable to compare to, not null
  117.      * @return negative if this is less, zero if equal, positive if greater
  118.      */
  119.     @Override
  120.     public int compareTo(final MutableShort other) {
  121.         return NumberUtils.compare(this.value, other.value);
  122.     }

  123.     /**
  124.      * Decrements the value.
  125.      *
  126.      * @since 2.2
  127.      */
  128.     public void decrement() {
  129.         value--;
  130.     }

  131.     /**
  132.      * Decrements this instance's value by 1; this method returns the value associated with the instance
  133.      * immediately after the decrement operation. This method is not thread safe.
  134.      *
  135.      * @return the value associated with the instance after it is decremented
  136.      * @since 3.5
  137.      */
  138.     public short decrementAndGet() {
  139.         value--;
  140.         return value;
  141.     }

  142.     /**
  143.      * Returns the value of this MutableShort as a double.
  144.      *
  145.      * @return the numeric value represented by this object after conversion to type double.
  146.      */
  147.     @Override
  148.     public double doubleValue() {
  149.         return value;
  150.     }

  151.     /**
  152.      * Compares this object to the specified object. The result is {@code true} if and only if the argument
  153.      * is not {@code null} and is a {@link MutableShort} object that contains the same {@code short}
  154.      * value as this object.
  155.      *
  156.      * @param obj  the object to compare with, null returns false
  157.      * @return {@code true} if the objects are the same; {@code false} otherwise.
  158.      */
  159.     @Override
  160.     public boolean equals(final Object obj) {
  161.         if (obj instanceof MutableShort) {
  162.             return value == ((MutableShort) obj).shortValue();
  163.         }
  164.         return false;
  165.     }

  166.     /**
  167.      * Returns the value of this MutableShort as a float.
  168.      *
  169.      * @return the numeric value represented by this object after conversion to type float.
  170.      */
  171.     @Override
  172.     public float floatValue() {
  173.         return value;
  174.     }

  175.     /**
  176.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  177.      * immediately prior to the addition operation. This method is not thread safe.
  178.      *
  179.      * @param operand the quantity to add, not null
  180.      * @throws NullPointerException if {@code operand} is null
  181.      * @return the value associated with this instance immediately before the operand was added
  182.      * @since 3.5
  183.      */
  184.     public short getAndAdd(final Number operand) {
  185.         final short last = value;
  186.         this.value += operand.shortValue();
  187.         return last;
  188.     }

  189.     /**
  190.      * Increments this instance's value by {@code operand}; this method returns the value associated with the instance
  191.      * immediately prior to the addition operation. This method is not thread safe.
  192.      *
  193.      * @param operand the quantity to add, not null
  194.      * @return the value associated with this instance immediately before the operand was added
  195.      * @since 3.5
  196.      */
  197.     public short getAndAdd(final short operand) {
  198.         final short last = value;
  199.         this.value += operand;
  200.         return last;
  201.     }

  202.     /**
  203.      * Decrements this instance's value by 1; this method returns the value associated with the instance
  204.      * immediately prior to the decrement operation. This method is not thread safe.
  205.      *
  206.      * @return the value associated with the instance before it was decremented
  207.      * @since 3.5
  208.      */
  209.     public short getAndDecrement() {
  210.         final short last = value;
  211.         value--;
  212.         return last;
  213.     }

  214.     /**
  215.      * Increments this instance's value by 1; this method returns the value associated with the instance
  216.      * immediately prior to the increment operation. This method is not thread safe.
  217.      *
  218.      * @return the value associated with the instance before it was incremented
  219.      * @since 3.5
  220.      */
  221.     public short getAndIncrement() {
  222.         final short last = value;
  223.         value++;
  224.         return last;
  225.     }

  226.     /**
  227.      * Gets the value as a Short instance.
  228.      *
  229.      * @return the value as a Short, never null
  230.      */
  231.     @Override
  232.     public Short getValue() {
  233.         return Short.valueOf(this.value);
  234.     }

  235.     /**
  236.      * Returns a suitable hash code for this mutable.
  237.      *
  238.      * @return a suitable hash code
  239.      */
  240.     @Override
  241.     public int hashCode() {
  242.         return value;
  243.     }

  244.     /**
  245.      * Increments the value.
  246.      *
  247.      * @since 2.2
  248.      */
  249.     public void increment() {
  250.         value++;
  251.     }

  252.     /**
  253.      * Increments this instance's value by 1; this method returns the value associated with the instance
  254.      * immediately after the increment operation. This method is not thread safe.
  255.      *
  256.      * @return the value associated with the instance after it is incremented
  257.      * @since 3.5
  258.      */
  259.     public short incrementAndGet() {
  260.         value++;
  261.         return value;
  262.     }

  263.     /**
  264.      * Returns the value of this MutableShort as an int.
  265.      *
  266.      * @return the numeric value represented by this object after conversion to type int.
  267.      */
  268.     @Override
  269.     public int intValue() {
  270.         return value;
  271.     }

  272.     /**
  273.      * Returns the value of this MutableShort as a long.
  274.      *
  275.      * @return the numeric value represented by this object after conversion to type long.
  276.      */
  277.     @Override
  278.     public long longValue() {
  279.         return value;
  280.     }

  281.     /**
  282.      * Sets the value from any Number instance.
  283.      *
  284.      * @param value  the value to set, not null
  285.      * @throws NullPointerException if the object is null
  286.      */
  287.     @Override
  288.     public void setValue(final Number value) {
  289.         this.value = value.shortValue();
  290.     }

  291.     /**
  292.      * Sets the value.
  293.      *
  294.      * @param value  the value to set
  295.      */
  296.     public void setValue(final short value) {
  297.         this.value = value;
  298.     }

  299.     // byteValue relies on Number implementation
  300.     /**
  301.      * Returns the value of this MutableShort as a short.
  302.      *
  303.      * @return the numeric value represented by this object after conversion to type short.
  304.      */
  305.     @Override
  306.     public short shortValue() {
  307.         return value;
  308.     }

  309.     /**
  310.      * Subtracts a value from the value of this instance.
  311.      *
  312.      * @param operand  the value to subtract, not null
  313.      * @throws NullPointerException if the object is null
  314.      * @since 2.2
  315.      */
  316.     public void subtract(final Number operand) {
  317.         this.value -= operand.shortValue();
  318.     }

  319.     /**
  320.      * Subtracts a value from the value of this instance.
  321.      *
  322.      * @param operand  the value to subtract, not null
  323.      * @since 2.2
  324.      */
  325.     public void subtract(final short operand) {
  326.         this.value -= operand;
  327.     }

  328.     /**
  329.      * Gets this mutable as an instance of Short.
  330.      *
  331.      * @return a Short instance containing the value from this mutable, never null
  332.      */
  333.     public Short toShort() {
  334.         return Short.valueOf(shortValue());
  335.     }

  336.     /**
  337.      * Returns the String value of this mutable.
  338.      *
  339.      * @return the mutable value as a string
  340.      */
  341.     @Override
  342.     public String toString() {
  343.         return String.valueOf(value);
  344.     }

  345. }