Package aQute.libg.ints
Class IntCounter
java.lang.Object
java.lang.Number
aQute.libg.ints.IntCounter
- All Implemented Interfaces:
Serializable
This is a very simple fast counter without any synchronization. It is
intended to be used as a counter in recursive calls or when you need to use a
counter shared between code and a lambda. In that case you cannot use an int
because it must be final to be used by the lambda. (Smalltalk supported this
in 1972, but alas.) Last, it also has overflow handling for the common math
operations. When operation would overflow, the old value is maintained and an
overflow flag is set.
void foo() { IntCounter ic = new IntCounter(); doSomething(ic::in); System.out.println(ic); }None of the methods are atomic. The API of AtomicInteger is used so that it can be replaced when the AtomicInteger is abused for the purpose of this class.
If an operation would overflow/underflow, overflow boolean is set. An overflowing value is then not set, the old value remains.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) int
(package private) boolean
private static final long
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionint
add
(int value) int
dec()
Increment the current value.int
div
(int value) double
If the overflow flag is set, a NaN will be returnedfloat
If the overflow flag is set, a NaN will be returnedint
get()
Get the current valueboolean
int
inc()
Increment the current value.int
intValue()
boolean
boolean
isZero()
long
int
mul
(int value) int
reset()
Reset the counter to zeroint
set
(int newValue) Set a new value and return the previous value.private int
set
(long value) int
sub
(int value) toString()
Returns the String representation of the current value.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
serialVersionUID
private static final long serialVersionUID- See Also:
-
count
int count -
overflow
boolean overflow
-
-
Constructor Details
-
IntCounter
public IntCounter() -
IntCounter
public IntCounter(int n)
-
-
Method Details
-
inc
public int inc()Increment the current value. The old value is returned and the new value is checked for overflow. overflow will keep the old value- Returns:
- the old value
-
dec
public int dec()Increment the current value. The old value is returned and the new value is checked for underflow.- Returns:
- the old value
-
reset
public int reset()Reset the counter to zero- Returns:
- the previous value
-
get
public int get()Get the current value- Returns:
- the current value
-
set
public int set(int newValue) Set a new value and return the previous value. Overflow is cleared.- Parameters:
newValue
- the new value- Returns:
- the previous value
-
add
public int add(int value) -
sub
public int sub(int value) -
mul
public int mul(int value) -
div
public int div(int value) -
intValue
public int intValue() -
longValue
public long longValue() -
floatValue
public float floatValue()If the overflow flag is set, a NaN will be returned- Specified by:
floatValue
in classNumber
-
doubleValue
public double doubleValue()If the overflow flag is set, a NaN will be returned- Specified by:
doubleValue
in classNumber
-
hasOverflow
public boolean hasOverflow() -
toString
Returns the String representation of the current value. If the value has overflown, a '!' is appended. -
set
- Throws:
IllegalArgumentException
-
isZero
public boolean isZero() -
isNotZero
public boolean isNotZero()
-