Class MutableInt

java.lang.Object
java.lang.Number
org.apache.commons.lang3.mutable.MutableInt
All Implemented Interfaces:
Serializable, Comparable<MutableInt>, Mutable<Number>

public class MutableInt extends Number implements Comparable<MutableInt>, Mutable<Number>
A mutable int wrapper.

Note that as MutableInt does not extend Integer, it is not treated by String.format as an Integer parameter.

Since:
2.1
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new MutableInt with the default value of zero.
    MutableInt(int value)
    Constructs a new MutableInt with the specified value.
    Constructs a new MutableInt with the specified value.
    Constructs a new MutableInt parsing the given string.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    add(int operand)
    Adds a value to the value of this instance.
    void
    add(Number operand)
    Adds a value to the value of this instance.
    int
    addAndGet(int operand)
    Increments this instance's value by operand; this method returns the value associated with the instance immediately after the addition operation.
    int
    addAndGet(Number operand)
    Increments this instance's value by operand; this method returns the value associated with the instance immediately after the addition operation.
    int
    Compares this mutable to another in ascending order.
    void
    Decrements the value.
    int
    Decrements this instance's value by 1; this method returns the value associated with the instance immediately after the decrement operation.
    double
    Returns the value of this MutableInt as a double.
    boolean
    Compares this object to the specified object.
    float
    Returns the value of this MutableInt as a float.
    int
    getAndAdd(int operand)
    Increments this instance's value by operand; this method returns the value associated with the instance immediately prior to the addition operation.
    int
    getAndAdd(Number operand)
    Increments this instance's value by operand; this method returns the value associated with the instance immediately prior to the addition operation.
    int
    Decrements this instance's value by 1; this method returns the value associated with the instance immediately prior to the decrement operation.
    int
    Increments this instance's value by 1; this method returns the value associated with the instance immediately prior to the increment operation.
    Gets the value as a Integer instance.
    int
    Returns a suitable hash code for this mutable.
    void
    Increments the value.
    int
    Increments this instance's value by 1; this method returns the value associated with the instance immediately after the increment operation.
    int
    Returns the value of this MutableInt as an int.
    long
    Returns the value of this MutableInt as a long.
    void
    setValue(int value)
    Sets the value.
    void
    Sets the value from any Number instance.
    void
    subtract(int operand)
    Subtracts a value from the value of this instance.
    void
    subtract(Number operand)
    Subtracts a value from the value of this instance.
    Gets this mutable as an instance of Integer.
    Returns the String value of this mutable.

    Methods inherited from class java.lang.Number

    byteValue, shortValue

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • MutableInt

      public MutableInt()
      Constructs a new MutableInt with the default value of zero.
    • MutableInt

      public MutableInt(int value)
      Constructs a new MutableInt with the specified value.
      Parameters:
      value - the initial value to store
    • MutableInt

      public MutableInt(Number value)
      Constructs a new MutableInt with the specified value.
      Parameters:
      value - the initial value to store, not null
      Throws:
      NullPointerException - if the object is null
    • MutableInt

      public MutableInt(String value)
      Constructs a new MutableInt parsing the given string.
      Parameters:
      value - the string to parse, not null
      Throws:
      NumberFormatException - if the string cannot be parsed into an int
      Since:
      2.5
  • Method Details

    • add

      public void add(int operand)
      Adds a value to the value of this instance.
      Parameters:
      operand - the value to add, not null
      Since:
      2.2
    • add

      public void add(Number operand)
      Adds a value to the value of this instance.
      Parameters:
      operand - the value to add, not null
      Throws:
      NullPointerException - if the object is null
      Since:
      2.2
    • addAndGet

      public int addAndGet(int operand)
      Increments this instance's value by operand; this method returns the value associated with the instance immediately after the addition operation. This method is not thread safe.
      Parameters:
      operand - the quantity to add, not null
      Returns:
      the value associated with this instance after adding the operand
      Since:
      3.5
    • addAndGet

      public int addAndGet(Number operand)
      Increments this instance's value by operand; this method returns the value associated with the instance immediately after the addition operation. This method is not thread safe.
      Parameters:
      operand - the quantity to add, not null
      Returns:
      the value associated with this instance after adding the operand
      Throws:
      NullPointerException - if operand is null
      Since:
      3.5
    • compareTo

      public int compareTo(MutableInt other)
      Compares this mutable to another in ascending order.
      Specified by:
      compareTo in interface Comparable<MutableInt>
      Parameters:
      other - the other mutable to compare to, not null
      Returns:
      negative if this is less, zero if equal, positive if greater
    • decrement

      public void decrement()
      Decrements the value.
      Since:
      2.2
    • decrementAndGet

      public int decrementAndGet()
      Decrements this instance's value by 1; this method returns the value associated with the instance immediately after the decrement operation. This method is not thread safe.
      Returns:
      the value associated with the instance after it is decremented
      Since:
      3.5
    • doubleValue

      public double doubleValue()
      Returns the value of this MutableInt as a double.
      Specified by:
      doubleValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type double.
    • equals

      public boolean equals(Object obj)
      Compares this object to the specified object. The result is true if and only if the argument is not null and is a MutableInt object that contains the same int value as this object.
      Overrides:
      equals in class Object
      Parameters:
      obj - the object to compare with, null returns false
      Returns:
      true if the objects are the same; false otherwise.
    • floatValue

      public float floatValue()
      Returns the value of this MutableInt as a float.
      Specified by:
      floatValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type float.
    • getAndAdd

      public int getAndAdd(int operand)
      Increments this instance's value by operand; this method returns the value associated with the instance immediately prior to the addition operation. This method is not thread safe.
      Parameters:
      operand - the quantity to add, not null
      Returns:
      the value associated with this instance immediately before the operand was added
      Since:
      3.5
    • getAndAdd

      public int getAndAdd(Number operand)
      Increments this instance's value by operand; this method returns the value associated with the instance immediately prior to the addition operation. This method is not thread safe.
      Parameters:
      operand - the quantity to add, not null
      Returns:
      the value associated with this instance immediately before the operand was added
      Throws:
      NullPointerException - if operand is null
      Since:
      3.5
    • getAndDecrement

      public int getAndDecrement()
      Decrements this instance's value by 1; this method returns the value associated with the instance immediately prior to the decrement operation. This method is not thread safe.
      Returns:
      the value associated with the instance before it was decremented
      Since:
      3.5
    • getAndIncrement

      public int getAndIncrement()
      Increments this instance's value by 1; this method returns the value associated with the instance immediately prior to the increment operation. This method is not thread safe.
      Returns:
      the value associated with the instance before it was incremented
      Since:
      3.5
    • getValue

      public Integer getValue()
      Gets the value as a Integer instance.
      Specified by:
      getValue in interface Mutable<Number>
      Returns:
      the value as a Integer, never null
    • hashCode

      public int hashCode()
      Returns a suitable hash code for this mutable.
      Overrides:
      hashCode in class Object
      Returns:
      a suitable hash code
    • increment

      public void increment()
      Increments the value.
      Since:
      2.2
    • incrementAndGet

      public int incrementAndGet()
      Increments this instance's value by 1; this method returns the value associated with the instance immediately after the increment operation. This method is not thread safe.
      Returns:
      the value associated with the instance after it is incremented
      Since:
      3.5
    • intValue

      public int intValue()
      Returns the value of this MutableInt as an int.
      Specified by:
      intValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type int.
    • longValue

      public long longValue()
      Returns the value of this MutableInt as a long.
      Specified by:
      longValue in class Number
      Returns:
      the numeric value represented by this object after conversion to type long.
    • setValue

      public void setValue(int value)
      Sets the value.
      Parameters:
      value - the value to set
    • setValue

      public void setValue(Number value)
      Sets the value from any Number instance.
      Specified by:
      setValue in interface Mutable<Number>
      Parameters:
      value - the value to set, not null
      Throws:
      NullPointerException - if the object is null
    • subtract

      public void subtract(int operand)
      Subtracts a value from the value of this instance.
      Parameters:
      operand - the value to subtract, not null
      Since:
      2.2
    • subtract

      public void subtract(Number operand)
      Subtracts a value from the value of this instance.
      Parameters:
      operand - the value to subtract, not null
      Throws:
      NullPointerException - if the object is null
      Since:
      2.2
    • toInteger

      public Integer toInteger()
      Gets this mutable as an instance of Integer.
      Returns:
      an Integer instance containing the value from this mutable, never null
    • toString

      public String toString()
      Returns the String value of this mutable.
      Overrides:
      toString in class Object
      Returns:
      the mutable value as a string