Implement ConstantArrayCollection.toString() 30/26830/4
authorRobert Varga <rovarga@cisco.com>
Fri, 11 Sep 2015 10:29:15 +0000 (12:29 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Mon, 14 Sep 2015 07:48:32 +0000 (07:48 +0000)
Missing toString() breaks places which rely on collections properly
formatting their elements. Add an implementation and provide a simple
test suite. Also add hashCode()/equals().

Change-Id: I09ce7ecf2c541ab546e9b2a6f9916f0e76d4a511
Signed-off-by: Robert Varga <rovarga@cisco.com>
common/util/src/main/java/org/opendaylight/yangtools/util/ConstantArrayCollection.java
common/util/src/test/java/org/opendaylight/yangtools/util/ConstantArrayCollectionTest.java [new file with mode: 0644]

index f7dbdb819b382eb7637b9f47e17a5b65ba6692f7..8c29d9e0b9949e716bd05190d3ba20260c232bd0 100644 (file)
@@ -130,4 +130,42 @@ final class ConstantArrayCollection<E> implements Collection<E>, Serializable {
     public void clear() {
         throw new UnsupportedOperationException();
     }
+
+    @Override
+    public int hashCode() {
+        int result = 1;
+        for (E e : array) {
+            result = 31 * result + e.hashCode();
+        }
+        return result;
+    }
+
+    @Override
+    public boolean equals(final Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (obj == null) {
+            return false;
+        }
+        if (!(obj instanceof ConstantArrayCollection)) {
+            return false;
+        }
+
+        return Arrays.equals(array, ((ConstantArrayCollection<?>) obj).array);
+    }
+
+    @Override
+    public String toString() {
+        if (array.length == 0) {
+            return "[]";
+        }
+
+        StringBuilder sb = new StringBuilder("[");
+        int i = 0;
+        while (i < array.length - 1) {
+            sb.append(String.valueOf(array[i++])).append(", ");
+        }
+        return sb.append(String.valueOf(array[i])).append(']').toString();
+    }
 }
diff --git a/common/util/src/test/java/org/opendaylight/yangtools/util/ConstantArrayCollectionTest.java b/common/util/src/test/java/org/opendaylight/yangtools/util/ConstantArrayCollectionTest.java
new file mode 100644 (file)
index 0000000..b67d751
--- /dev/null
@@ -0,0 +1,101 @@
+/*
+ * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.util;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.Iterables;
+import java.util.Collection;
+import java.util.Collections;
+import org.junit.Test;
+
+public class ConstantArrayCollectionTest {
+    private static final String[] ARRAY = new String[] { "a", "bb", "ccc" };
+    private static final Collection<String> REF = ImmutableList.copyOf(ARRAY);
+
+    private static Collection<String> create() {
+        return new ConstantArrayCollection<>(ARRAY.clone());
+    }
+
+    @Test
+    public void testToString() {
+        // Empty
+        assertEquals(Collections.emptySet().toString(), new ConstantArrayCollection<>(new Object[0]).toString());
+
+        // Normal
+        assertEquals(REF.toString(), create().toString());
+    }
+
+    @Test
+    public void testEquals() {
+        final Collection<?> c = create();
+
+        assertTrue(c.containsAll(REF));
+        assertTrue(REF.containsAll(c));
+        assertTrue(Iterables.elementsEqual(REF, c));
+    }
+
+    @Test
+    public void testSimpleOperations() {
+        final Collection<?> c = create();
+
+        assertEquals(ARRAY.length, c.size());
+        assertFalse(c.isEmpty());
+        assertTrue(c.contains("ccc"));
+        assertFalse(c.contains(""));
+        assertFalse(c.contains(1));
+
+        assertTrue(c.containsAll(Collections.<String>emptyList()));
+        assertFalse(c.containsAll(Collections.singleton("")));
+        assertFalse(c.containsAll(Collections.singleton(1)));
+    }
+
+    @Test
+    public void testProtection() {
+        final Collection<?> c = create();
+
+        try {
+            c.add(null);
+            fail();
+        } catch (UnsupportedOperationException e) {
+        }
+
+        try {
+            c.remove(null);
+            fail();
+        } catch (UnsupportedOperationException e) {
+        }
+
+        try {
+            c.addAll(null);
+            fail();
+        } catch (UnsupportedOperationException e) {
+        }
+
+        try {
+            c.removeAll(null);
+            fail();
+        } catch (UnsupportedOperationException e) {
+        }
+
+        try {
+            c.retainAll(null);
+            fail();
+        } catch (UnsupportedOperationException e) {
+        }
+
+        try {
+            c.clear();
+            fail();
+        } catch (UnsupportedOperationException e) {
+        }
+    }
+}