diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Match.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Match.java index 804514cb14..5f707d62ed 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Match.java +++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Match.java @@ -108,12 +108,13 @@ public final class Match { return Objects.hash(matchAny, value); } + @SuppressWarnings("unchecked") @Override public boolean equals(Object other) { if (!(other instanceof Match)) { return false; } - Match that = (Match) other; + Match that = (Match) other; return Objects.equals(this.matchAny, that.matchAny) && Objects.equals(this.value, that.value); } diff --git a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Result.java b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Result.java index 548174a0e5..856f706df5 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Result.java +++ b/core/store/dist/src/main/java/org/onosproject/store/consistent/impl/Result.java @@ -15,6 +15,10 @@ */ package org.onosproject.store.consistent.impl; +import static com.google.common.base.MoreObjects.toStringHelper; + +import java.util.Objects; + /** * Result of a database update operation. * @@ -74,6 +78,7 @@ public final class Result { /** * Returns the status of database update operation. + * * @return database update status */ public Status status() { @@ -82,10 +87,35 @@ public final class Result { /** * Returns the return value for the update. + * * @return value returned by database update. If the status is another * other than Status.OK, this returns a null */ public V value() { return value; } + + @Override + public int hashCode() { + return Objects.hash(value, status); + } + + @SuppressWarnings("unchecked") + @Override + public boolean equals(Object other) { + if (!(other instanceof Result)) { + return false; + } + Result that = (Result) other; + return Objects.equals(this.value, that.value) && + Objects.equals(this.status, that.status); + } + + @Override + public String toString() { + return toStringHelper(this) + .add("status", status) + .add("value", value) + .toString(); + } } \ No newline at end of file diff --git a/core/store/dist/src/main/java/org/onosproject/store/ecmap/EventuallyConsistentMapImpl.java b/core/store/dist/src/main/java/org/onosproject/store/ecmap/EventuallyConsistentMapImpl.java index 2de8947e9a..b56d74b6d8 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/ecmap/EventuallyConsistentMapImpl.java +++ b/core/store/dist/src/main/java/org/onosproject/store/ecmap/EventuallyConsistentMapImpl.java @@ -653,7 +653,7 @@ public class EventuallyConsistentMapImpl public void processItems(List> items) { Map> map = Maps.newHashMap(); items.forEach(item -> map.compute(item.key(), (key, existing) -> - existing == null || item.compareTo(existing) > 0 ? item : existing)); + item.isNewerThan(existing) ? item : existing)); communicationExecutor.submit(() -> { clusterCommunicator.unicast(ImmutableList.copyOf(map.values()), updateMessageSubject, diff --git a/core/store/dist/src/main/java/org/onosproject/store/ecmap/MapValue.java b/core/store/dist/src/main/java/org/onosproject/store/ecmap/MapValue.java index 1a89c6bd93..bb69b4726d 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/ecmap/MapValue.java +++ b/core/store/dist/src/main/java/org/onosproject/store/ecmap/MapValue.java @@ -83,10 +83,11 @@ public class MapValue implements Comparable> { return Objects.hashCode(timestamp, value); } + @SuppressWarnings("unchecked") @Override public boolean equals(Object other) { if (other instanceof MapValue) { - MapValue that = (MapValue) other; + MapValue that = (MapValue) other; return Objects.equal(this.timestamp, that.timestamp) && Objects.equal(this.value, that.value); } diff --git a/core/store/dist/src/main/java/org/onosproject/store/ecmap/UpdateEntry.java b/core/store/dist/src/main/java/org/onosproject/store/ecmap/UpdateEntry.java index 41eb3a23c1..fcf6198407 100644 --- a/core/store/dist/src/main/java/org/onosproject/store/ecmap/UpdateEntry.java +++ b/core/store/dist/src/main/java/org/onosproject/store/ecmap/UpdateEntry.java @@ -22,7 +22,7 @@ import com.google.common.base.MoreObjects; /** * Describes a single update event in an EventuallyConsistentMap. */ -final class UpdateEntry implements Comparable> { +final class UpdateEntry { private final K key; private final MapValue value; @@ -55,9 +55,13 @@ final class UpdateEntry implements Comparable> { return value; } - @Override - public int compareTo(UpdateEntry o) { - return this.value.timestamp().compareTo(o.value.timestamp()); + /** + * Returns if this entry is newer than other entry. + * @param other other entry + * @return true if this entry is newer; false otherwise + */ + public boolean isNewerThan(UpdateEntry other) { + return other == null || value.isNewerThan(other.value); } @Override diff --git a/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/ResultTest.java b/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/ResultTest.java new file mode 100644 index 0000000000..2a3bab8730 --- /dev/null +++ b/core/store/dist/src/test/java/org/onosproject/store/consistent/impl/ResultTest.java @@ -0,0 +1,42 @@ +package org.onosproject.store.consistent.impl; + +import static junit.framework.TestCase.assertEquals; +import static junit.framework.TestCase.assertFalse; +import static junit.framework.TestCase.assertNull; +import static junit.framework.TestCase.assertTrue; + +import org.junit.Test; + +/** + * Unit tests for Result. + */ +public class ResultTest { + + @Test + public void testLocked() { + Result r = Result.locked(); + assertFalse(r.success()); + assertNull(r.value()); + assertEquals(Result.Status.LOCKED, r.status()); + } + + @Test + public void testOk() { + Result r = Result.ok("foo"); + assertTrue(r.success()); + assertEquals("foo", r.value()); + assertEquals(Result.Status.OK, r.status()); + } + + @Test + public void testEquality() { + Result r1 = Result.ok("foo"); + Result r2 = Result.locked(); + Result r3 = Result.ok("bar"); + Result r4 = Result.ok("foo"); + assertTrue(r1.equals(r4)); + assertFalse(r1.equals(r2)); + assertFalse(r1.equals(r3)); + assertFalse(r2.equals(r3)); + } +}