Class Counter


  • public class Counter
    extends java.lang.Object
    Counter - Documentation.

    The primary impetus for adding this class is rooted in a requirement made by java that in a lambda-expression, all variables must be declared final, or be effectively final. Inside of a Lambda Expression, using this class Counter instead of a Java Primitive int, long, short etc... allows a programmer to do many "integer-like" operations - most important of which is counting - inside of lambdas for tasks such as 'array-indexing' or 'running-total.' Such operations are impossible due to the aforementioned effectively final requirement made by the Java Compiler on variables used inside lambda-expressions. The example below shows a 'Lambda Expression' which would not work using a primitive {@code 'int'} type.

    Example:
    1
    2
    3
    Counter counter = new Counter();
    iuSource.forEach (url -> counter.addOne()); // an 'int counter' would generate a compile-time error
    size = counter.size();
    




    • Constructor Detail

      • Counter

        public Counter()
        Creates a new counter instance, and initializes it's internal-private int counter field to '0'.
      • Counter

        public Counter​(int startingValue)
        Creates a new counter object, and initializes it's value to the passed-parameter int startingValue
        Parameters:
        startingValue - The with which to initialize the internal-private int counter field.
    • Method Detail

      • addOne

        public int addOne()
        Increments the internal-private int counter field by a value of 1.
        Returns:
        The value stored in the counter, after performing the addition.
        Code:
        Exact Method Body:
        1
         return ++counter;
        
      • subtractOne

        public int subtractOne()
        Decrements the internal-private int counter field by a value of 1.
        Returns:
        The value stored in the counter, after performing the subtraction.
        Code:
        Exact Method Body:
        1
         return counter--;
        
      • modifyBy

        public int modifyBy​(int n)
        Adds the input-parameter 'n' value to the current value of the internal-private int counter field.
        Returns:
        The value stored in the counter, after performing the addition.
        Code:
        Exact Method Body:
        1
         return counter += n;
        
      • set

        public void set​(int n)
        Sets the value of the internal-private int counter field to the value of the input passed-parameter 'n'.
        Parameters:
        n - The internal counter will be set to this value.
        Code:
        Exact Method Body:
        1
         counter = n;
        
      • size

        public int size()
        Retrieves the value of the internal-private int counter field.
        Returns:
        The current value stored in the counter.
        Code:
        Exact Method Body:
        1
         return counter;
        
      • get

        public java.lang.Integer get()
        Retrieves the value of the internal-private int counter field, as a java java.lang.Integer instance-object.
        Returns:
        The current value stored in the counter, as an instantiated Integer-instance.
        Code:
        Exact Method Body:
        1
         return Integer.valueOf(counter);