Seal PathArgumentList 38/102038/4
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 10 Aug 2022 14:51:03 +0000 (16:51 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Tue, 4 Oct 2022 14:00:34 +0000 (16:00 +0200)
We have only two possible subclasses, make sure we use a sealed class
for that.

Change-Id: Ibb0297a660c0d61e766ee0fd8303500c44efd1dc
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
data/yang-data-api/src/main/java/org/opendaylight/yangtools/yang/data/api/PathArgumentList.java
data/yang-data-api/src/test/java/org/opendaylight/yangtools/yang/data/api/PathArgumentListTest.java

index e0ff756808a4509558f5e075295fed9402717a62..1f09eb463049a3c11d872e6efa7eb83e6c4afeec 100644 (file)
@@ -13,8 +13,8 @@ import java.util.Collection;
 import org.eclipse.jdt.annotation.NonNull;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 
-// FIXME: sealed once we have JDK17+
-abstract class PathArgumentList extends AbstractList<PathArgument> {
+abstract sealed class PathArgumentList extends AbstractList<PathArgument>
+        permits StackedPathArguments, StackedReversePathArguments {
     @Override
     public abstract @NonNull UnmodifiableIterator<PathArgument> iterator();
 
index adc0a62ee016b4caf31d60f4842a3591d12081f4..2140cdf6b27af2b0c5f55fd04dc4eaec9ac7107b 100644 (file)
@@ -13,62 +13,35 @@ import static org.junit.jupiter.api.Assertions.assertSame;
 import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
-import com.google.common.collect.UnmodifiableIterator;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
-import java.util.NoSuchElementException;
 import org.junit.jupiter.api.Test;
 import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.common.QNameModule;
 import org.opendaylight.yangtools.yang.common.Revision;
 import org.opendaylight.yangtools.yang.common.XMLNamespace;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
 
 public class PathArgumentListTest {
-    private static final class TestClass extends PathArgumentList {
-        @Override
-        public UnmodifiableIterator<PathArgument> iterator() {
-            return new UnmodifiableIterator<>() {
-                @Override
-                public boolean hasNext() {
-                    return false;
-                }
-
-                @Override
-                public PathArgument next() {
-                    throw new NoSuchElementException();
-                }
-            };
-        }
-
-        @Override
-        public PathArgument get(final int index) {
-            return null;
-        }
-
-        @Override
-        public int size() {
-            return 0;
-        }
-    }
+    private static final PathArgumentList LIST = new StackedPathArguments(YangInstanceIdentifier.empty(),
+        List.of(new NodeIdentifier(QName.create("foo", "foo"))));
 
     @Test
     public void testIsEmpty() {
-        assertFalse(new TestClass().isEmpty());
+        assertFalse(LIST.isEmpty());
     }
 
     @Test
     public void testProtections() {
-        final PathArgumentList l = new TestClass();
-
-        assertThrows(UnsupportedOperationException.class, () -> l.remove(null));
-        assertThrows(UnsupportedOperationException.class, () -> l.addAll(List.of()));
-        assertThrows(UnsupportedOperationException.class, () -> l.removeAll(List.of()));
-        assertThrows(UnsupportedOperationException.class, () -> l.retainAll(List.of()));
-        assertThrows(UnsupportedOperationException.class, () -> l.clear());
-        assertThrows(UnsupportedOperationException.class, () -> l.addAll(0, null));
+        assertThrows(UnsupportedOperationException.class, () -> LIST.remove(null));
+        assertThrows(UnsupportedOperationException.class, () -> LIST.addAll(List.of()));
+        assertThrows(UnsupportedOperationException.class, () -> LIST.removeAll(List.of()));
+        assertThrows(UnsupportedOperationException.class, () -> LIST.retainAll(List.of()));
+        assertThrows(UnsupportedOperationException.class, () -> LIST.clear());
+        assertThrows(UnsupportedOperationException.class, () -> LIST.addAll(0, null));
     }
 
     @Test