package test3; /** * This class represents a counter. Each counter has a count. * * @author Franck van Breugel */ public class Counter { private long count; /** * Initializes this counter with the given count. * * @param count the count of this counter. * @pre. count >= 0 */ private Counter(long count) { this.setCount(count); } /** * Returns the count of this counter. * * @return the count of this counter. */ public long getCount() { return this.count; } /** * Sets the count of this counter to the given count. * * @param count the new count of this counter. * @pre. count >= 0 */ private void setCount(long count) { this.count = count; } /** * Increments the count of this counter. */ public void increment() { this.setCount(this.getCount() + 1); } /** * Resets the counter of this counter to 0. */ public void reset() { this.setCount(0); } /** * Compares this counter with the given other counter. * Returns a negative integer/zero/positive integer if * the count of this counter is smaller than/equal/greater * than the counter of the other counter. * * @param other a counter. * @return a negative integer/zero/positive integer if * the count of this counter is smaller than/equal/greater * than the counter of the other counter. */ public int compareTo(Counter other) { return (int) (this.getCount() - other.getCount()); } /** * Returns a copy of the given counter if that counter is * not null. Otherwise, a counter with count 0 is returned. * * @param counter counter to be copied. * @return a copy of the given counter if counter != null, * a counter with count 0 otherwise. */ public static Counter copy(Counter counter) { Counter copy; if (counter == null) { copy = new Counter(0); } else { copy = new Counter(counter.getCount()); } return copy; } }