@Override public C.List<T> snapshot() { if (isImmutable()) { return this; } return ListBuilder.toList(this); }
@Override public C.List<T> snapshot() { if (isImmutable()) { return this; } return ListBuilder.toList(this); }
protected final boolean isMutable() { return !isImmutable() && !isReadOnly(); }
protected final boolean isMutable() { return !isImmutable() && !isReadOnly(); }
@Override public C.List<T> tail() { int sz = size(); if (0 == sz) { throw new UnsupportedOperationException(); } if (isImmutable()) { return subList(1, sz); } C.List<T> l = C.newSizedList(sz - 1); l.addAll(subList(1, sz)); return l; }
@Override public C.List<T> tail() { int sz = size(); if (0 == sz) { throw new UnsupportedOperationException(); } if (isImmutable()) { return subList(1, sz); } C.List<T> l = C.newSizedList(sz - 1); l.addAll(subList(1, sz)); return l; }
@Override public Lang.T2<C.List<T>, C.List<T>> split(final Lang.Function<? super T, Boolean> predicate) { final C.List<T> left = C.newList(); final C.List<T> right = C.newList(); accept(new $.Visitor<T>() { @Override public void visit(T t) throws Lang.Break { if (predicate.apply(t)) { left.add(t); } else { right.add(t); } } }); if (isImmutable() || isReadOnly()) { return $.T2(C.list(left), C.list(right)); } return $.T2(left, right); }
@Override public Lang.T2<C.List<T>, C.List<T>> split(final Lang.Function<? super T, Boolean> predicate) { final C.List<T> left = C.newList(); final C.List<T> right = C.newList(); accept(new $.Visitor<T>() { @Override public void visit(T t) throws Lang.Break { if (predicate.apply(t)) { left.add(t); } else { right.add(t); } } }); if (isImmutable() || isReadOnly()) { return $.T2(C.list(left), C.list(right)); } return $.T2(left, right); }
@Override public C.List<T> take(int n) { boolean immutable = isImmutable(); if (n == 0) { if (immutable) { return Nil.list(); } else { return C.newList(); } } else if (n < 0) { return drop(size() + n); } else if (n >= size()) { return this; } if (immutable) { return subList(0, n); } C.List<T> l = C.newSizedList(n); l.addAll(subList(0, n)); return l; }
@Override public C.List<T> take(int n) { boolean immutable = isImmutable(); if (n == 0) { if (immutable) { return Nil.list(); } else { return C.newList(); } } else if (n < 0) { return drop(size() + n); } else if (n >= size()) { return this; } if (immutable) { return subList(0, n); } C.List<T> l = C.newSizedList(n); l.addAll(subList(0, n)); return l; }
private C.List<T> unLazyPrepend(Iterable<? extends T> iterable) { if (isMutable()) { int pos = 0; for (T t : iterable) { add(pos++, t); } return this; } // immutable if (isImmutable()) { ListBuilder<T> lb = new ListBuilder<T>(size() * 2); lb.append(iterable).append(this); return lb.toList(); } // mutable but read only C.List<T> l = C.newSizedList(size() * 2); l.addAll(iterable); l.addAll(this); return l; }
@Override public <R> C.List<R> collect(String path) { boolean immutable = isImmutable(); int sz = size(); if (0 == sz) { return immutable ? Nil.<R>list() : C.<R>newList(); } if (immutable) { ListBuilder<R> lb = new ListBuilder<>(sz); for (T t : this) { lb.add((R) $.getProperty(t, path)); } return lb.toList(); } else { C.List<R> list = C.newSizedList(sz); for (T t : this) { list.add((R) $.getProperty(t, path)); } return list; } }
@Override public C.List<T> append(T t) { if (isMutable()) { add(t); return this; } // Immutable if (isImmutable()) { ListBuilder<T> lb = new ListBuilder<T>(size() + 1); lb.addAll(this); lb.add(t); return lb.toList(); } // mutable but readonly C.List<T> l = C.newSizedList(size() + 1); l.addAll(this); l.add(t); return l; }
private C.List<T> unLazyPrepend(Iterable<? extends T> iterable) { if (isMutable()) { int pos = 0; for (T t : iterable) { add(pos++, t); } return this; } // immutable if (isImmutable()) { ListBuilder<T> lb = new ListBuilder<T>(size() * 2); lb.append(iterable).append(this); return lb.toList(); } // mutable but read only C.List<T> l = C.newSizedList(size() * 2); l.addAll(iterable); l.addAll(this); return l; }
@Override public C.List<T> append(T t) { if (isMutable()) { add(t); return this; } // Immutable if (isImmutable()) { ListBuilder<T> lb = new ListBuilder<T>(size() + 1); lb.addAll(this); lb.add(t); return lb.toList(); } // mutable but readonly C.List<T> l = C.newSizedList(size() + 1); l.addAll(this); l.add(t); return l; }
@SuppressWarnings("unchecked") private C.List<T> unLazyAppend(Iterable<? extends T> iterable) { if (isMutable()) { if (iterable instanceof Collection) { addAll((Collection<? extends T>) iterable); } else { C.forEach(iterable, $.visitor(C.F.addTo(this))); } return this; } // immutable if (isImmutable()) { ListBuilder<T> lb = new ListBuilder<T>(size() * 2); lb.append(this).append(iterable); return lb.toList(); } // mutable but read only C.List<T> l = C.newSizedList(size() * 2); l.addAll(this); l.addAll(iterable); return l; }
@Override public C.List<T> filter($.Function<? super T, Boolean> predicate) { boolean immutable = isImmutable(); int sz = size(); // TODO: handle lazy filter if (immutable) { if (0 == sz) { return Nil.list(); } ListBuilder<T> lb = new ListBuilder<T>(sz); forEach($.visitor($.predicate(predicate).ifThen(C.F.addTo(lb)))); return lb.toList(); } else { if (0 == sz) { return C.newList(); } C.List<T> l = C.newSizedList(sz); forEach($.visitor($.predicate(predicate).ifThen(C.F.addTo(l)))); return l; } }
@Override public C.List<T> remove($.Function<? super T, Boolean> predicate) { boolean immutable = isImmutable(); int sz = size(); // TODO: handle lazy remove if (immutable) { if (0 == sz) { return Nil.list(); } ListBuilder<T> lb = new ListBuilder<T>(sz); forEach($.visitor($.predicate(predicate).elseThen(C.F.addTo(lb)))); return lb.toList(); } else { if (0 == sz) { return C.newList(); } C.List<T> l = C.newSizedList(sz); forEach($.visitor($.predicate(predicate).elseThen(C.F.addTo(l)))); return l; } }
@Override public C.List<T> remove($.Function<? super T, Boolean> predicate) { boolean immutable = isImmutable(); int sz = size(); // TODO: handle lazy remove if (immutable) { if (0 == sz) { return Nil.list(); } ListBuilder<T> lb = new ListBuilder<T>(sz); forEach($.visitor($.predicate(predicate).elseThen(C.F.addTo(lb)))); return lb.toList(); } else { if (0 == sz) { return C.newList(); } C.List<T> l = C.newSizedList(sz); forEach($.visitor($.predicate(predicate).elseThen(C.F.addTo(l)))); return l; } }
@Override public <R> C.List<R> map($.Function<? super T, ? extends R> mapper) { boolean immutable = isImmutable(); int sz = size(); if (isLazy()) { return MappedList.of(this, mapper); } if (immutable) { if (0 == sz) { return Nil.list(); } ListBuilder<R> lb = new ListBuilder<R>(sz); forEach($.visitor($.f1(mapper).andThen(C.F.addTo(lb)))); return lb.toList(); } else { if (0 == sz) { return C.newList(); } C.List<R> l = C.newSizedList(sz); forEach($.visitor($.f1(mapper).andThen(C.F.addTo(l)))); return l; } }