BUG-4803: fix equals() method
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / ImmutableOffsetMap.java
index 02d8fe4d4ac0c7f5f2a123352ffc2a4fb7979d52..df0479ed9f72dc30d342fbb118115d42b57ba08f 100644 (file)
@@ -94,13 +94,12 @@ public final class ImmutableOffsetMap<K, V> implements UnmodifiableMapPhase<K, V
             return ImmutableMap.of();
         }
         if (size == 1) {
-            // Efficient single-entry implementation.
+            // Efficient single-entry implementation
             final Entry<K, V> e = m.entrySet().iterator().next();
             return SharedSingletonMap.of(e.getKey(), e.getValue());
         }
 
-        // copyOf() disconnects the key set while retaining its order across cache lookup.
-        final Map<K, Integer> offsets = OffsetMapCache.orderedOffsets(m.keySet());
+        final Map<K, Integer> offsets = OffsetMapCache.offsetsFor(m.keySet());
         @SuppressWarnings("unchecked")
         final V[] array = (V[]) new Object[offsets.size()];
         for (Entry<K, V> e : m.entrySet()) {
@@ -140,42 +139,42 @@ public final class ImmutableOffsetMap<K, V> implements UnmodifiableMapPhase<K, V
         if (o == this) {
             return true;
         }
-        if (o == null) {
+        if (!(o instanceof Map)) {
             return false;
         }
 
         if (o instanceof ImmutableOffsetMap) {
             final ImmutableOffsetMap<?, ?> om = (ImmutableOffsetMap<?, ?>) o;
-            if (offsets.equals(om.offsets) && Arrays.deepEquals(objects, om.objects)) {
-                return true;
+
+            // If the offset match, the arrays have to match, too
+            if (offsets.equals(om.offsets)) {
+                return Arrays.deepEquals(objects, om.objects);
             }
         } else if (o instanceof MutableOffsetMap) {
             // Let MutableOffsetMap do the actual work.
             return o.equals(this);
-        } else if (o instanceof Map) {
-            final Map<?, ?> om = (Map<?, ?>)o;
+        }
 
-            // Size and key sets have to match
-            if (size() != om.size() || !keySet().equals(om.keySet())) {
-                return false;
-            }
+        final Map<?, ?> other = (Map<?, ?>)o;
+
+        // Size and key sets have to match
+        if (size() != other.size() || !keySet().equals(other.keySet())) {
+            return false;
+        }
 
-            try {
-                // Ensure all objects are present
-                for (Entry<K, Integer> e : offsets.entrySet()) {
-                    if (!objects[e.getValue()].equals(om.get(e.getKey()))) {
-                        return false;
-                    }
+        try {
+            // Ensure all objects are present
+            for (Entry<K, Integer> e : offsets.entrySet()) {
+                if (!objects[e.getValue()].equals(other.get(e.getKey()))) {
+                    return false;
                 }
-            } catch (ClassCastException e) {
-                // Can be thrown by om.get() indicating we have incompatible key types
-                return false;
             }
-
-            return true;
+        } catch (ClassCastException e) {
+            // Can be thrown by other.get() indicating we have incompatible key types
+            return false;
         }
 
-        return false;
+        return true;
     }
 
     @Override
@@ -236,7 +235,7 @@ public final class ImmutableOffsetMap<K, V> implements UnmodifiableMapPhase<K, V
 
     @Override
     public MutableOffsetMap<K, V> toModifiableMap() {
-        return MutableOffsetMap.copyOf(this);
+        return new MutableOffsetMap<>(this);
     }
 
     @Override
@@ -325,7 +324,6 @@ public final class ImmutableOffsetMap<K, V> implements UnmodifiableMapPhase<K, V
     private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException {
         final int s = in.readInt();
 
-        // ImmutableList.build() does not have a sizing hint ...
         final List<K> keys = new ArrayList<>(s);
         final V[] values = (V[]) new Object[s];
 
@@ -334,7 +332,7 @@ public final class ImmutableOffsetMap<K, V> implements UnmodifiableMapPhase<K, V
             values[i] = (V)in.readObject();
         }
 
-        setField(OFFSETS_FIELD, OffsetMapCache.orderedOffsets(keys));
+        setField(OFFSETS_FIELD, OffsetMapCache.offsetsFor(keys));
         setField(ARRAY_FIELD, values);
     }
 }