Class MutableInteger
- java.lang.Object
-
- java.lang.Number
-
- io.microsphere.lang.MutableInteger
-
- All Implemented Interfaces:
java.io.Serializable
public class MutableInteger extends java.lang.NumberA mutable integer container that provides various atomic operations for integer addition, increment, decrement, and value retrieval. This class extendsNumber, allowing it to be used in contexts where a numeric representation is required.Example Usage
MutableInteger i = MutableInteger.of(5); // Get and set a new value int oldValue = i.getAndSet(10); // Returns 5, value becomes 10 // Increment and get the new value int newValue = i.incrementAndGet(); // Returns 11, value becomes 11 // Add a delta and get the updated value int result = i.addAndGet(5); // Returns 16, value becomes 16 // Get current value int currentValue = i.get(); // Returns 16- Since:
- 1.0.0
- Author:
- Mercy
- See Also:
AtomicInteger, Serialized Form
-
-
Constructor Summary
Constructors Constructor Description MutableInteger(int value)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description intaddAndGet(int delta)Adds the givendeltato the current value and returns the updated value.intdecrementAndGet()Decrements the current value by 1 and returns the updated value.doubledoubleValue()Returns the current value as adouble.booleanequals(java.lang.Object obj)floatfloatValue()Returns the current value as afloat.intget()Gets the current value stored in thisMutableInteger.intgetAndAdd(int delta)Adds the givendeltato the current value and returns the previous value before the addition.intgetAndDecrement()Decrements the current value by 1 and returns the previous value before the decrement.intgetAndIncrement()increments the current value by 1 and returns the previous value before the increment.intgetAndSet(int newValue)Sets the value to the givennewValueand returns the previous value.inthashCode()intincrementAndGet()Increments the current value by 1 and returns the updated value.intintValue()Returns the integer value stored in thisMutableInteger.longlongValue()static MutableIntegerof(int value)Creates a new instance ofMutableIntegerinitialized with the given value.MutableIntegerset(int newValue)Sets the value to the given newValue and returns this instance.java.lang.StringtoString()
-
-
-
Method Detail
-
get
public final int get()
Gets the current value stored in thisMutableInteger.- Returns:
- the current integer value
Example Usage
MutableInteger i = new MutableInteger(5); int currentValue = i.get(); // retrieves the current value System.out.println(currentValue); // prints 5
-
set
public final MutableInteger set(int newValue)
Sets the value to the given newValue and returns this instance.- Parameters:
newValue- the new value to set- Returns:
- this instance of
MutableIntegerExample Usage
MutableInteger i = new MutableInteger(0); i.set(10); // sets the value to 10 System.out.println(i.get()); // prints 10
-
getAndSet
public final int getAndSet(int newValue)
Sets the value to the givennewValueand returns the previous value.- Parameters:
newValue- the new value to set- Returns:
- the previous value before the update
Example Usage
MutableInteger i = new MutableInteger(5); int oldValue = i.getAndSet(10); // sets the value to 10, returns 5 System.out.println(oldValue); // prints 5 System.out.println(i.get()); // prints 10
-
getAndIncrement
public final int getAndIncrement()
increments the current value by 1 and returns the previous value before the increment.This method behaves similarly to a combination of the
get()andincrementAndGet()methods, where the current value is returned before it is increased by 1.- Returns:
- the value before incrementing
Example Usage
MutableInteger i = new MutableInteger(5); int oldValue = i.getAndIncrement(); // increments the value to 6, returns 5 System.out.println(oldValue); // prints 5 System.out.println(i.get()); // prints 6
-
getAndDecrement
public final int getAndDecrement()
Decrements the current value by 1 and returns the previous value before the decrement.This method behaves similarly to a combination of the
get()anddecrementAndGet()methods, where the current value is returned before it is decreased by 1.- Returns:
- the value before decrementing
Example Usage
MutableInteger i = new MutableInteger(5); int oldValue = i.getAndDecrement(); // decrements the value to 4, returns 5 System.out.println(oldValue); // prints 5 System.out.println(i.get()); // prints 4
-
getAndAdd
public final int getAndAdd(int delta)
Adds the givendeltato the current value and returns the previous value before the addition.This method ensures that the current value is returned before the addition operation is performed, similar to a combination of the
get()andaddAndGet(int)methods.- Parameters:
delta- the value to add to the current value- Returns:
- the previous value before the addition
Example Usage
MutableInteger i = new MutableInteger(5); int oldValue = i.getAndAdd(3); // adds 3 to the current value (5), returns 5 System.out.println(oldValue); // prints 5 System.out.println(i.get()); // prints 8
-
incrementAndGet
public final int incrementAndGet()
Increments the current value by 1 and returns the updated value.This method behaves similarly to the
++valueoperation, where the value is increased by 1 before it is returned.- Returns:
- the updated value after incrementing
Example Usage
MutableInteger i = new MutableInteger(5); int newValue = i.incrementAndGet(); // increments the value to 6, returns 6 System.out.println(newValue); // prints 6 System.out.println(i.get()); // prints 6
-
decrementAndGet
public final int decrementAndGet()
Decrements the current value by 1 and returns the updated value.This method behaves similarly to the
--valueoperation, where the value is decreased by 1 before it is returned.- Returns:
- the updated value after decrementing
Example Usage
MutableInteger i = new MutableInteger(5); int newValue = i.decrementAndGet(); // decrements the value to 4, returns 4 System.out.println(newValue); // prints 4 System.out.println(i.get()); // prints 4
-
addAndGet
public int addAndGet(int delta)
Adds the givendeltato the current value and returns the updated value.This method ensures that the new value is calculated and stored before it is returned, behaving similarly to the compound addition operation followed by a retrieval.
- Parameters:
delta- the value to add to the current value- Returns:
- the updated value after adding the delta
Example Usage
MutableInteger i = new MutableInteger(5); int newValue = i.addAndGet(3); // adds 3 to the current value (5), returns 8 System.out.println(newValue); // prints 8 System.out.println(i.get()); // prints 8
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
intValue
public int intValue()
Returns the integer value stored in thisMutableInteger.This method provides the implementation for the
Numberclass's abstract method, allowing this class to be used where aNumberis expected.- Specified by:
intValuein classjava.lang.Number- Returns:
- the current integer value
Example Usage
MutableInteger i = new MutableInteger(7); int value = i.intValue(); // retrieves the current integer value System.out.println(value); // prints 7
-
longValue
public long longValue()
- Specified by:
longValuein classjava.lang.Number
-
floatValue
public float floatValue()
Returns the current value as afloat.This method provides the implementation for the
Numberclass's abstract method, allowing this class to be used where aNumberis expected.- Specified by:
floatValuein classjava.lang.Number- Returns:
- the current value as a
floatExample Usage
MutableInteger i = new MutableInteger(5); float value = i.floatValue(); // retrieves the current value as a float System.out.println(value); // prints 5.0
-
doubleValue
public double doubleValue()
Returns the current value as adouble.This method provides the implementation for the
Numberclass's abstract method, allowing this class to be used where aNumberis expected.- Specified by:
doubleValuein classjava.lang.Number- Returns:
- the current value as a
doubleExample Usage
MutableInteger i = new MutableInteger(5); double value = i.doubleValue(); // retrieves the current value as a double System.out.println(value); // prints 5.0
-
hashCode
public int hashCode()
- Overrides:
hashCodein classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equalsin classjava.lang.Object
-
of
public static MutableInteger of(int value)
Creates a new instance ofMutableIntegerinitialized with the given value.This static factory method provides a convenient way to create an instance of
MutableIntegerwith the specified initial value.- Parameters:
value- the initial value for theMutableInteger- Returns:
- a new instance of
MutableIntegerinitialized with the given valueExample Usage
MutableInteger i = MutableInteger.of(10); // creates an MutableInteger with initial value 10 System.out.println(i.get()); // prints 10
-
-