Advertisement
bobmarley12345

Linq in Java

May 27th, 2022
690
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 9.26 KB | None | 0 0
  1. // made by REghZy :)))
  2. // a small class, that replicates C#'s linq features
  3. // In c#, during iteration, it applies linq in a chain, whereas this calculates them all at once
  4. // meaning, only ArrayList instances are created (if there are items present), not sub-linq instances
  5. // Hopefully, meaning it's faster than C#'s Linq (although way less features)
  6. package reghzy.linqv2;
  7.  
  8. import reghzy.api.utils.Parsing;
  9.  
  10. import javax.annotation.Nonnull;
  11. import javax.annotation.Nullable;
  12. import java.util.ArrayList;
  13. import java.util.Arrays;
  14. import java.util.Collection;
  15. import java.util.HashMap;
  16. import java.util.HashSet;
  17. import java.util.Iterator;
  18. import java.util.LinkedHashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import java.util.Set;
  22. import java.util.function.Consumer;
  23. import java.util.function.Function;
  24. import java.util.function.Predicate;
  25. import java.util.function.Supplier;
  26.  
  27. /**
  28.  * A collection utility class for manipulating (filtering, mapping, etc) collections of items, in a builder-styled way
  29.  * @param <E> The type of elements this linq instance contains
  30.  */
  31. public class Linq<E> implements Iterable<E> {
  32.     private Collection<E> items;
  33.  
  34.     private Linq(Collection<E> iterable) {
  35.         this.items = iterable;
  36.     }
  37.  
  38.     public static <E> Linq<E> of(@Nullable Collection<E> collection) {
  39.         return new Linq<E>(collection == null ? new ArrayList<E>(0) : collection);
  40.     }
  41.  
  42.     public static <E> Linq<E> of(@Nullable E[] array) {
  43.         return new Linq<E>(array == null ? new ArrayList<E>(0) : Arrays.asList(array));
  44.     }
  45.  
  46.     public static <E> Linq<E> ofNonNull(@Nonnull Collection<E> collection) {
  47.         return new Linq<E>(collection);
  48.     }
  49.  
  50.     public static <E> Linq<E> ofNonNull(@Nonnull E[] array) {
  51.         return new Linq<E>(Arrays.asList(array));
  52.     }
  53.  
  54.     public Linq<E> where(Predicate<E> predicate) {
  55.         if (this.items.isEmpty())
  56.             return this;
  57.  
  58.         ArrayList<E> list = new ArrayList<E>(this.items.size());
  59.         for(E e : this.items) {
  60.             if (predicate.test(e)) {
  61.                 list.add(e);
  62.             }
  63.         }
  64.  
  65.         this.items = list;
  66.         return this;
  67.     }
  68.  
  69.     public <R> Linq<R> select(Function<E, R> predicate) {
  70.         if (this.items.isEmpty())
  71.             return (Linq<R>) this;
  72.  
  73.         ArrayList<R> list = new ArrayList<R>(this.items.size());
  74.         for (E e : this.items) {
  75.             list.add(predicate.apply(e));
  76.         }
  77.  
  78.         this.items = (ArrayList<E>) list;
  79.         return (Linq<R>) this;
  80.     }
  81.  
  82.     public Linq<E> ofType(Class<?> clazz) {
  83.         if (this.items.isEmpty())
  84.             return this;
  85.  
  86.         ArrayList<E> list = new ArrayList<E>(this.items.size());
  87.         for (E e : this.items) {
  88.             if (clazz.isInstance(e)) {
  89.                 list.add(e);
  90.             }
  91.         }
  92.  
  93.         this.items = list;
  94.         return this;
  95.     }
  96.  
  97.     public List<E> toListFast() {
  98.         return (this.items instanceof List) ? (List<E>) this.items : toList();
  99.     }
  100.  
  101.     public List<E> toList() {
  102.         return new ArrayList<E>(this.items);
  103.     }
  104.  
  105.     public Set<E> toSet() {
  106.         return new HashSet<E>(this.items);
  107.     }
  108.  
  109.     public Object[] toArray() {
  110.         return this.items.toArray();
  111.     }
  112.  
  113.     public E[] toArray(E[] array) {
  114.         return this.items.toArray(array);
  115.     }
  116.  
  117.     public <K, V> Map<K, V> toMap(Function<E, K> keyProvider, Function<E, V> valueProvider) {
  118.         return toMap(keyProvider, valueProvider, HashMap::new);
  119.     }
  120.  
  121.     public <K, V> Map<K, V> toLinkedMap(Function<E, K> keyProvider, Function<E, V> valueProvider) {
  122.         return toMap(keyProvider, valueProvider, LinkedHashMap::new);
  123.     }
  124.  
  125.     public <K, V> Map<K, V> toMap(Function<E, K> keyProvider, Function<E, V> valueProvider, Supplier<Map<K, V>> mapSupplier) {
  126.         Map<K, V> map = mapSupplier.get();
  127.         if (this.items.isEmpty())
  128.             return map;
  129.  
  130.         for (E e : this.items) {
  131.             map.put(keyProvider.apply(e), valueProvider.apply(e));
  132.         }
  133.  
  134.         return map;
  135.     }
  136.  
  137.     public <K> Map<K, E> toMapElementAsValue(Function<E, K> valueToKey) {
  138.         return toMapElementAsValue(valueToKey, (Supplier<Map<K,E>>) HashMap::new);
  139.     }
  140.  
  141.     public <K> Map<K, E> toLinkedMapElementAsValue(Function<E, K> valueToKey) {
  142.         return toMapElementAsValue(valueToKey, (Supplier<Map<K, E>>) LinkedHashMap::new);
  143.     }
  144.  
  145.     public <K> Map<K, E> toMapElementAsValue(Function<E, K> elementToKey, Supplier<Map<K, E>> mapSupplier) {
  146.         Map<K, E> map = mapSupplier.get();
  147.         if (this.items.isEmpty())
  148.             return map;
  149.  
  150.         for (E e : this.items) {
  151.             map.put(elementToKey.apply(e), e);
  152.         }
  153.  
  154.         return map;
  155.     }
  156.  
  157.     public <V> Map<E, V> toMapElementAsKey(Function<E, V> valueToKey) {
  158.         return toMapElementAsKey(valueToKey, (Supplier<Map<E, V>>) HashMap::new);
  159.     }
  160.  
  161.     public <V> Map<E, V> toLinkedMapElementAsKey(Function<E, V> valueToKey) {
  162.         return toMapElementAsKey(valueToKey, (Supplier<Map<E, V>>) LinkedHashMap::new);
  163.     }
  164.  
  165.     public <V> Map<E, V> toMapElementAsKey(Function<E, V> elementToValue, Supplier<Map<E, V>> mapSupplier) {
  166.         Map<E, V> map = mapSupplier.get();
  167.         if (this.items.isEmpty())
  168.             return map;
  169.  
  170.         for (E e : this.items) {
  171.             map.put(e, elementToValue.apply(e));
  172.         }
  173.  
  174.         return map;
  175.     }
  176.  
  177.     public Linq<String> toStrings() {
  178.         if (this.items.isEmpty())
  179.             return (Linq<String>) this;
  180.  
  181.         ArrayList<String> list = new ArrayList<String>(this.items.size());
  182.         for (E e : this.items) {
  183.             list.add(String.valueOf(e));
  184.         }
  185.  
  186.         this.items = (ArrayList<E>) list;
  187.         return (Linq<String>) this;
  188.     }
  189.  
  190.     public Linq<Integer> toIntegers() {
  191.         if (this.items.isEmpty())
  192.             return (Linq<Integer>) this;
  193.  
  194.         ArrayList<Integer> list = new ArrayList<Integer>(this.items.size());
  195.         for (E e : this.items) {
  196.             list.add(Integer.parseInt(String.valueOf(e)));
  197.         }
  198.  
  199.         this.items = (ArrayList<E>) list;
  200.         return (Linq<Integer>) this;
  201.     }
  202.  
  203.     public Linq<Integer> ofIntegers() {
  204.         if (this.items.isEmpty())
  205.             return (Linq<Integer>) this;
  206.  
  207.         ArrayList<Integer> list = new ArrayList<Integer>(this.items.size());
  208.         for (E e : this.items) {
  209.             if (Parsing.parseInt(String.valueOf(e))) {
  210.                 list.add(Parsing.getInt());
  211.             }
  212.         }
  213.  
  214.         this.items = (ArrayList<E>) list;
  215.         return (Linq<Integer>) this;
  216.     }
  217.  
  218.     public Linq<Double> toDoubles() {
  219.         if (this.items.isEmpty())
  220.             return (Linq<Double>) this;
  221.  
  222.         ArrayList<Double> list = new ArrayList<Double>(this.items.size());
  223.         for (E e : this.items) {
  224.             list.add(Double.parseDouble(String.valueOf(e)));
  225.         }
  226.  
  227.         this.items = (ArrayList<E>) list;
  228.         return (Linq<Double>) this;
  229.     }
  230.  
  231.     public Linq<Double> ofDoubles() {
  232.         if (this.items.isEmpty())
  233.             return (Linq<Double>) this;
  234.  
  235.         ArrayList<Double> list = new ArrayList<Double>(this.items.size());
  236.         for (E e : this.items) {
  237.             if (Parsing.parseDouble(String.valueOf(e))) {
  238.                 list.add(Parsing.getDouble());
  239.             }
  240.         }
  241.  
  242.         this.items = (ArrayList<E>) list;
  243.         return (Linq<Double>) this;
  244.     }
  245.  
  246.     public Linq<E> foreach(Consumer<E> consumer) {
  247.         if (this.items.isEmpty())
  248.             return this;
  249.  
  250.         for (E t : this.items) {
  251.             consumer.accept(t);
  252.         }
  253.  
  254.         return this;
  255.     }
  256.  
  257.     public Linq<E> tryForeach(Consumer<E> action) {
  258.         if (this.items.isEmpty())
  259.             return this;
  260.  
  261.         for (E e : this.items) {
  262.             try {
  263.                 action.accept(e);
  264.             }
  265.             catch (Throwable ignored) { }
  266.         }
  267.  
  268.         return this;
  269.     }
  270.  
  271.     public Linq<E> tryForeach(Consumer<E> action, Consumer<Throwable> exceptionHandler) {
  272.         if (this.items.isEmpty())
  273.             return this;
  274.  
  275.         for (E t : this.items) {
  276.             try {
  277.                 action.accept(t);
  278.             }
  279.             catch (Throwable ex) {
  280.                 exceptionHandler.accept(ex);
  281.             }
  282.         }
  283.  
  284.         return this;
  285.     }
  286.  
  287.     @Override
  288.     public void forEach(Consumer<? super E> action) {
  289.         if (this.items.isEmpty())
  290.             return;
  291.  
  292.         for (E e : this.items) {
  293.             action.accept(e);
  294.         }
  295.     }
  296.  
  297.     public void tryForEach(Consumer<? super E> action) {
  298.         if (this.items.isEmpty())
  299.             return;
  300.  
  301.         for (E e : this.items) {
  302.             try {
  303.                 action.accept(e);
  304.             }
  305.             catch (Throwable ignored) { }
  306.         }
  307.     }
  308.  
  309.     public void tryForEach(Consumer<? super E> action, Consumer<Throwable> exceptionHandler) {
  310.         if (this.items.isEmpty())
  311.             return;
  312.  
  313.         for (E e : this.items) {
  314.             try {
  315.                 action.accept(e);
  316.             }
  317.             catch (Throwable ex) {
  318.                 exceptionHandler.accept(ex);
  319.             }
  320.         }
  321.     }
  322.  
  323.     @Nonnull
  324.     @Override
  325.     public Iterator<E> iterator() {
  326.         return this.items.iterator();
  327.     }
  328. }
  329.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement