1
0
Fork 0
mirror of https://github.com/correl/mage.git synced 2025-04-13 01:01:11 -09:00

Merge pull request from poixen/testfix

Updated mana class to pass failing tests
This commit is contained in:
poixen 2015-11-25 19:58:10 +01:00
commit 42c1915598
2 changed files with 72 additions and 23 deletions
Mage.Tests/src/test/java/org/mage/test/mana
Mage/src/mage

View file

@ -27,7 +27,7 @@ public class ManaTest {
public void shouldNotAllowNullCopyConstructor() { public void shouldNotAllowNullCopyConstructor() {
// given // given
expectedException.expect(NullPointerException.class); expectedException.expect(NullPointerException.class);
expectedException.expectMessage("The passed in Mana can not be null"); expectedException.expectMessage("The passed in mana can not be null");
// when // when
Mana nullMana = null; Mana nullMana = null;

View file

@ -28,12 +28,15 @@
package mage; package mage;
import java.io.Serializable; import java.io.Serializable;
import java.util.Objects;
import mage.constants.ColoredManaSymbol; import mage.constants.ColoredManaSymbol;
import mage.constants.ManaType; import mage.constants.ManaType;
import static mage.constants.ManaType.COLORLESS; import static mage.constants.ManaType.COLORLESS;
import mage.filter.FilterMana; import mage.filter.FilterMana;
import mage.util.Copyable; import mage.util.Copyable;
import mage.util.ThreadLocalStringBuilder; import mage.util.ThreadLocalStringBuilder;
import org.apache.log4j.Logger;
/** /**
* *
@ -41,6 +44,9 @@ import mage.util.ThreadLocalStringBuilder;
*/ */
public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> { public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
private static final transient Logger logger = Logger.getLogger(Mana.class);
protected int red; protected int red;
protected int green; protected int green;
protected int blue; protected int blue;
@ -61,6 +67,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public Mana(final Mana mana) { public Mana(final Mana mana) {
Objects.requireNonNull(mana, "The passed in mana can not be null");
this.red = mana.red; this.red = mana.red;
this.green = mana.green; this.green = mana.green;
this.blue = mana.blue; this.blue = mana.blue;
@ -71,7 +78,8 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
this.flag = mana.flag; this.flag = mana.flag;
} }
public Mana(ColoredManaSymbol color) { public Mana(final ColoredManaSymbol color) {
Objects.requireNonNull(color, "The passed in ColoredManaSymbol can not be null");
switch (color) { switch (color) {
case G: case G:
green = 1; green = 1;
@ -92,37 +100,37 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public static Mana RedMana(int num) { public static Mana RedMana(int num) {
return new Mana(num, 0, 0, 0, 0, 0, 0); return new Mana(notNegative(num, "Red"), 0, 0, 0, 0, 0, 0);
} }
public static Mana GreenMana(int num) { public static Mana GreenMana(int num) {
return new Mana(0, num, 0, 0, 0, 0, 0); return new Mana(0, notNegative(num, "Green"), 0, 0, 0, 0, 0);
} }
public static Mana BlueMana(int num) { public static Mana BlueMana(int num) {
return new Mana(0, 0, num, 0, 0, 0, 0); return new Mana(0, 0, notNegative(num, "Blue"), 0, 0, 0, 0);
} }
public static Mana WhiteMana(int num) { public static Mana WhiteMana(int num) {
return new Mana(0, 0, 0, num, 0, 0, 0); return new Mana(0, 0, 0, notNegative(num, "White"), 0, 0, 0);
} }
public static Mana BlackMana(int num) { public static Mana BlackMana(int num) {
return new Mana(0, 0, 0, 0, num, 0, 0); return new Mana(0, 0, 0, 0, notNegative(num, "Black"), 0, 0);
} }
public static Mana ColorlessMana(int num) { public static Mana ColorlessMana(int num) {
return new Mana(0, 0, 0, 0, 0, num, 0); return new Mana(0, 0, 0, 0, 0, notNegative(num, "Colorless"), 0);
} }
public Mana(int red, int green, int blue, int white, int black, int colorless, int any) { public Mana(int red, int green, int blue, int white, int black, int colorless, int any) {
this.red = red; this.red = notNegative(red, "Red");
this.green = green; this.green = notNegative(green, "Green");
this.blue = blue; this.blue = notNegative(blue, "Blue");
this.white = white; this.white = notNegative(white, "White");
this.black = black; this.black = notNegative(black, "Black");
this.colorless = colorless; this.colorless = notNegative(colorless, "Colorless");
this.any = any; this.any = notNegative(any, "Any");
} }
public void add(Mana mana) { public void add(Mana mana) {
@ -226,7 +234,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
colorless++; colorless++;
} }
if (oldColorless == colorless) { if (oldColorless == colorless) {
break; // to prevent endless loop -> should not be possible, but who knows throw new ArithmeticException("Not enough mana to pay colorless");
} }
} }
} }
@ -424,7 +432,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public void setRed(int red) { public void setRed(int red) {
this.red = red; this.red = notNegative(red, "Red");
} }
public int getGreen() { public int getGreen() {
@ -432,7 +440,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public void setGreen(int green) { public void setGreen(int green) {
this.green = green; this.green = notNegative(green, "Green");
} }
public int getBlue() { public int getBlue() {
@ -440,7 +448,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public void setBlue(int blue) { public void setBlue(int blue) {
this.blue = blue; this.blue = notNegative(blue, "Blue");
} }
public int getWhite() { public int getWhite() {
@ -448,7 +456,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public void setWhite(int white) { public void setWhite(int white) {
this.white = white; this.white = notNegative(white, "White");
} }
public int getBlack() { public int getBlack() {
@ -456,7 +464,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public void setBlack(int black) { public void setBlack(int black) {
this.black = black; this.black = notNegative(black, "Black");
} }
public int getColorless() { public int getColorless() {
@ -464,7 +472,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public void setColorless(int colorless) { public void setColorless(int colorless) {
this.colorless = colorless; this.colorless = notNegative(colorless, "Colorless");
} }
public int getAny() { public int getAny() {
@ -472,7 +480,7 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
public void setAny(int any) { public void setAny(int any) {
this.any = any; this.any = notNegative(any, "Any");
} }
@Override @Override
@ -665,4 +673,45 @@ public class Mana implements Comparable<Mana>, Serializable, Copyable<Mana> {
} }
return count; return count;
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Mana mana = (Mana) o;
if (red != mana.red) return false;
if (green != mana.green) return false;
if (blue != mana.blue) return false;
if (white != mana.white) return false;
if (black != mana.black) return false;
if (colorless != mana.colorless) return false;
if (any != mana.any) return false;
return flag == mana.flag;
}
@Override
public int hashCode() {
int result = red;
result = 31 * result + green;
result = 31 * result + blue;
result = 31 * result + white;
result = 31 * result + black;
result = 31 * result + colorless;
result = 31 * result + any;
result = 31 * result + (flag ? 1 : 0);
return result;
}
private static int notNegative(int value, final String name) {
if (value < 0) {
logger.info(name + " can not be less than 0. Passed in: " + value + " Defaulting to 0.");
value = 0;
}
return value;
}
} }