mirror of
https://github.com/correl/mage.git
synced 2025-04-01 19:07:57 -09:00
Added unit tests and javadoc for Counter class.
This commit is contained in:
parent
afc523b7c7
commit
ad0fb6b7eb
3 changed files with 225 additions and 9 deletions
Mage/src
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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) {
|
||||
|
|
135
Mage/src/test/java/mage/counters/CounterTest.java
Normal file
135
Mage/src/test/java/mage/counters/CounterTest.java
Normal file
|
@ -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());
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue