/** * Does the list contain the specified element? */ public boolean contains(final Object x) { return elements.contains(x); }
/** * Reverse list. * If the list is empty or a singleton, then the same list is returned. * Otherwise a new list is formed. */ public ImmutableList<A> reverse() { // if it is empty or a singleton, return itself if (isEmpty() || tail.isEmpty()) { return this; } ImmutableList<A> rev = empty(); for (ImmutableList<A> l = this; l.nonEmpty(); l = l.tail) { rev = new ImmutableList<>(l.head, rev); } return rev; }
/** * Construct a list consisting of given elements. */ @SuppressWarnings({"varargs", "unchecked"}) public static <A> ImmutableList<A> of(final A x1, final A x2, final A x3, final A... rest) { return new ImmutableList<>(x1, new ImmutableList<>(x2, new ImmutableList<>(x3, from(rest)))); }
/** * Construct a list consisting of given elements. */ public static <A> ImmutableList<A> of(final A x1, final A x2) { return new ImmutableList<>(x1, of(x2)); }
/** * Append given element at length, forming and returning * a new list. */ public ImmutableList<A> append(final A x) { return of(x).prependList(this); }
/** * Construct a list consisting of given element. */ public static <A> ImmutableList<A> of(final A x1) { return new ImmutableList<>(x1, ImmutableList.<A>empty()); }
@Override public ImmutableList<Type<?>> visitClassType(final Type<?> t, final ImmutableList<Type<?>> list) { final TypeList interfaces = t.getExplicitInterfaces(); if (interfaces.isEmpty()) { return ImmutableList.empty(); } ImmutableList<Type<?>> result = union(list, ImmutableList.from(t.getExplicitInterfaces().toArray())); for (final Type ifType : interfaces) { if (!list.contains(ifType)) { result = union(result, visit(ifType, result)); } } return result; }
public static ImmutableList<Type<?>> closure(final Type<?> t) { ImmutableList<Type<?>> cl = closureCache.get(t); if (cl == null) { final Type st = superType(t); if (!t.isCompoundType()) { if (st != null && st.getKind() == TypeKind.DECLARED) { cl = insert(closure(st), t); } else if (st != null && st.getKind() == TypeKind.TYPEVAR) { cl = closure(st).prepend(t); } else { cl = ImmutableList.<Type<?>>of(t); } } else { cl = closure(superType(t)); } for (ImmutableList<Type<?>> l = interfaces(t); l.nonEmpty(); l = l.tail) { cl = union(cl, closure(l.head)); } closureCache.put(t, cl); } return cl; }
/** * Copy successive elements of this list into given vector until * list is exhausted or end of vector is reached. */ @NotNull @Override @SuppressWarnings("unchecked") public <T> T[] toArray(@NotNull final T[] vec) { int i = 0; ImmutableList<A> l = this; while (l.nonEmpty() && i < vec.length) { vec[i] = (T)l.head; l = l.tail; i++; } if (l.isEmpty()) { if (i < vec.length) { vec[i] = null; } return vec; } return toArray((T[])Array.newInstance(vec.getClass().getComponentType(), size())); }
@Override public ImmutableList<Type<?>> visitTypeParameter(final Type<?> t, final ImmutableList<Type<?>> list) { final Type upperBound = t.getExtendsBound(); if (upperBound.isCompoundType()) { return interfaces(upperBound); } if (upperBound.isInterface()) { return ImmutableList.<Type<?>>of(upperBound); } return ImmutableList.empty(); }
final TypeList S = freshTypeVariables(T); ImmutableList<Type<?>> currentA = ImmutableList.from(A.toArray()); ImmutableList<Type<?>> currentT = ImmutableList.from(T.toArray()); ImmutableList<Type<?>> currentS = ImmutableList.from(S.toArray()); while (!currentA.isEmpty() && !currentT.isEmpty() && !currentS.isEmpty()) { if (!currentA.isEmpty() || !currentT.isEmpty() || !currentS.isEmpty()) { return erasure(t); // some "rare" type involved
@Override public ImmutableList<Type<?>> visitArrayType(final Type<?> type, final ImmutableList<Type<?>> parameter) { return ImmutableList.empty(); }
/** * Append all elements in a list to buffer. */ public ListBuffer<A> appendList(ImmutableList<A> xs) { while (xs.nonEmpty()) { append(xs.head); xs = xs.tail; } return this; }
public A get(final int index) { if (index < 0) { throw new IndexOutOfBoundsException(String.valueOf(index)); } ImmutableList<A> l = this; int i = index; while (i-- > 0 && !l.isEmpty()) { l = l.tail; } if (l.isEmpty()) { throw new IndexOutOfBoundsException( "Index: " + index + ", " + "Size: " + size() ); } return l.head; }
/** * Prepend an element to buffer. */ public ListBuffer<A> prepend(final A x) { elements = elements.prepend(x); count++; return this; }
public void adapt(final Type source, final Type target) throws AdaptFailure { visit(source, target); ImmutableList<Type<?>> fromList = from.toList(); ImmutableList<Type<?>> toList = to.toList(); while (!fromList.isEmpty()) { final Type t = mapping.get(fromList.head); if (toList.head != t) { toList.head = t; } fromList = fromList.tail; toList = toList.tail; } }