package jetbrains.mps.internal.collections.runtime;
/*Generated by MPS */
import java.util.Collection;
import java.io.Serializable;
import java.util.Collections;
import jetbrains.mps.internal.collections.runtime.impl.NullCollectionSequence;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
public abstract class CollectionSequence<T> extends AbstractCollectionSequence<T> implements ICollectionSequence<T>, Collection<T>, Serializable {
private static final long serialVersionUID = -5323571231659062625L;
protected CollectionSequence() {
}
@Override
public ICollectionSequence<T> asUnmodifiable() {
final Collection<T> unmodifiableCollection = Collections.unmodifiableCollection(getCollection());
return new CollectionSequence<T>() {
@Override
protected Collection<T> getCollection() {
return unmodifiableCollection;
}
};
}
@Override
public ICollectionSequence<T> asSynchronized() {
final Collection<T> synchronizedCollection = CollectionUtils.synchronizedCollection(getCollection());
return new CollectionSequence<T>() {
@Override
protected Collection<T> getCollection() {
return synchronizedCollection;
}
};
}
public static <U> ICollectionSequence<U> fromCollection(final Collection<U> coll) {
if (AbstractSequence.USE_NULL_SEQUENCE) {
if (coll == null) {
return NullCollectionSequence.instance();
}
}
if (coll instanceof ICollectionSequence<?>) {
return (ICollectionSequence<U>) coll;
}
return new CollectionSequence<U>() {
@Override
protected Collection<U> getCollection() {
return coll;
}
};
}
public static <U> ICollectionSequence<U> fromCollectionWithValues(Collection<U> coll, Iterable<? extends U> it) {
Collection<U> tmp = coll;
if (AbstractSequence.USE_NULL_SEQUENCE) {
if (coll == null && it == null) {
return NullCollectionSequence.instance();
} else
if (coll == null) {
tmp = new ArrayList<U>();
} else
if (it == null) {
return CollectionSequence.fromCollection(coll);
}
}
if (AbstractSequence.IGNORE_NULL_VALUES) {
for (U u : it) {
if (u != null) {
tmp.add(u);
}
}
} else
if (it instanceof Collection<?>) {
tmp.addAll((Collection<? extends U>) it);
} else {
for (U u : it) {
tmp.add(u);
}
}
if (tmp instanceof ICollectionSequence<?>) {
return (ICollectionSequence<U>) tmp;
}
final Collection<U> myColl = tmp;
return new CollectionSequence<U>() {
@Override
protected Collection<U> getCollection() {
return myColl;
}
};
}
public static <U> ICollectionSequence<U> fromCollectionAndArray(Collection<U> coll, U... array) {
if (AbstractSequence.NULL_ARRAY_IS_SINGLETON) {
if (array == null) {
array = (U[]) Sequence.nullSingletonArray();
}
}
if (AbstractSequence.USE_NULL_SEQUENCE) {
if (coll == null && array == null) {
return NullCollectionSequence.instance();
} else
if (coll == null) {
coll = new ArrayList<U>();
} else
if (array == null) {
if (coll instanceof ICollectionSequence<?>) {
return (ICollectionSequence<U>) coll;
}
final Collection<U> myColl = coll;
return new CollectionSequence<U>() {
@Override
protected Collection<U> getCollection() {
return myColl;
}
};
}
}
List<U> input = Arrays.asList(array);
if (AbstractSequence.IGNORE_NULL_VALUES) {
for (U u : input) {
if (u != null) {
coll.add(u);
}
}
} else {
coll.addAll(input);
}
if (coll instanceof ICollectionSequence<?>) {
return (ICollectionSequence<U>) coll;
}
final Collection<U> myColl = coll;
return new CollectionSequence<U>() {
@Override
protected Collection<U> getCollection() {
return myColl;
}
};
}
}