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;
/**
@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());
}
}
- 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());
* 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
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
* 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
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