Skip to content

io microsphere lang MutableInteger

github-actions[bot] edited this page Mar 21, 2026 · 1 revision

MutableInteger

Type: Class | Module: microsphere-java-core | Package: io.microsphere.lang | Since: 1.0.0

Source: microsphere-java-core/src/main/java/io/microsphere/lang/MutableInteger.java

Overview

A mutable integer container that provides various atomic operations for integer addition, increment, decrement, and value retrieval. This class extends Number, 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
`

Declaration

public class MutableInteger extends Number

Author: Mercy

Version Information

  • Introduced in: 1.0.0
  • Current Project Version: 0.1.10-SNAPSHOT

Version Compatibility

This component is tested and compatible with the following Java versions:

Java Version Status
Java 8 ✅ Compatible
Java 11 ✅ Compatible
Java 17 ✅ Compatible
Java 21 ✅ Compatible
Java 25 ✅ Compatible

Examples

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

Method Examples

get

MutableInteger i = new MutableInteger(5);
int currentValue = i.get(); // retrieves the current value
System.out.println(currentValue); // prints 5

set

MutableInteger i = new MutableInteger(0);
i.set(10); // sets the value to 10
System.out.println(i.get()); // prints 10

getAndSet

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

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

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

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

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

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

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

intValue

MutableInteger i = new MutableInteger(7);
int value = i.intValue(); // retrieves the current integer value
System.out.println(value); // prints 7

longValue

MutableInteger i = new MutableInteger(5);
long value = i.longValue(); // retrieves the current value as a long
System.out.println(value); // prints 5

floatValue

MutableInteger i = new MutableInteger(5);
float value = i.floatValue(); // retrieves the current value as a float
System.out.println(value); // prints 5.0

doubleValue

MutableInteger i = new MutableInteger(5);
double value = i.doubleValue(); // retrieves the current value as a double
System.out.println(value); // prints 5.0

of

MutableInteger i = MutableInteger.of(10); // creates an MutableInteger with initial value 10
System.out.println(i.get()); // prints 10

Usage

Maven Dependency

Add the following dependency to your pom.xml:

<dependency>
    <groupId>io.github.microsphere-projects</groupId>
    <artifactId>microsphere-java-core</artifactId>
    <version>${microsphere-java.version}</version>
</dependency>

Tip: Use the BOM (microsphere-java-dependencies) for consistent version management. See the Getting Started guide.

Import

import io.microsphere.lang.MutableInteger;

API Reference

Public Methods

Method Description
get Gets the current value stored in this MutableInteger.
set Sets the value to the given newValue and returns this instance.
getAndSet Sets the value to the given newValue and returns the previous value.
getAndIncrement increments the current value by 1 and returns the previous value before the increment.
getAndDecrement Decrements the current value by 1 and returns the previous value before the decrement.
getAndAdd Adds the given delta to the current value and returns the previous value before the addition.
incrementAndGet Increments the current value by 1 and returns the updated value.
decrementAndGet Decrements the current value by 1 and returns the updated value.
addAndGet Adds the given delta to the current value and returns the updated value.
intValue Returns the integer value stored in this MutableInteger.
longValue Returns the current value as a long.
floatValue Returns the current value as a float.
doubleValue Returns the current value as a double.
of Creates a new instance of MutableInteger initialized with the given value.

Method Details

get

public final int get()

Gets the current value stored in this MutableInteger.

set

public final MutableInteger set(int newValue)

Sets the value to the given newValue and returns this instance.

getAndSet

public final int getAndSet(int newValue)

Sets the value to the given newValue and returns the previous value.

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() and incrementAndGet() methods, where the current value is returned before it is increased by 1.

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() and decrementAndGet() methods, where the current value is returned before it is decreased by 1.

getAndAdd

public final int getAndAdd(int delta)

Adds the given delta 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() and addAndGet(int) methods.

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.

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.

addAndGet

public int addAndGet(int delta)

Adds the given delta 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.

intValue

public int intValue()

Returns the integer value stored in this MutableInteger.

This method provides the implementation for the Number class's abstract method, allowing this class to be used where a Number is expected.

longValue

public long longValue()

Returns the current value as a long.

This method provides the implementation for the Number class's abstract method, allowing this class to be used where a Number is expected.

floatValue

public float floatValue()

Returns the current value as a float.

This method provides the implementation for the Number class's abstract method, allowing this class to be used where a Number is expected.

doubleValue

public double doubleValue()

Returns the current value as a double.

This method provides the implementation for the Number class's abstract method, allowing this class to be used where a Number is expected.

of

public static MutableInteger of(int value)

Creates a new instance of MutableInteger initialized with the given value.

This static factory method provides a convenient way to create an instance of MutableInteger with the specified initial value.

See Also

  • java.util.concurrent.atomic.AtomicInteger

This documentation was auto-generated from the source code of microsphere-java.

Home

java-annotations

java-core

jdk-tools

lang-model

annotation-processor

java-test

Clone this wiki locally