Add SSv1
[yangtools.git] / common / util / src / main / java / org / opendaylight / yangtools / util / CollectionWrappers.java
index 166e9cb7bb6b36fe56ceafa1777fd6cbc90c3683..cd35d74735a551accb76fbd3e5b69e7b80833c84 100644 (file)
@@ -25,6 +25,7 @@ import java.util.Set;
 import java.util.Spliterator;
 import java.util.stream.Stream;
 import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.concepts.Delegator;
 import org.opendaylight.yangtools.concepts.Immutable;
 
 /**
@@ -35,13 +36,18 @@ import org.opendaylight.yangtools.concepts.Immutable;
 @Beta
 @NonNullByDefault
 public final class CollectionWrappers {
-    private static final class ListWrapper<E> extends AbstractList<E> {
+    private static final class ListWrapper<E> extends AbstractList<E> implements Delegator<Collection<E>> {
         private final Collection<E> delegate;
 
-        private ListWrapper(final Collection<E> delegate) {
+        ListWrapper(final Collection<E> delegate) {
             this.delegate = requireNonNull(delegate);
         }
 
+        @Override
+        public Collection<E> getDelegate() {
+            return delegate;
+        }
+
         @Override
         public Iterator<E> iterator() {
             return Iterators.unmodifiableIterator(delegate.iterator());
@@ -73,13 +79,18 @@ public final class CollectionWrappers {
         }
     }
 
-    private static final class SetWrapper<E> extends AbstractSet<E> {
+    private static final class SetWrapper<E> extends AbstractSet<E> implements Delegator<Collection<E>> {
         private final Collection<E> delegate;
 
-        private SetWrapper(final Collection<E> delegate) {
+        SetWrapper(final Collection<E> delegate) {
             this.delegate = requireNonNull(delegate);
         }
 
+        @Override
+        public Collection<E> getDelegate() {
+            return delegate;
+        }
+
         @Override
         public Iterator<E> iterator() {
             return Iterators.unmodifiableIterator(delegate.iterator());
@@ -117,6 +128,7 @@ public final class CollectionWrappers {
      * interface, it is returned unwrapped. Backing collection is required to be effectively immutable. If this
      * requirement is violated, the returned object may behave in unpredictable ways.
      *
+     * @param <E> the type of elements in the collection
      * @param collection Collection to be wrapped
      * @return An effectively-immutable wrapper of the collection.
      * @throws NullPointerException if collection is null
@@ -125,6 +137,9 @@ public final class CollectionWrappers {
         if (collection.isEmpty()) {
             return ImmutableList.of();
         }
+        if (collection instanceof SetWrapper) {
+            return wrapAsList(((SetWrapper<E>) collection).getDelegate());
+        }
         if (collection instanceof List) {
             final List<E> cast = (List<E>) collection;
             return cast instanceof ListWrapper || cast instanceof Immutable || cast instanceof ImmutableList
@@ -142,6 +157,7 @@ public final class CollectionWrappers {
      * it effectively implements the Set contract. Backing collection is required to be effectively immutable. If this
      * requirement is violated, the returned object may behave in unpredictable ways.
      *
+     * @param <E> the type of elements in the collection
      * @param collection Collection to be wrapped
      * @return An effectively-immutable wrapper of the collection.
      * @throws NullPointerException if collection is null or any of its elements is null
@@ -151,6 +167,9 @@ public final class CollectionWrappers {
         if (collection.isEmpty()) {
             return ImmutableSet.of();
         }
+        if (collection instanceof ListWrapper) {
+            return wrapAsSet(((ListWrapper<E>) collection).getDelegate());
+        }
         if (collection instanceof Set) {
             final Set<E> cast = (Set<E>) collection;
             return cast instanceof SetWrapper || cast instanceof Immutable || cast instanceof SingletonSet