Module kivakit.core
Class BaseCount<SubClass extends BaseCount<SubClass>>
java.lang.Object
com.telenav.kivakit.core.value.count.BaseCount<SubClass>
- Type Parameters:
SubClass
- The subclass type
- All Implemented Interfaces:
Countable
,NextValue<SubClass>
,Sized
,MapFactory<Long,
,SubClass> Arithmetic<SubClass>
,Castable
,CastTrait
,DoubleQuantizable
,IntegerNumeric<SubClass>
,Maximizable<SubClass>
,Minimizable<SubClass>
,Quantizable
,QuantumComparable<SubClass>
,Stringable
,StringSource
,Source<Long>
,Comparable<Countable>
,Supplier<Long>
public abstract class BaseCount<SubClass extends BaseCount<SubClass>>
extends Object
implements IntegerNumeric<SubClass>, Countable, Comparable<Countable>, Source<Long>
Base class for classes that represent some kind of cardinal number. Each method in this class works for all subclass
types. Instances of the subclass are created with
newInstance(long)
. If a subclass wishes to store
information in its own field, it can override asLong()
.
Counts have useful properties that primitive values like long and int do not have:
- Counts are guaranteed to be positive values, so it is not possible to have a
Count
of -1 - Counts have a variety of useful and convenient methods for:
- Conversion
- Comparison
- Mathematics
- Looping
- Iteration
- Array allocation
- Counts implement the
Quantizable
interface, which makes them interoperable with methods that consumeQuantizable
s. - Counts provide a more readable, comma-separated String representation by default
Efficiency
Count
objects are cheaper than they might seem for two reasons:
- (1) low count constants, and powers of two and ten can be accessed as constant objects in subclasses and values up to 65,536 are cached by subclass.
- (2) allocation of count objects higher than 65,536 are cheap in Java due to the design of generational garbage
collectors. This said, there will be occasions where
Count
objects are not desirable (for example, inside a doubly nested loop) and sometimes they may improve a public API method or constructor, while the internal representation is a primitive value for efficiency.
Although Count
objects are convenient and make method signatures clear and type-safe, as always, the
best approach is to simply use Count
objects until a clear inefficiency shows up in a profiler like YourKit.
Conversion
asInt()
- This count cast to an int valueasLong()
- This count as a longget()
- This count as a longcount()
- This countasBitCount()
- This count as aBitCount
asEstimate()
- This count as anEstimate
asMaximum()
- This count as aMaximum
asMinimum()
- This count as aMinimum
quantum()
- This count as a quantum long value (Quantizable.quantum()
)
String Representations
Quantizable.asString(Format)
- This count formatted in the given formatQuantizable.asCommaSeparatedString()
- This count as a comma-separated string, like 65,536Quantizable.asSimpleString()
- This count as a simple string, like 65536
Comparison
compareTo(Countable)
-Comparable.compareTo(Object)
implementationQuantumComparable.isLessThan(Quantizable)
- True if this count is less than the given quantumQuantumComparable.isGreaterThan(Quantizable)
- True if this count is greater than the given quantumQuantumComparable.isLessThanOrEqualTo(Quantizable)
- True if this count is less than or equal to the given quantum- True if this count is greater than or equal to the given quantum
isZero()
- True if this count is zeroisNonZero()
- True if this count is not zero
Minima and Maxima
isMaximum()
- True if this count ismaximum()
isMinimum()
- True if this count is zeroasMaximum()
- Converts this count to aMaximum
asMinimum()
- Converts this count to aMinimum
maximum(SubClass)
- Returns the maximum of this count and the given countminimum(SubClass)
- Returns the minimum of this count and the given count
Arithmetic
decremented()
- This count minus oneincremented()
- This count plus oneplus(Quantizable)
- This count plus the given countplus(long)
- This count plus the given valueminus(Quantizable)
- This count minus the given countminus(long)
- This count minus the given valuedividedBy(Quantizable)
- This count divided by the given count, using integer division without roundingdividedBy(long)
- This count divided by the given value, using integer division without roundingtimes(Quantizable)
- This count times the given counttimes(long)
- This count times the given valuetimes(double)
- This count times the given value, cast to a long valuetimes(Percent)
- This count times the given percentage
Mathematics
percent(Percent)
- The given percentage of this countpercentOf(Quantizable)
- This count as a percentage of the given countdividesEvenlyBy(Quantizable)
- True if there is no remainder when dividing this count by the given countpowerOfTenCeiling(int)
- The maximum value of this count taking on the given number of digitspowerOfTenFloor(int)
- The minimum value of this count taking on the given number of digitsnextPrime()
- The next prime value from a limited table of primes, useful in allocating linear hashmapsbitsToRepresent()
- The number of bits required to represent this countpowerOfTwoCeiling()
- The next power of two above this count
Looping
loop(Runnable)
- Runs the given code blockcount()
timesloop(LoopBody)
- Runs the given code blockcount()
times, passing the iteration number to the code
Iteration
forEachByte(Consumer)
- Passes to the given consumer, each byte from 0 to the smaller of this count orByte.MAX_VALUE
, exclusiveforEachInteger(Consumer)
- Passes to the given consumer, each byte from 0 to the smaller of this count orInteger.MAX_VALUE
, exclusiveforEachLong(Consumer)
- Passes each long from 0 toasLong()
to the given consumer, exclusiveforEachShort(Consumer)
- Passes to the given consumer, each byte from 0 to the smaller of this count orShort.MAX_VALUE
, exclusive
Array Allocation
newByteArray()
- Allocates a byte array ofcount()
elementsnewCharArray()
- Allocates a char array ofcount()
elementsnewDoubleArray()
- Allocates a double array ofcount()
elementsnewFloatArray()
- Allocates a float array ofcount()
elementsnewIntArray()
- Allocates an int array ofcount()
elementsnewLongArray()
- Allocates a long array ofcount()
elementsnewObjectArray()
- Allocates an Object array ofcount()
elementsnewShortArray()
- Allocates a short array ofcount()
elementsnewStringArray()
- Allocates a String array ofcount()
elements
Count
objects implement the hashCode()
/ equals(Object)
contract and are Comparable
.
- Author:
- jonathanl (shibo)
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface com.telenav.kivakit.interfaces.string.Stringable
Stringable.Format
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionasCount()
int
asInt()
long
asLong()
int
count()
The size of this object as aCount
dividedBy
(long divisor) dividedBy
(Quantizable divisor) boolean
dividesEvenlyBy
(Quantizable value) boolean
void
void
forEachByte
(Consumer<Byte> consumer) void
forEachInteger
(Consumer<Integer> consumer) void
forEachLong
(Consumer<Long> consumer) void
forEachShort
(Consumer<Short> consumer) get()
ImplementsSupplier.get()
to return this count as a Longint
hashCode()
inRangeExclusive
(long value) inRangeInclusive
(long value) boolean
isBetweenExclusive
(BaseCount<?> minimum, BaseCount<?> exclusiveMaximum) boolean
isBetweenInclusive
(BaseCount<?> minimum, BaseCount<?> inclusiveMaximum) boolean
boolean
boolean
boolean
isZero()
void
loop
(FilteredLoopBody<SubClass> body) void
void
void
loopInclusive
(LoopBody<SubClass> code) void
loopIndexes
(Consumer<Integer> body) maximum()
Returns the maximum possible Valueminimum()
Returns The minimum possible valueminus
(long count) minus
(Quantizable count) byte[]
char[]
double[]
float[]
abstract SubClass
newInstance
(long count) Returns a new instance of the concrete subclass of this abstract class.final SubClass
newInstance
(Long count) Creates a value by constructing an object with the given parameterint[]
long[]
<Element> Element[]
short[]
String[]
next()
nextWrap()
offset
(long offset) percentOf
(Quantizable total) plus
(long count) plus
(Quantizable count) powerOfTenCeiling
(int digits) Returns the next higher power of ten with the given number of digits.powerOfTenFloor
(int digits) Returns the next lower power of ten with the given number of digits.Rounds this count up to the next higher por of twolong
quantum()
Returns the discrete value for this objectint
size()
Gets the size of this objecttimes
(double multiplier) times
(long multiplier) times
(Quantizable count) toExclusive
(SubClass maximum) toInclusive
(SubClass maximum) toString()
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface com.telenav.kivakit.interfaces.numeric.Castable
as, asByte, asChar, asDouble, asFloat, asShort
Methods inherited from interface com.telenav.kivakit.interfaces.numeric.CastTrait
cast, cast, maximum, minimum
Methods inherited from interface com.telenav.kivakit.interfaces.numeric.Quantizable
asCommaSeparatedString, asSimpleString, asString, doubleQuantum
Methods inherited from interface com.telenav.kivakit.interfaces.numeric.QuantumComparable
isApproximately, isGreaterThan, isGreaterThanOrEqualTo, isLessThan, isLessThanOrEqualTo
Methods inherited from interface com.telenav.kivakit.interfaces.collection.Sized
isEmpty, isNonEmpty
Methods inherited from interface com.telenav.kivakit.interfaces.string.Stringable
asString
-
Constructor Details
-
BaseCount
public BaseCount() -
BaseCount
protected BaseCount(long count)
-
-
Method Details
-
asBitCount
-
asCount
-
asEstimate
-
asInt
public int asInt() -
asLong
public long asLong() -
asMaximum
-
asMinimum
-
bitsToRepresent
-
count
The size of this object as aCount
-
decremented
- Specified by:
decremented
in interfaceIntegerNumeric<SubClass extends BaseCount<SubClass>>
-
dividedBy
- Specified by:
dividedBy
in interfaceArithmetic<SubClass extends BaseCount<SubClass>>
-
dividedBy
-
equals
-
forEach
-
forEachByte
-
forEachInteger
-
forEachLong
-
forEachShort
-
get
ImplementsSupplier.get()
to return this count as a Long -
hashCode
public int hashCode() -
inRangeExclusive
-
inRangeInclusive
-
incremented
- Specified by:
incremented
in interfaceIntegerNumeric<SubClass extends BaseCount<SubClass>>
-
isMaximum
public boolean isMaximum() -
isMinimum
public boolean isMinimum() -
isNonZero
public boolean isNonZero()- Specified by:
isNonZero
in interfaceQuantizable
-
isZero
public boolean isZero()- Specified by:
isZero
in interfaceQuantizable
-
loop
-
loopIndexes
-
maximum
- Specified by:
maximum
in interfaceMaximizable<SubClass extends BaseCount<SubClass>>
- Returns:
- The maximum of this value and the given value.
-
maximum
Returns the maximum possible Value- Specified by:
maximum
in interfaceMaximizable<SubClass extends BaseCount<SubClass>>
-
minimum
- Specified by:
minimum
in interfaceMinimizable<SubClass extends BaseCount<SubClass>>
- Returns:
- The minimum of this value and the given value.
-
minimum
Returns The minimum possible value- Specified by:
minimum
in interfaceMinimizable<SubClass extends BaseCount<SubClass>>
-
minus
- Specified by:
minus
in interfaceArithmetic<SubClass extends BaseCount<SubClass>>
-
minus
-
newByteArray
public byte[] newByteArray() -
newCharArray
public char[] newCharArray() -
newDoubleArray
public double[] newDoubleArray() -
newFloatArray
public float[] newFloatArray() -
newInstance
Returns a new instance of the concrete subclass of this abstract class.- Parameters:
count
- The count value- Returns:
- The new instance
-
newInstance
Description copied from interface:MapFactory
Creates a value by constructing an object with the given parameter- Specified by:
newInstance
in interfaceMapFactory<Long,
SubClass extends BaseCount<SubClass>> - Parameters:
count
- The constructor parameter for creating a new object- Returns:
- The new object instance
-
newIntArray
public int[] newIntArray() -
newLongArray
public long[] newLongArray() -
newObjectArray
public <Element> Element[] newObjectArray() -
newShortArray
public short[] newShortArray() -
newStringArray
-
next
-
nextPrime
-
nextWrap
-
offset
-
plus
- Specified by:
plus
in interfaceArithmetic<SubClass extends BaseCount<SubClass>>
-
plus
-
powerOfTenCeiling
Returns the next higher power of ten with the given number of digits. For example, if this is a count of 6700, and digits is 5, then the result will be 6700 + 10_000 = 16,700 / 10,000 = 1 * 10,000 = 10,000.- Parameters:
digits
- The number of digits
-
powerOfTenFloor
Returns the next lower power of ten with the given number of digits. For example, if this is a count of 16,700, and digits is 5, then the result will be 16,700 / 10,000 = 1 * 10,000 = 10,000.- Parameters:
digits
- The number of digits
-
powerOfTwoCeiling
Rounds this count up to the next higher por of two -
quantum
public long quantum()Returns the discrete value for this object- Specified by:
quantum
in interfaceQuantizable
- Specified by:
quantum
in interfaceSized
-
size
public int size()Gets the size of this object -
times
- Specified by:
times
in interfaceArithmetic<SubClass extends BaseCount<SubClass>>
-
times
-
times
-
toExclusive
-
toInclusive
-
toString
-