diff --git a/Mage/src/main/java/mage/counters/Counter.java b/Mage/src/main/java/mage/counters/Counter.java index f3a125ec28..7a9c16e5b5 100644 --- a/Mage/src/main/java/mage/counters/Counter.java +++ b/Mage/src/main/java/mage/counters/Counter.java @@ -28,64 +28,145 @@ package mage.counters; +import org.apache.log4j.Logger; + import java.io.Serializable; /** - * * @author BetaSteward_at_googlemail.com */ public class Counter implements Serializable { - protected String name; + private static final Logger logger = Logger.getLogger(Counter.class); + + protected final String name; protected int count; - public Counter(String name) { + + /** + * Creates a {@link Counter} with the provided {@code name} and a default value of 1 + * + * @param name the name of this counter. + */ + public Counter(final String name) { this.name = name; this.count = 1; } - public Counter(String name, int count) { + + /** + * Creates a {@link Counter} with the provided {@code name} and {@code count} + * + * @param name the name of this counter. + * @param count the value of this counter. + */ + public Counter(final String name, final int count) { this.name = name; this.count = count; } + + /** + * Creates a {@link Counter} from an existing {@link Counter} object. + * + * @param counter the {@link Counter} to create a copy from. + */ public Counter(final Counter counter) { this.name = counter.name; this.count = counter.count; } - public void add() { + /** + * Increases the {@code count} by 1 + */ + public void increase() { count++; } + /** + * Adds the passed in {@code amount} to the {@code count} + * + * @param amount the value to add to the {@code count} + */ public void add(int amount) { count += amount; } - public void remove() { + + /** + * Decreases the {@code count} by one. Will not allow the count to be less than 0. + * If an attempt is made to make the count be less than zero, the call will be logged. + */ + public void decrease() { if (count > 0) { count--; + } else { + logger.warn("An attempt was made to set the counter '" + name + + "' to less than 0. Setting to 0."); } } + + /** + * Decreases the {@code count} by tne passed in {@code amount}. Will not allow the count + * to be less than 0. If an attempt is made to make the count be less than zero, the call will be logged. + */ public void remove(int amount) { if (count > amount) { count -= amount; - } - else { + } else { + logger.warn("An attempt was made to set the counter '" + name + + "' to less than 0. Setting to 0."); count = 0; } } + + /** + * Returns the name of this {@link Counter} + * + * @return the name of this {@link Counter} + */ public String getName() { return name; } + /** + * Returns the count of this {@link Counter} + * + * @return the count of this {@link Counter} + */ public int getCount() { return count; } + /** + * Returns a deep copy of this object. + * + * @return a deep copy of this object. + */ public Counter copy() { return new Counter(this); } + + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + + Counter counter = (Counter) o; + + if (count != counter.count) return false; + return !(name != null ? !name.equals(counter.name) : counter.name != null); + + } + + + @Override + public int hashCode() { + int result = name != null ? name.hashCode() : 0; + result = 31 * result + count; + return result; + } } diff --git a/Mage/src/main/java/mage/counters/Counters.java b/Mage/src/main/java/mage/counters/Counters.java index 473106f081..21842e5978 100644 --- a/Mage/src/main/java/mage/counters/Counters.java +++ b/Mage/src/main/java/mage/counters/Counters.java @@ -56,7 +56,7 @@ public class Counters extends HashMap<String, Counter> implements Serializable { if (!this.containsKey(name)) { this.put(name, new Counter(name)); } - this.get(name).add(); + this.get(name).increase(); } public void addCounter(String name, int amount) { diff --git a/Mage/src/test/java/mage/counters/CounterTest.java b/Mage/src/test/java/mage/counters/CounterTest.java new file mode 100644 index 0000000000..77bb39cf01 --- /dev/null +++ b/Mage/src/test/java/mage/counters/CounterTest.java @@ -0,0 +1,135 @@ +package mage.counters; + +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.*; + +/** + * Custom unit tests for {@link Counter} + */ +public class CounterTest { + + private Counter counter; + + @Before + public void setUp() { + counter = new Counter("test", 1); + } + + + @Test + public void shouldIncreaseCounter() { + // given + + // when + counter.increase(); + + // then + assertEquals(2, counter.getCount()); + assertEquals("test", counter.getName()); + } + + + @Test + public void shouldAddMana() { + // given + + // when + counter.add(5); + + // then + assertEquals(6, counter.getCount()); + } + + + @Test + public void shouldDecreaseCounter() { + // given + + + // when + counter.decrease(); + + // then + assertEquals(0, counter.getCount()); + } + + + @Test + public void shouldNotDecreaseToLessThanZero() { + // given + + // when + counter.decrease(); + counter.decrease(); + + // then + assertEquals(0, counter.getCount()); + } + + + @Test + public void shouldRemoveCounters() { + // given + + + // when + counter.remove(1); + + // then + assertEquals(0, counter.getCount()); + } + + + @Test + public void shouldNotRemoveMoreCountersThanAvailable() { + // given + + // when + counter.remove(10); + + // then + assertEquals(0, counter.getCount()); + } + + + @Test + public void shouldReturnCopy() { + // given + + // when + Counter copy = counter.copy(); + + // then + assertEquals(copy, counter); + assertFalse(copy == counter); + } + + + @Test + public void shouldCreateCounterFromCounter() { + // given + + // when + Counter copy = new Counter(counter); + + // then + assertEquals(1, copy.getCount()); + assertEquals("test", copy.getName()); + } + + + @Test + public void shouldCreatDefaultCounter() { + // given + + + // when + Counter defaultCounter = new Counter("default"); + + // then + assertEquals(1, defaultCounter.getCount()); + assertEquals("default", defaultCounter.getName()); + } +} \ No newline at end of file