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();
+ }
}
--- /dev/null
+/*
+ * 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) {
+ }
+ }
+}