Class MutableInteger
- java.lang.Object
-
- java.lang.Number
-
- io.microsphere.lang.MutableInteger
-
- All Implemented Interfaces:
java.io.Serializable
public class MutableInteger extends java.lang.Number
A 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 int
addAndGet(int delta)
Adds the givendelta
to the current value and returns the updated value.int
decrementAndGet()
Decrements the current value by 1 and returns the updated value.double
doubleValue()
Returns the current value as adouble
.boolean
equals(java.lang.Object obj)
float
floatValue()
Returns the current value as afloat
.int
get()
Gets the current value stored in thisMutableInteger
.int
getAndAdd(int delta)
Adds the givendelta
to the current value and returns the previous value before the addition.int
getAndDecrement()
Decrements the current value by 1 and returns the previous value before the decrement.int
getAndIncrement()
increments the current value by 1 and returns the previous value before the increment.int
getAndSet(int newValue)
Sets the value to the givennewValue
and returns the previous value.int
hashCode()
int
incrementAndGet()
Increments the current value by 1 and returns the updated value.int
intValue()
Returns the integer value stored in thisMutableInteger
.long
longValue()
static MutableInteger
of(int value)
Creates a new instance ofMutableInteger
initialized with the given value.MutableInteger
set(int newValue)
Sets the value to the given newValue and returns this instance.java.lang.String
toString()
-
-
-
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
MutableInteger
Example 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 givennewValue
and 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 givendelta
to 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
++value
operation, 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
--value
operation, 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 givendelta
to 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:
toString
in classjava.lang.Object
-
intValue
public int intValue()
Returns the integer value stored in thisMutableInteger
.This method provides the implementation for the
Number
class's abstract method, allowing this class to be used where aNumber
is expected.- Specified by:
intValue
in 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:
longValue
in classjava.lang.Number
-
floatValue
public float floatValue()
Returns the current value as afloat
.This method provides the implementation for the
Number
class's abstract method, allowing this class to be used where aNumber
is expected.- Specified by:
floatValue
in classjava.lang.Number
- Returns:
- the current value as a
float
Example 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
Number
class's abstract method, allowing this class to be used where aNumber
is expected.- Specified by:
doubleValue
in classjava.lang.Number
- Returns:
- the current value as a
double
Example 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:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
of
public static MutableInteger of(int value)
Creates a new instance ofMutableInteger
initialized with the given value.This static factory method provides a convenient way to create an instance of
MutableInteger
with the specified initial value.- Parameters:
value
- the initial value for theMutableInteger
- Returns:
- a new instance of
MutableInteger
initialized with the given valueExample Usage
MutableInteger i = MutableInteger.of(10); // creates an MutableInteger with initial value 10 System.out.println(i.get()); // prints 10
-
-