|
|
@@ -39,11 +39,10 @@ public class Amount {
|
|
|
COUNT, WEIGHT, VOLUME;
|
|
|
}
|
|
|
|
|
|
- Unit type;
|
|
|
+ Unit unit;
|
|
|
double value;
|
|
|
Volume vol;
|
|
|
Weight wgt;
|
|
|
- String unit;
|
|
|
|
|
|
@JsonCreator
|
|
|
public Amount(final String serial) {
|
|
|
@@ -61,50 +60,45 @@ public class Amount {
|
|
|
wgt = rw.get();
|
|
|
return;
|
|
|
}
|
|
|
- unit = tokens[1];
|
|
|
}
|
|
|
|
|
|
@JsonValue @Override
|
|
|
public String toString() {
|
|
|
StringBuilder build = new StringBuilder();
|
|
|
build.append(value);
|
|
|
- build.append(' ');
|
|
|
- build.append(unitName());
|
|
|
+ if (unit != Unit.COUNT) {
|
|
|
+ build.append(' ');
|
|
|
+ build.append(unitName());
|
|
|
+ }
|
|
|
return build.toString();
|
|
|
}
|
|
|
|
|
|
private String unitName() {
|
|
|
- switch (type) {
|
|
|
- case COUNT:
|
|
|
- return unit;
|
|
|
+ switch (unit) {
|
|
|
case WEIGHT:
|
|
|
return wgt.displayName;
|
|
|
case VOLUME:
|
|
|
return vol.displayName;
|
|
|
default:
|
|
|
- throw new IllegalStateException("Unknown enum value: " + type);
|
|
|
+ return "";
|
|
|
}
|
|
|
}
|
|
|
|
|
|
public Amount plus(final Amount amount) {
|
|
|
- if (type != amount.type) {
|
|
|
- throw new IllegalArgumentException("Cannot merge mass and volume together");
|
|
|
- } else if (!unit.equals(amount.unit)) {
|
|
|
- throw new IllegalArgumentException("Cannot merge objects with different units");
|
|
|
+ if (unit != amount.unit) {
|
|
|
+ throw new IllegalArgumentException("Cannot merge mass/volume/count amounts together");
|
|
|
}
|
|
|
- return new Amount(type, value + amount.value * scale(amount), vol, wgt, unit);
|
|
|
+ return new Amount(unit, value + amount.value * scale(amount), vol, wgt);
|
|
|
}
|
|
|
|
|
|
private double scale(final Amount amount) {
|
|
|
- switch (type) {
|
|
|
- case COUNT:
|
|
|
- return 1.0;
|
|
|
+ switch (unit) {
|
|
|
case WEIGHT:
|
|
|
return amount.wgt.atomicUnits / wgt.atomicUnits;
|
|
|
case VOLUME:
|
|
|
return amount.vol.atomicUnits / vol.atomicUnits;
|
|
|
default:
|
|
|
- throw new IllegalStateException("Unknown enum value: " + type);
|
|
|
+ return 1.0;
|
|
|
}
|
|
|
}
|
|
|
}
|