ahc.util
Class IntegerFactory

java.lang.Object
  |
  +--ahc.util.IntegerFactory

public class IntegerFactory
extends java.lang.Object

This class is an implementation of the GOF "Flyweight" pattern. It provides a single point of access to Integer objects and caches them. Since Integer objects are immutable, an entire program can e.g. share a single instance of e.g. "Integer (1)"; this class caches instances and thus allows memory to be saved.
This class uses a fixed number of 1000 bins to store the integers (analogously to a HashMap) in order to maximize retrieval performance. If it is used for numbers larger than that, it will still work but performance will degrade due to the linear lookup overhead inside each bin.
CAUTION: This class never releases an Integer once it was created. So this class should be used only in those cases when that is desired, e.g. for counters of relatively small numbers in collections.

Author:
Arno Haase

Field Summary
static int NUM_BINS
           
 
Constructor Summary
IntegerFactory()
           
 
Method Summary
static java.lang.Integer add(java.lang.Integer v1, int v2)
          convenience method for retrieving the sum.
static java.lang.Integer add(java.lang.Integer v1, java.lang.Integer v2)
          convenience method for retrieving the sum.
static java.lang.Integer decrement(java.lang.Integer value)
          convenience method for retrieving the next lower number.
static java.lang.Integer get(int value)
           
static java.lang.Integer increment(java.lang.Integer value)
          convenience method for retrieving the next higher number.
static java.lang.Integer mod(java.lang.Integer a, int b)
          an implementation of the mathematically typical - as opposed to the Java - implementation of the modulus funtion.
static java.lang.Integer mod(java.lang.Integer a, java.lang.Integer b)
          an implementation of the mathematically typical - as opposed to the Java - implementation of the modulus funtion.
static java.lang.Integer mod(int a, int b)
          an implementation of the mathematically typical - as opposed to the Java - implementation of the modulus funtion.
static java.lang.Integer mod(int a, java.lang.Integer b)
          an implementation of the mathematically typical - as opposed to the Java - implementation of the modulus funtion.
static java.lang.Integer subtract(java.lang.Integer v1, int v2)
          convenience method for retrieving the difference.
static java.lang.Integer subtract(java.lang.Integer v1, java.lang.Integer v2)
          convenience method for retrieving the difference.
static java.lang.Integer subtract(int v1, java.lang.Integer v2)
          convenience method for retrieving the difference.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

NUM_BINS

public static final int NUM_BINS
See Also:
Constant Field Values
Constructor Detail

IntegerFactory

public IntegerFactory()
Method Detail

get

public static java.lang.Integer get(int value)

increment

public static java.lang.Integer increment(java.lang.Integer value)
convenience method for retrieving the next higher number. A parameter value of null is treated as 0.


decrement

public static java.lang.Integer decrement(java.lang.Integer value)
convenience method for retrieving the next lower number. A parameter value of null is treated as 0.


add

public static java.lang.Integer add(java.lang.Integer v1,
                                    java.lang.Integer v2)
convenience method for retrieving the sum. A parameter value of null is treated as 0.


add

public static java.lang.Integer add(java.lang.Integer v1,
                                    int v2)
convenience method for retrieving the sum. A parameter value of null is treated as 0.


subtract

public static java.lang.Integer subtract(java.lang.Integer v1,
                                         java.lang.Integer v2)
convenience method for retrieving the difference. A parameter value of null is treated as 0.


subtract

public static java.lang.Integer subtract(java.lang.Integer v1,
                                         int v2)
convenience method for retrieving the difference. A parameter value of null is treated as 0.


subtract

public static java.lang.Integer subtract(int v1,
                                         java.lang.Integer v2)
convenience method for retrieving the difference. A parameter value of null is treated as 0.


mod

public static java.lang.Integer mod(int a,
                                    int b)
an implementation of the mathematically typical - as opposed to the Java - implementation of the modulus funtion. For "a mod b", this implementation returns a number c such that
This differs from the usual Java implementation of a % b which returns negative numbers for negative a.


mod

public static java.lang.Integer mod(java.lang.Integer a,
                                    int b)
an implementation of the mathematically typical - as opposed to the Java - implementation of the modulus funtion. For "a mod b", this implementation returns a number c such that
This differs from the usual Java implementation of a % b which returns negative numbers for negative a.


mod

public static java.lang.Integer mod(int a,
                                    java.lang.Integer b)
an implementation of the mathematically typical - as opposed to the Java - implementation of the modulus funtion. For "a mod b", this implementation returns a number c such that
This differs from the usual Java implementation of a % b which returns negative numbers for negative a.


mod

public static java.lang.Integer mod(java.lang.Integer a,
                                    java.lang.Integer b)
an implementation of the mathematically typical - as opposed to the Java - implementation of the modulus funtion. For "a mod b", this implementation returns a number c such that
This differs from the usual Java implementation of a % b which returns negative numbers for negative a.