
com.artclod.common.collect.GuavaImFList Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of functional-collections Show documentation
Show all versions of functional-collections Show documentation
FCollections is a series of function style Java Collections
package com.artclod.common.collect;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.Iterator;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import com.artclod.common.collect.base.BaseFList;
import com.artclod.common.collect.base.ImFListMixin;
import com.artclod.common.collect.builder.CollectionBuilder;
import com.artclod.common.collect.builder.GuavaImFListBuilder;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableList.Builder;
import com.google.common.collect.Lists;
public class GuavaImFList extends BaseFList> implements ImFList, ImFListMixin {
private static final long serialVersionUID = 1L;
public static GuavaImFList wrap(ImmutableList inner) {
return new GuavaImFList(inner);
}
public static GuavaImFList create() {
return new GuavaImFList(ImmutableList.of());
}
public static GuavaImFList create(Iterable extends E> elements) {
return new GuavaImFList(ImmutableList.copyOf(elements));
}
public static GuavaImFList create(Collection extends E> elements) {
return new GuavaImFList(ImmutableList.copyOf(elements));
}
public static GuavaImFList create(Iterator extends E> elements) {
return new GuavaImFList(ImmutableList.copyOf(elements));
}
@SafeVarargs
public static GuavaImFList create(E... elements) {
return new GuavaImFList(ImmutableList.copyOf(elements));
}
protected final ImmutableList inner;
public GuavaImFList(ImmutableList inner) {
super(inner);
this.inner = inner;
}
@Override
protected CollectionBuilder> builder() {
return new GuavaImFListBuilder(ImmutableList.builder());
}
public ImmutableList toGuava() {
return inner;
}
public Builder toBuilder() {
return ImmutableList. builder().addAll(inner);
}
public ArrayList toArrayList() {
return Lists.newArrayList(this);
}
// ==== ImFList methods ====
public ArrayFList toMu() {
return ArrayFList.create(this);
}
public GuavaImFList toIm() {
return this;
}
@Override
public GuavaImFList map(Function super E, ? extends O> f) {
Builder builder = ImmutableList. builder();
for (E e : this) {
builder.add(f.apply(e));
}
return new GuavaImFList(builder.build());
}
@Override
public GuavaImFList flatMap(Function super E, ? extends Collection extends O>> mapper) {
Builder builder = ImmutableList. builder();
for (E e : this) {
builder.addAll(mapper.apply(e));
}
return new GuavaImFList(builder.build());
}
// ==== Copy modifiers ====
@Override
public GuavaImFList addCp(E e) {
ArrayList list = toArrayList();
list.add(e);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList removeCp(Object o) {
ArrayList list = toArrayList();
list.remove(o);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList addAllCp(Collection extends E> c) {
ArrayList list = toArrayList();
list.addAll(c);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList addAllCp(int index, Collection extends E> c) {
ArrayList list = toArrayList();
list.addAll(index, c);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList removeAllCp(Collection> c) {
ArrayList list = toArrayList();
list.removeAll(c);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList retainAllCp(Collection> c) {
ArrayList list = toArrayList();
list.retainAll(c);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList replaceAllCp(UnaryOperator operator) {
ArrayList list = toArrayList();
list.replaceAll(operator);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList sortCp(Comparator super E> c) {
ArrayList list = toArrayList();
list.sort(c);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList setCp(int index, E element) {
ArrayList list = toArrayList();
list.set(index, element);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList addCp(int index, E element) {
ArrayList list = toArrayList();
list.add(index, element);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList removeCp(int index) {
ArrayList list = toArrayList();
list.remove(index);
return new GuavaImFList(ImmutableList.copyOf(list));
}
@Override
public GuavaImFList removeIfCp(Predicate super E> filter) {
ArrayList list = toArrayList();
list.removeIf(filter);
return new GuavaImFList(ImmutableList.copyOf(list));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy