Package Torello.Java.Additional
Class Counter
- java.lang.Object
-
- Torello.Java.Additional.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 aLambda Expression
, using thisclass Counter
instead of a Java Primitiveint, 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 aforementionedeffectively 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();
Hi-Lited Source-Code:- View Here: Torello/Java/Additional/Counter.java
- Open New Browser-Tab: Torello/Java/Additional/Counter.java
File Size: 2,419 Bytes Line Count: 74 '\n' Characters Found
-
-
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()
-
-
-
Constructor Detail
-
Counter
public Counter()
Creates a new counter instance, and initializes it's internal-privateint counter
field to'0'
.
-
Counter
public Counter(int startingValue)
Creates a new counter object, and initializes it's value to the passed-parameterint startingValue
- Parameters:
startingValue
- The with which to initialize the internal-privateint counter
field.
-
-
Method Detail
-
addOne
public int addOne()
Increments the internal-privateint 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-privateint 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-privateint 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-privateint 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-privateint 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-privateint counter
field, as a javajava.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);
-
-