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>
Direct Known Subclasses:
BitCount, Bytes, Count, Estimate, Maximum, Minimum

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:

  1. Counts are guaranteed to be positive values, so it is not possible to have a Count of -1
  2. Counts have a variety of useful and convenient methods for:
    • Conversion
    • Comparison
    • Mathematics
    • Looping
    • Iteration
    • Array allocation
  3. Counts implement the Quantizable interface, which makes them interoperable with methods that consume Quantizables.
  4. 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


String Representations


Comparison


Minima and Maxima


Arithmetic


Mathematics


Looping


Iteration


Array Allocation

Count objects implement the hashCode() / equals(Object) contract and are Comparable.


Author:
jonathanl (shibo)
See Also: