Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // made by REghZy :)))
- // a small class, that replicates C#'s linq features
- // In c#, during iteration, it applies linq in a chain, whereas this calculates them all at once
- // meaning, only ArrayList instances are created (if there are items present), not sub-linq instances
- // Hopefully, meaning it's faster than C#'s Linq (although way less features)
- package reghzy.linqv2;
- import reghzy.api.utils.Parsing;
- import javax.annotation.Nonnull;
- import javax.annotation.Nullable;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.LinkedHashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Set;
- import java.util.function.Consumer;
- import java.util.function.Function;
- import java.util.function.Predicate;
- import java.util.function.Supplier;
- /**
- * A collection utility class for manipulating (filtering, mapping, etc) collections of items, in a builder-styled way
- * @param <E> The type of elements this linq instance contains
- */
- public class Linq<E> implements Iterable<E> {
- private Collection<E> items;
- private Linq(Collection<E> iterable) {
- this.items = iterable;
- }
- public static <E> Linq<E> of(@Nullable Collection<E> collection) {
- return new Linq<E>(collection == null ? new ArrayList<E>(0) : collection);
- }
- public static <E> Linq<E> of(@Nullable E[] array) {
- return new Linq<E>(array == null ? new ArrayList<E>(0) : Arrays.asList(array));
- }
- public static <E> Linq<E> ofNonNull(@Nonnull Collection<E> collection) {
- return new Linq<E>(collection);
- }
- public static <E> Linq<E> ofNonNull(@Nonnull E[] array) {
- return new Linq<E>(Arrays.asList(array));
- }
- public Linq<E> where(Predicate<E> predicate) {
- if (this.items.isEmpty())
- return this;
- ArrayList<E> list = new ArrayList<E>(this.items.size());
- for(E e : this.items) {
- if (predicate.test(e)) {
- list.add(e);
- }
- }
- this.items = list;
- return this;
- }
- public <R> Linq<R> select(Function<E, R> predicate) {
- if (this.items.isEmpty())
- return (Linq<R>) this;
- ArrayList<R> list = new ArrayList<R>(this.items.size());
- for (E e : this.items) {
- list.add(predicate.apply(e));
- }
- this.items = (ArrayList<E>) list;
- return (Linq<R>) this;
- }
- public Linq<E> ofType(Class<?> clazz) {
- if (this.items.isEmpty())
- return this;
- ArrayList<E> list = new ArrayList<E>(this.items.size());
- for (E e : this.items) {
- if (clazz.isInstance(e)) {
- list.add(e);
- }
- }
- this.items = list;
- return this;
- }
- public List<E> toListFast() {
- return (this.items instanceof List) ? (List<E>) this.items : toList();
- }
- public List<E> toList() {
- return new ArrayList<E>(this.items);
- }
- public Set<E> toSet() {
- return new HashSet<E>(this.items);
- }
- public Object[] toArray() {
- return this.items.toArray();
- }
- public E[] toArray(E[] array) {
- return this.items.toArray(array);
- }
- public <K, V> Map<K, V> toMap(Function<E, K> keyProvider, Function<E, V> valueProvider) {
- return toMap(keyProvider, valueProvider, HashMap::new);
- }
- public <K, V> Map<K, V> toLinkedMap(Function<E, K> keyProvider, Function<E, V> valueProvider) {
- return toMap(keyProvider, valueProvider, LinkedHashMap::new);
- }
- public <K, V> Map<K, V> toMap(Function<E, K> keyProvider, Function<E, V> valueProvider, Supplier<Map<K, V>> mapSupplier) {
- Map<K, V> map = mapSupplier.get();
- if (this.items.isEmpty())
- return map;
- for (E e : this.items) {
- map.put(keyProvider.apply(e), valueProvider.apply(e));
- }
- return map;
- }
- public <K> Map<K, E> toMapElementAsValue(Function<E, K> valueToKey) {
- return toMapElementAsValue(valueToKey, (Supplier<Map<K,E>>) HashMap::new);
- }
- public <K> Map<K, E> toLinkedMapElementAsValue(Function<E, K> valueToKey) {
- return toMapElementAsValue(valueToKey, (Supplier<Map<K, E>>) LinkedHashMap::new);
- }
- public <K> Map<K, E> toMapElementAsValue(Function<E, K> elementToKey, Supplier<Map<K, E>> mapSupplier) {
- Map<K, E> map = mapSupplier.get();
- if (this.items.isEmpty())
- return map;
- for (E e : this.items) {
- map.put(elementToKey.apply(e), e);
- }
- return map;
- }
- public <V> Map<E, V> toMapElementAsKey(Function<E, V> valueToKey) {
- return toMapElementAsKey(valueToKey, (Supplier<Map<E, V>>) HashMap::new);
- }
- public <V> Map<E, V> toLinkedMapElementAsKey(Function<E, V> valueToKey) {
- return toMapElementAsKey(valueToKey, (Supplier<Map<E, V>>) LinkedHashMap::new);
- }
- public <V> Map<E, V> toMapElementAsKey(Function<E, V> elementToValue, Supplier<Map<E, V>> mapSupplier) {
- Map<E, V> map = mapSupplier.get();
- if (this.items.isEmpty())
- return map;
- for (E e : this.items) {
- map.put(e, elementToValue.apply(e));
- }
- return map;
- }
- public Linq<String> toStrings() {
- if (this.items.isEmpty())
- return (Linq<String>) this;
- ArrayList<String> list = new ArrayList<String>(this.items.size());
- for (E e : this.items) {
- list.add(String.valueOf(e));
- }
- this.items = (ArrayList<E>) list;
- return (Linq<String>) this;
- }
- public Linq<Integer> toIntegers() {
- if (this.items.isEmpty())
- return (Linq<Integer>) this;
- ArrayList<Integer> list = new ArrayList<Integer>(this.items.size());
- for (E e : this.items) {
- list.add(Integer.parseInt(String.valueOf(e)));
- }
- this.items = (ArrayList<E>) list;
- return (Linq<Integer>) this;
- }
- public Linq<Integer> ofIntegers() {
- if (this.items.isEmpty())
- return (Linq<Integer>) this;
- ArrayList<Integer> list = new ArrayList<Integer>(this.items.size());
- for (E e : this.items) {
- if (Parsing.parseInt(String.valueOf(e))) {
- list.add(Parsing.getInt());
- }
- }
- this.items = (ArrayList<E>) list;
- return (Linq<Integer>) this;
- }
- public Linq<Double> toDoubles() {
- if (this.items.isEmpty())
- return (Linq<Double>) this;
- ArrayList<Double> list = new ArrayList<Double>(this.items.size());
- for (E e : this.items) {
- list.add(Double.parseDouble(String.valueOf(e)));
- }
- this.items = (ArrayList<E>) list;
- return (Linq<Double>) this;
- }
- public Linq<Double> ofDoubles() {
- if (this.items.isEmpty())
- return (Linq<Double>) this;
- ArrayList<Double> list = new ArrayList<Double>(this.items.size());
- for (E e : this.items) {
- if (Parsing.parseDouble(String.valueOf(e))) {
- list.add(Parsing.getDouble());
- }
- }
- this.items = (ArrayList<E>) list;
- return (Linq<Double>) this;
- }
- public Linq<E> foreach(Consumer<E> consumer) {
- if (this.items.isEmpty())
- return this;
- for (E t : this.items) {
- consumer.accept(t);
- }
- return this;
- }
- public Linq<E> tryForeach(Consumer<E> action) {
- if (this.items.isEmpty())
- return this;
- for (E e : this.items) {
- try {
- action.accept(e);
- }
- catch (Throwable ignored) { }
- }
- return this;
- }
- public Linq<E> tryForeach(Consumer<E> action, Consumer<Throwable> exceptionHandler) {
- if (this.items.isEmpty())
- return this;
- for (E t : this.items) {
- try {
- action.accept(t);
- }
- catch (Throwable ex) {
- exceptionHandler.accept(ex);
- }
- }
- return this;
- }
- @Override
- public void forEach(Consumer<? super E> action) {
- if (this.items.isEmpty())
- return;
- for (E e : this.items) {
- action.accept(e);
- }
- }
- public void tryForEach(Consumer<? super E> action) {
- if (this.items.isEmpty())
- return;
- for (E e : this.items) {
- try {
- action.accept(e);
- }
- catch (Throwable ignored) { }
- }
- }
- public void tryForEach(Consumer<? super E> action, Consumer<Throwable> exceptionHandler) {
- if (this.items.isEmpty())
- return;
- for (E e : this.items) {
- try {
- action.accept(e);
- }
- catch (Throwable ex) {
- exceptionHandler.accept(ex);
- }
- }
- }
- @Nonnull
- @Override
- public Iterator<E> iterator() {
- return this.items.iterator();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement