Package aQute.libg.ints
Class IntCounter
- java.lang.Object
-
- java.lang.Number
-
- aQute.libg.ints.IntCounter
-
- All Implemented Interfaces:
java.io.Serializable
public class IntCounter extends java.lang.NumberThis 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:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description (package private) intcount(package private) booleanoverflowprivate static longserialVersionUID
-
Constructor Summary
Constructors Constructor Description IntCounter()IntCounter(int n)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description intadd(int value)intdec()Increment the current value.intdiv(int value)doubledoubleValue()If the overflow flag is set, a NaN will be returnedfloatfloatValue()If the overflow flag is set, a NaN will be returnedintget()Get the current valuebooleanhasOverflow()intinc()Increment the current value.intintValue()booleanisNotZero()booleanisZero()longlongValue()intmul(int value)intreset()Reset the counter to zerointset(int newValue)Set a new value and return the previous value.private intset(long value)intsub(int value)java.lang.StringtoString()Returns the String representation of the current value.
-
-
-
Field Detail
-
serialVersionUID
private static final long serialVersionUID
- See Also:
- Constant Field Values
-
count
int count
-
overflow
boolean overflow
-
-
Method Detail
-
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()
- Specified by:
intValuein classjava.lang.Number
-
longValue
public long longValue()
- Specified by:
longValuein classjava.lang.Number
-
floatValue
public float floatValue()
If the overflow flag is set, a NaN will be returned- Specified by:
floatValuein classjava.lang.Number
-
doubleValue
public double doubleValue()
If the overflow flag is set, a NaN will be returned- Specified by:
doubleValuein classjava.lang.Number
-
hasOverflow
public boolean hasOverflow()
-
toString
public java.lang.String toString()
Returns the String representation of the current value. If the value has overflown, a '!' is appended.- Overrides:
toStringin classjava.lang.Object- Returns:
- the String representation of the current value
-
set
private int set(long value) throws java.lang.IllegalArgumentException- Throws:
java.lang.IllegalArgumentException
-
isZero
public boolean isZero()
-
isNotZero
public boolean isNotZero()
-
-