diff options
3 files changed, 84 insertions, 23 deletions
diff --git a/document/src/main/java/com/yahoo/document/datatypes/MapFieldValue.java b/document/src/main/java/com/yahoo/document/datatypes/MapFieldValue.java index 6d6c18755c1..bf40546d637 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/MapFieldValue.java +++ b/document/src/main/java/com/yahoo/document/datatypes/MapFieldValue.java @@ -10,7 +10,16 @@ import com.yahoo.document.serialization.FieldReader; import com.yahoo.document.serialization.FieldWriter; import com.yahoo.document.serialization.XmlSerializationHelper; import com.yahoo.document.serialization.XmlStream; -import java.util.*; + +import java.util.Arrays; +import java.util.Map; +import java.util.HashMap; +import java.util.Set; +import java.util.List; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashSet; + /** * Vespa map. Backed by and and parametrized by FieldValue @@ -72,10 +81,7 @@ public class MapFieldValue<K extends FieldValue, V extends FieldValue> extends C */ public boolean equals(Object o) { if (!(o instanceof MapFieldValue)) return false; - MapFieldValue otherSet = (MapFieldValue) o; - Map<K, V> map1 = values; - Map<K, V> map2 = otherSet.values; - return (super.equals(o) && map1.equals(map2)); + return super.equals(o) && values.equals(((MapFieldValue) o).values); } @Override @@ -276,14 +282,24 @@ public class MapFieldValue<K extends FieldValue, V extends FieldValue> extends C return comp; } //types are equal, this must be of this type - MapFieldValue otherValue = (MapFieldValue) fieldValue; - comp = CollectionComparator.compare(values.keySet(), otherValue.values.keySet()); - - if (comp != 0) { - return comp; + MapFieldValue<K,V> rhs = (MapFieldValue<K,V>) fieldValue; + if (size() < rhs.size()) { + return -1; + } else if (size() > rhs.size()) { + return 1; + } + Map.Entry<K,V> [] entries = entrySet().toArray(new Map.Entry[size()]); + Map.Entry<K,V> [] rhsEntries = rhs.entrySet().toArray(new Map.Entry[rhs.size()]); + Arrays.sort(entries, (Map.Entry<K,V> a, Map.Entry<K,V> b) -> { return a.getKey().compareTo(b.getKey()); }); + Arrays.sort(rhsEntries, (Map.Entry<K,V> a, Map.Entry<K,V> b) -> { return a.getKey().compareTo(b.getKey()); }); + for (int i = 0; i < entries.length; i++) { + comp = entries[i].getKey().compareTo(rhsEntries[i].getKey()); + if (comp != 0) return comp; + comp = entries[i].getValue().compareTo(rhsEntries[i].getValue()); + if (comp != 0) return comp; } - return CollectionComparator.compare(values.values(), otherValue.values.values()); + return 0; } /** diff --git a/document/src/main/java/com/yahoo/document/datatypes/WeightedSet.java b/document/src/main/java/com/yahoo/document/datatypes/WeightedSet.java index 0e4c56406f0..63dc1cab063 100644 --- a/document/src/main/java/com/yahoo/document/datatypes/WeightedSet.java +++ b/document/src/main/java/com/yahoo/document/datatypes/WeightedSet.java @@ -241,8 +241,7 @@ public final class WeightedSet<K extends FieldValue> extends CollectionFieldValu */ public boolean equals(Object o) { if (!(o instanceof WeightedSet)) return false; - WeightedSet otherSet = (WeightedSet) o; - return (super.equals(o) && map.equals(otherSet.map)); + return (super.equals(o) && map.equals(((WeightedSet<K>)o).map)); } /** @@ -293,15 +292,7 @@ public final class WeightedSet<K extends FieldValue> extends CollectionFieldValu return comp; } - //types are equal, this must be of this type - WeightedSet otherValue = (WeightedSet) fieldValue; - comp = CollectionComparator.compare(map.keySet(), otherValue.map.keySet()); - - if (comp != 0) { - return comp; - } - - return CollectionComparator.compare(map.values(), otherValue.map.values()); + return map.compareTo(((WeightedSet<K>)fieldValue).map); } diff --git a/document/src/test/java/com/yahoo/document/datatypes/WeightedSetTestCase.java b/document/src/test/java/com/yahoo/document/datatypes/WeightedSetTestCase.java index 3436c73feae..107da479f72 100644 --- a/document/src/test/java/com/yahoo/document/datatypes/WeightedSetTestCase.java +++ b/document/src/test/java/com/yahoo/document/datatypes/WeightedSetTestCase.java @@ -2,13 +2,13 @@ package com.yahoo.document.datatypes; import com.yahoo.document.DataType; -import com.yahoo.document.MapDataType; import org.junit.Test; import java.util.LinkedHashMap; import java.util.Map; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; @@ -21,6 +21,60 @@ import static org.junit.Assert.fail; public class WeightedSetTestCase { @Test + public void testEquals() { + WeightedSet<StringFieldValue> a = new WeightedSet<>(DataType.TAG); + a.put(new StringFieldValue("this is a test"), 5); + a.put(new StringFieldValue("this is a second test"), 7); + + WeightedSet<StringFieldValue> b = new WeightedSet<>(DataType.TAG); + b.put(new StringFieldValue("this is a second test"), 7); + b.put(new StringFieldValue("this is a test"), 5); + + assertEquals(a, b); + assertEquals(0, a.compareTo(b)); + assertEquals(0, b.compareTo(a)); + + } + + @Test + public void testCompareTo() { + WeightedSet<StringFieldValue> a = new WeightedSet<>(DataType.TAG); + a.put(new StringFieldValue("this is a test"), 5); + a.put(new StringFieldValue("this is a second test"), 7); + + WeightedSet<StringFieldValue> b = new WeightedSet<>(DataType.TAG); + b.put(new StringFieldValue("this is a test"), 5); + + assertNotEquals(a, b); + assertEquals(1, a.compareTo(b)); + assertEquals(-1, b.compareTo(a)); + + b.clear(); + b.put(new StringFieldValue("this is a test"), 5); + b.put(new StringFieldValue("this is a third test"), 7); + + assertNotEquals(a, b); + assertEquals(-1, a.compareTo(b)); + assertEquals(1, b.compareTo(a)); + + b.clear(); + b.put(new StringFieldValue("this is a test"), 5); + b.put(new StringFieldValue("this is a second test"), 7); + + assertEquals(a, b); + assertEquals(0, a.compareTo(b)); + assertEquals(0, b.compareTo(a)); + + b.clear(); + b.put(new StringFieldValue("this is a test"), 5); + b.put(new StringFieldValue("this is a second test"), 6); + + assertNotEquals(a, b); + assertEquals(1, a.compareTo(b)); + assertEquals(-1, b.compareTo(a)); + } + + @Test public void testSet() { WeightedSet<StringFieldValue> wset = new WeightedSet<>(DataType.TAG); |