From ba446965dad004cb38679f0f0e1a526151d84213 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Wed, 29 Jul 2020 12:25:47 +0800 Subject: 5.x - 20w30a Signed-off-by: shedaniel --- .../me/shedaniel/rei/utils/CollectionUtils.java | 285 --------------------- 1 file changed, 285 deletions(-) delete mode 100644 src/main/java/me/shedaniel/rei/utils/CollectionUtils.java (limited to 'src/main/java/me/shedaniel/rei/utils/CollectionUtils.java') diff --git a/src/main/java/me/shedaniel/rei/utils/CollectionUtils.java b/src/main/java/me/shedaniel/rei/utils/CollectionUtils.java deleted file mode 100644 index 33f8ef457..000000000 --- a/src/main/java/me/shedaniel/rei/utils/CollectionUtils.java +++ /dev/null @@ -1,285 +0,0 @@ -/* - * This file is licensed under the MIT License, part of Roughly Enough Items. - * Copyright (c) 2018, 2019, 2020 shedaniel - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package me.shedaniel.rei.utils; - -import com.google.common.collect.Lists; -import com.google.common.collect.Sets; -import me.shedaniel.rei.api.EntryStack; -import net.fabricmc.api.EnvType; -import net.fabricmc.api.Environment; - -import java.util.*; -import java.util.function.Function; -import java.util.function.Predicate; -import java.util.function.Supplier; -import java.util.stream.Collectors; - -public class CollectionUtils { - public static List getOrPutEmptyList(Map> map, A key) { - List b = map.get(key); - if (b != null) - return b; - map.put(key, Lists.newArrayList()); - return map.get(key); - } - - public static T findFirstOrNullEquals(List list, T obj) { - for (T t : list) { - if (t.equals(obj)) - return t; - } - return null; - } - - public static List castAndMap(List list, Class castClass) { - List l = new ArrayList<>(); - for (T t : list) { - if (castClass.isAssignableFrom(t.getClass())) - l.add((R) t); - } - return l; - } - - public static T findFirstOrNull(List list, Predicate predicate) { - for (T t : list) { - if (predicate.test(t)) - return t; - } - return null; - } - - public static boolean anyMatch(List list, Predicate predicate) { - for (T t : list) { - if (predicate.test(t)) - return true; - } - return false; - } - - @Environment(EnvType.CLIENT) - public static boolean anyMatchEqualsAll(List list, EntryStack stack) { - for (EntryStack t : list) { - if (t.equalsAll(stack)) - return true; - } - return false; - } - - @Environment(EnvType.CLIENT) - public static boolean anyMatchEqualsEntryIgnoreAmount(List list, EntryStack stack) { - for (EntryStack t : list) { - if (t.equalsIgnoreAmount(stack)) - return true; - } - return false; - } - - @Environment(EnvType.CLIENT) - public static EntryStack firstOrNullEqualsAll(List list, EntryStack stack) { - for (EntryStack t : list) { - if (t.equalsAll(stack)) - return t; - } - return null; - } - - @Environment(EnvType.CLIENT) - public static EntryStack findFirstOrNullEqualsEntryIgnoreAmount(Collection list, EntryStack stack) { - for (EntryStack t : list) { - if (t.equalsIgnoreAmount(stack)) - return t; - } - return null; - } - - public static List filter(List list, Predicate predicate) { - List l = Lists.newArrayList(); - for (T t : list) { - if (predicate.test(t)) { - l.add(t); - } - } - return l; - } - - public static Set filter(Set list, Predicate predicate) { - Set l = Sets.newLinkedHashSet(); - for (T t : list) { - if (predicate.test(t)) { - l.add(t); - } - } - return l; - } - - public static List filterSetToList(Set list, Predicate predicate) { - List l = Lists.newArrayList(); - for (T t : list) { - if (predicate.test(t)) { - l.add(t); - } - } - return l; - } - - public static List map(List list, Function function) { - List l = new ArrayList<>(list.size() + 1); - for (T t : list) { - l.add(function.apply(t)); - } - return l; - } - - public static List map(Collection list, Function function) { - List l = new ArrayList<>(list.size() + 1); - for (T t : list) { - l.add(function.apply(t)); - } - return l; - } - - public static List mapParallel(Collection list, Function function) { - return list.parallelStream().map(function).collect(Collectors.toList()); - } - - public static > C mapParallel(Collection list, Function function, Supplier supplier) { - return list.parallelStream().map(function).collect(Collectors.toCollection(supplier)); - } - - public static List map(T[] list, Function function) { - List l = new ArrayList<>(list.length + 1); - for (T t : list) { - l.add(function.apply(t)); - } - return l; - } - - public static Optional mapAndMax(List list, Function function, Comparator comparator) { - if (list.isEmpty()) - return Optional.empty(); - List copyOf = CollectionUtils.map(list, function); - copyOf.sort(comparator); - return Optional.ofNullable(copyOf.get(copyOf.size() - 1)); - } - - public static Optional mapAndMax(T[] list, Function function, Comparator comparator) { - if (list.length <= 0) - return Optional.empty(); - List copyOf = CollectionUtils.map(list, function); - copyOf.sort(comparator); - return Optional.ofNullable(copyOf.get(copyOf.size() - 1)); - } - - public static Optional max(List list, Comparator comparator) { - if (list.isEmpty()) - return Optional.empty(); - ArrayList ts = new ArrayList<>(list); - ts.sort(comparator); - return Optional.ofNullable(ts.get(ts.size() - 1)); - } - - public static Optional max(T[] list, Comparator comparator) { - if (list.length <= 0) - return Optional.empty(); - T[] copyOf = list.clone(); - Arrays.sort(copyOf, comparator); - return Optional.ofNullable(copyOf[copyOf.length - 1]); - } - - public static String joinToString(List list, String separator) { - StringJoiner joiner = new StringJoiner(separator); - for (String t : list) { - joiner.add(t); - } - return joiner.toString(); - } - - public static String joinToString(String[] list, String separator) { - StringJoiner joiner = new StringJoiner(separator); - for (String t : list) { - joiner.add(t); - } - return joiner.toString(); - } - - public static String mapAndJoinToString(List list, Function function, String separator) { - StringJoiner joiner = new StringJoiner(separator); - for (T t : list) { - joiner.add(function.apply(t)); - } - return joiner.toString(); - } - - public static String mapAndJoinToString(T[] list, Function function, String separator) { - StringJoiner joiner = new StringJoiner(separator); - for (T t : list) { - joiner.add(function.apply(t)); - } - return joiner.toString(); - } - - public static List filterAndMap(List list, Predicate predicate, Function function) { - List l = null; - for (T t : list) { - if (predicate.test(t)) { - if (l == null) - l = Lists.newArrayList(); - l.add(function.apply(t)); - } - } - return l == null ? Collections.emptyList() : l; - } - - public static int sumInt(List list, Function function) { - int sum = 0; - for (T t : list) { - sum += function.apply(t); - } - return sum; - } - - public static int sumInt(List list) { - int sum = 0; - for (int t : list) { - sum += t; - } - return sum; - } - - public static double sumDouble(List list, Function function) { - double sum = 0; - for (T t : list) { - sum += function.apply(t); - } - return sum; - } - - public static double sumDouble(List list) { - double sum = 0; - for (double t : list) { - sum += t; - } - return sum; - } -} -- cgit