package jetbrains.mps.internal.collections.runtime; /*Generated by MPS */ import jetbrains.mps.internal.collections.runtime.impl.NullSetSequence; import jetbrains.mps.internal.collections.runtime.impl.NullSequence; import jetbrains.mps.internal.collections.runtime.impl.BasicSequence; import java.util.Arrays; import jetbrains.mps.baseLanguage.closures.runtime.AdapterClass; import jetbrains.mps.baseLanguage.closures.runtime._FunctionTypes; import java.util.Collections; public abstract class Sequence<T> extends AbstractSequence<T> implements ISequence<T>, Iterable<T> { public static final boolean USE_NULL_SEQUENCE = true; public static final boolean IGNORE_NULL_VALUES = false; public static final boolean NULL_WHEN_EMPTY = true; public static final boolean NULL_ARRAY_IS_SINGLETON = true; protected static final Object[] ARRAY_WITH_NULL = new Object[]{null}; public Sequence() { } @Override public String toString() { Iterable<T> iterable = toIterable(); if (iterable == null) { return "null"; } StringBuilder sb = new StringBuilder("["); String sep = ""; for (T t : iterable) { sb.append(sep).append(String.valueOf(t)); sep = ", "; } sb.append("]"); return sb.toString(); } @SuppressWarnings(value = "unchecked") protected static <U> U[] nullSingletonArray() { return (U[]) ARRAY_WITH_NULL; } public static <U> ISequence<U> emptySequence() { return NullSetSequence.instance(); } public static <U> ISequence<U> fromArray(U... array) { if (USE_NULL_SEQUENCE) { if (array == null) { return NullSequence.instance(); } } return new BasicSequence<U>(Arrays.asList(array)); } public static <U> ISequence<U> fromClosure(@AdapterClass(value = "ISequenceClosure") _FunctionTypes._return_P0_E0<? extends Iterable<U>> cls) { return Sequence.fromIterable(cls.invoke()); } public static <U> ISequence<U> fromIterable(Iterable<U> iterable) { if (USE_NULL_SEQUENCE) { if (iterable == null) { return NullSequence.instance(); } } if (iterable instanceof ISequence) { return (ISequence<U>) iterable; } return new BasicSequence<U>(iterable); } public static <U> ISequence<U> singleton(U value) { if (IGNORE_NULL_VALUES) { if (value == null) { return NullSequence.instance(); } } return new BasicSequence<U>(Collections.singleton(value)); } }