net.amygdalum.util.builders.Lists Maven / Gradle / Ivy
package net.amygdalum.util.builders;
import static java.util.Arrays.asList;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
public final class Lists {
private List list;
private Lists() {
this.list = new ArrayList();
}
private Lists(Collection extends T> list) {
this.list = new ArrayList(list);
}
@SafeVarargs
public static Lists list(T... elements) {
return new Lists(asList(elements));
}
public static Lists empty() {
return new Lists();
}
@SafeVarargs
public static List of(T... elements) {
return new ArrayList(asList(elements));
}
@SafeVarargs
public static List ofLinked(T... elements) {
return new LinkedList(asList(elements));
}
@SafeVarargs
public static List of(Predicate cond, T... elements) {
ArrayList list = new ArrayList();
for (T element : elements) {
if (cond.evaluate(element)) {
list.add(element);
}
}
return list;
}
@SafeVarargs
public static List ofLinked(Predicate cond, T... elements) {
LinkedList list = new LinkedList();
for (T element : elements) {
if (cond.evaluate(element)) {
list.add(element);
}
}
return list;
}
public static List ofPrimitives(int[] array) {
ArrayList list = new ArrayList(array.length);
for (int i : array) {
list.add(i);
}
return list;
}
public static List ofLinkedPrimitives(int[] array) {
LinkedList list = new LinkedList();
for (int i : array) {
list.add(i);
}
return list;
}
public Lists add(T add) {
list.add(add);
return this;
}
public Lists addConditional(boolean b, T add) {
if (b) {
list.add(add);
}
return this;
}
@SuppressWarnings("unchecked")
public Lists addAll(T... add) {
list.addAll(asList(add));
return this;
}
public Lists addAll(List add) {
list.addAll(add);
return this;
}
public Lists remove(T remove) {
list.remove(remove);
return this;
}
public Lists removeConditional(boolean b, T remove) {
if (b) {
list.remove(remove);
}
return this;
}
@SuppressWarnings("unchecked")
public Lists removeAll(T... remove) {
list.removeAll(asList(remove));
return this;
}
public Lists removeAll(List remove) {
list.removeAll(remove);
return this;
}
public Lists retain(T retain) {
Set retainAll = new HashSet();
retainAll.add(retain);
list.retainAll(retainAll);
return this;
}
public Lists retainConditional(boolean b, T retain) {
if (b) {
Set retainAll = new HashSet();
retainAll.add(retain);
list.retainAll(retainAll);
}
return this;
}
public Lists retainAll(List retain) {
list.retainAll(retain);
return this;
}
@SuppressWarnings("unchecked")
public Lists retainAll(T... retain) {
list.retainAll(asList(retain));
return this;
}
public List build() {
return list;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy