001package Torello.Java.Additional; 002 003/** 004 * An extremely basic <CODE>'Counter'</CODE> class that is used to help alleviate Java's 005 * stringent <CODE>'Effectively Final'</CODE> requirement for lambda-expressions. 006 * 007 * <EMBED CLASS='external-html' DATA-FILE-ID=LAMBDA_NOTE> 008 * <EMBED CLASS='external-html' DATA-FILE-ID=COUNTER> 009 */ 010public class Counter 011{ 012 private int counter; 013 014 /** 015 * Creates a new counter instance, and initializes it's internal-private 016 * {@code int counter} field to {@code '0'}. 017 */ 018 public Counter() 019 { this.counter = 0; } 020 021 /** 022 * Creates a new counter object, and initializes it's value to the passed-parameter 023 * {@code int startingValue} 024 * 025 * @param startingValue The with which to initialize the internal-private 026 * {@code int counter} field. 027 */ 028 public Counter(int startingValue) 029 { this.counter = startingValue; } 030 031 /** 032 * Increments the internal-private {@code int counter} field by a value of 1. 033 * @return The value stored in the counter, after performing the addition. 034 */ 035 public int addOne() 036 { return ++counter; } 037 038 /** 039 * Decrements the internal-private {@code int counter} field by a value of 1. 040 * @return The value stored in the counter, after performing the subtraction. 041 */ 042 public int subtractOne() 043 { return counter--; } 044 045 /** 046 * Adds the input-parameter 'n' value to the current value of the internal-private 047 * {@code int counter} field. 048 * @return The value stored in the counter, after performing the addition. 049 */ 050 public int modifyBy(int n) 051 { return counter += n; } 052 053 /** 054 * Sets the value of the internal-private {@code int counter} field to the value of the input 055 * passed-parameter {@code 'n'}. 056 * @param n The internal counter will be set to this value. 057 */ 058 public void set(int n) 059 { counter = n; } 060 061 /** 062 * Retrieves the value of the internal-private {@code int counter} field. 063 * @return The current value stored in the counter. 064 */ 065 public int size() 066 { return counter; } 067 068 /** 069 * Retrieves the value of the internal-private {@code int counter} field, as a java 070 * {@code java.lang.Integer} instance-object. 071 * @return The current value stored in the counter, as an instantiated Integer-instance. 072 */ 073 public Integer get() 074 { return Integer.valueOf(counter); } 075}