From: Robert Varga Date: Fri, 11 Sep 2015 10:29:15 +0000 (+0200) Subject: Implement ConstantArrayCollection.toString() X-Git-Tag: release/beryllium~326 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=92180b8be60ca2d023ed5ec9477246ce0c1387c9;p=yangtools.git Implement ConstantArrayCollection.toString() 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 --- diff --git a/common/util/src/main/java/org/opendaylight/yangtools/util/ConstantArrayCollection.java b/common/util/src/main/java/org/opendaylight/yangtools/util/ConstantArrayCollection.java index f7dbdb819b..8c29d9e0b9 100644 --- a/common/util/src/main/java/org/opendaylight/yangtools/util/ConstantArrayCollection.java +++ b/common/util/src/main/java/org/opendaylight/yangtools/util/ConstantArrayCollection.java @@ -130,4 +130,42 @@ final class ConstantArrayCollection implements Collection, 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 index 0000000000..b67d751b20 --- /dev/null +++ b/common/util/src/test/java/org/opendaylight/yangtools/util/ConstantArrayCollectionTest.java @@ -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 REF = ImmutableList.copyOf(ARRAY); + + private static Collection 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.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) { + } + } +}