ItEr14S08ModeladoTempoItEr13S09: TimeQuantity now implements equals based on its values.
This commit is contained in:
parent
b546bda82a
commit
f515e57537
2 changed files with 44 additions and 3 deletions
|
|
@ -17,7 +17,7 @@ public class TimeQuantity {
|
|||
Granularity.class);
|
||||
for (Granularity granularity : Granularity.values()) {
|
||||
Integer acc = sumAll(granularity, values);
|
||||
if (acc != null) {
|
||||
if (acc != null && acc != 0) {
|
||||
result.put(granularity, acc);
|
||||
}
|
||||
}
|
||||
|
|
@ -30,7 +30,7 @@ public class TimeQuantity {
|
|||
for (EnumMap<Granularity, Integer> enumMap : maps) {
|
||||
if (enumMap.containsKey(granularity)) {
|
||||
Integer valueToAdd = enumMap.get(granularity);
|
||||
result = result == null ? enumMap.get(granularity) : result
|
||||
result = result == null ? valueToAdd : result
|
||||
+ valueToAdd;
|
||||
}
|
||||
}
|
||||
|
|
@ -46,7 +46,11 @@ public class TimeQuantity {
|
|||
if (result.containsKey(granularity)) {
|
||||
newQuantity += result.get(granularity);
|
||||
}
|
||||
result.put(granularity, newQuantity);
|
||||
if (newQuantity != 0) {
|
||||
result.put(granularity, newQuantity);
|
||||
} else {
|
||||
result.remove(granularity);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
|
|
@ -60,6 +64,20 @@ public class TimeQuantity {
|
|||
this(new EnumMap<Granularity, Integer>(Granularity.class));
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (obj instanceof TimeQuantity) {
|
||||
TimeQuantity other = (TimeQuantity) obj;
|
||||
return values.equals(other.values);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return values.hashCode();
|
||||
}
|
||||
|
||||
private TimeQuantity(EnumMap<Granularity, Integer> enumMap) {
|
||||
Validate.notNull(enumMap);
|
||||
this.values = enumMap;
|
||||
|
|
|
|||
|
|
@ -1,6 +1,8 @@
|
|||
package org.navalplanner.business.common.test.partialtime;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.equalTo;
|
||||
import static org.hamcrest.CoreMatchers.not;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotSame;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
|
|
@ -90,6 +92,27 @@ public class TimeQuantityTest {
|
|||
assertThat(quantity.valueFor(Granularity.YEAR), equalTo(0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void twoTimeQuantitiesAreEqualsIfHaveTheSameValues() {
|
||||
assertEquals(TimeQuantity.empty(), TimeQuantity.empty());
|
||||
assertEquals(TimeQuantity.empty().plus(3, Granularity.HOUR),
|
||||
TimeQuantity.empty().plus(3, Granularity.HOUR));
|
||||
assertEquals(TimeQuantity.empty().plus(3, Granularity.HOUR),
|
||||
TimeQuantity.empty().plus(2, Granularity.HOUR).plus(1,
|
||||
Granularity.HOUR));
|
||||
assertThat(TimeQuantity.empty().plus(2, Granularity.DAY),
|
||||
not(equalTo(TimeQuantity.empty().plus(1, Granularity.DAY))));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsWorksWellWithZeroValues() {
|
||||
TimeQuantity quantity = TimeQuantity.empty().plus(2, Granularity.MONTH)
|
||||
.plus(-2, Granularity.MONTH);
|
||||
assertEquals(quantity.hashCode(), TimeQuantity.empty().hashCode());
|
||||
assertEquals(quantity, TimeQuantity.empty());
|
||||
assertEquals(quantity, TimeQuantity.empty().plus(0, Granularity.MONTH));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void theGreatestGranularitySpecifiedCanBeKnown() {
|
||||
TimeQuantity timeQuantity = TimeQuantity.empty().plus(2,
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue