Class Counter


  • public class Counter
    extends java.lang.Object
    An extremely basic 'Counter' class that is used to help alleviate Java's stringent 'Effectively Final' requirement for lambda-expressions.

    Lambda-Expression Helper Class

    Java's Function-Pointer syntax (Lambda Exression) can be an invaluable way to create simple methods/functions without having to write the full-blown syntax for a Java Method. Lambda-Expressions, however prohibit variables from changing their value inside the expression This wrapper-class can help alleviate some of the complexities that may crop up from time to time.
    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:
    Counter counter = new Counter();
    iuSource.forEach (url -> counter.addOne()); // an 'int counter' would generate a compile-time error
    size = counter.size();
    


    • Constructor Summary

      Constructors 
      Constructor Description
      Counter()
      Creates a new counter instance, and initializes it's internal-private int counter field to '0'.
      Counter​(int startingValue)
      Creates a new counter object, and initializes it's value to the passed-parameter int startingValue
    • Method Summary

       
      Modify Internal Counter & Return Value
      Modifier and Type Method
      int addOne()
      int modifyBy​(int n)
      int subtractOne()
       
      Set Internal Counter's Value
      Modifier and Type Method
      void set​(int n)
       
      Retrieve Internal Counter's Value, as a java.lang.Integer
      Modifier and Type Method
      Integer get()
       
      Retrieve Internal Counter's Value, as an int
      Modifier and Type Method
      int size()
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • 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:
         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:
         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:
         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:
         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:
         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:
         return Integer.valueOf(counter);