Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package dc.j8common.statics;
- import dc.j8common.func.TriConsumer;
- import java.util.*;
- import java.util.function.*;
- import java.util.stream.Stream;
- import java.util.stream.StreamSupport;
- import static java.util.stream.Collectors.toList;
- import static java.util.stream.Collectors.toMap;
- /** Collections */
- public final class Cs {
- //region Collections
- public static <A> int indexOf(Iterable<A> iter, Predicate<A> predicate) {
- int i = 0;
- for (A a : iter) {
- if (predicate.test(a)) {
- return i;
- }
- i++;
- }
- return -1;
- }
- public static <B, A extends Collection<B>> A raze(A in, A out, int count) {
- for (B b : in) {
- if (count-- < 0) {
- return out;
- }
- out.add(b);
- }
- return out;
- }
- public static <T, A extends Collection<T>> A add(A col, T elem) {
- col.add(elem);
- return col;
- }
- public static <T, A extends Collection<T>> A remove(A col, T elem) {
- col.remove(elem);
- return col;
- }
- public static <T, A extends Collection<T>, B extends Collection<T>> A addAll(A col1, B col2) {
- col1.addAll(col2);
- return col1;
- }
- public static <T, A extends Collection<T>, B extends Collection<T>> A addAll(A col1, B... cols) {
- for (B col : cols) {
- col1.addAll(col);
- }
- return col1;
- }
- public static <T, A extends Collection<T>, B extends Collection<T>> A removeAll(A col1, B col2) {
- col1.removeAll(col2);
- return col1;
- }
- public static <T> boolean addAll(Collection<T> addTo, Iterator<? extends T> iterator) {
- boolean wasModified;
- for (wasModified = false; iterator.hasNext(); wasModified |= addTo.add(iterator.next())) {
- ;
- }
- return wasModified;
- }
- public static <A extends Collection> boolean isNullOrEmpty(A collection) {
- return !(collection != null && collection.size() != 0);
- }
- //endregion
- //region Lists
- public static <T, A extends List<T>> A sort(A col1, Comparator<T> comp) {
- col1.sort(comp);
- return col1;
- }
- public static <T extends Comparable<T>, A extends List<T>> A sort(A col1) {
- col1.sort(Comparable::compareTo);
- return col1;
- }
- public static <A, B> List<B> map(List<A> lst, Function<A, B> f) {
- return lst.stream().map(f).collect(toList());
- }
- public static <A> Optional<A> last(List<A> lst) {
- if (lst.size() == 0) {
- return Optional.empty();
- }
- return Optional.ofNullable(lst.get(lst.size() - 1));
- }
- public static <A> List<A> list(Iterable<? extends A> iterable) {
- return Cs.stream(iterable).collect(toList());
- }
- //endregion
- //region Maps
- public static <K, V> Map<K, V> filter(Map<K, V> map, BiPredicate<K, V> filter) {
- return map.entrySet().stream()
- .filter(kvEntry -> filter.test(kvEntry.getKey(), kvEntry.getValue()))
- .collect(toMap(kvEntry -> kvEntry.getKey(), kvEntry -> kvEntry.getValue()));
- }
- /** if no value is contained in map - add the value provided by a supplier, otherwise use remapping */
- public static <A, B> B compute(Map<A, B> map, A key, BiFunction<A, B, B> remapping, Supplier<B> startingValue) {
- return map.compute(key, (a, b) -> b == null ? startingValue.get() : remapping.apply(a, b));
- }
- /** if no value is contained in map - add the value provided by a supplier, but in any case use remapping */
- public static <A, B> B computeAndApply(Map<A, B> map, A key, BiFunction<A, B, B> remap,
- Supplier<B> startingValueThenRemap) {
- return map.compute(key, (a, b) -> b == null ? remap.apply(a, startingValueThenRemap.get()) : remap.apply(a, b));
- }
- /** invoke consumer if value exists */
- public static <A, B> void doIfExist(Map<A, B> map, A key, Consumer<B> doIfExist) {
- B b = map.get(key);
- if (b != null) {
- doIfExist.accept(b);
- }
- }
- /** invoke one consumer if value exists and another otherwise */
- public static <A, B> void doInBothCases(Map<A, B> map, A key, Consumer<B> doIfExist, Runnable doIfNotExist) {
- B b = map.get(key);
- if (b != null) {
- doIfExist.accept(b);
- } else {
- doIfNotExist.run();
- }
- }
- public static <T, X, A extends Map<T, X>, B extends Map<T, X>> A putAll(A collector, B map) {
- collector.putAll(map);
- return collector;
- }
- public static <T, X, A extends Map<T, X>> A put(A map, T key, X val) {
- map.put(key, val);
- return map;
- }
- public static <T, A extends Map<T, ?>> A remove(A map, T elem) {
- map.remove(elem);
- return map;
- }
- //endregion
- //region Iteration and mapping: index, synchro, eachWithEach
- /** index in iteration */
- public static <A> void forEachWithIndex(Iterable<A> iter, BiConsumer<A, Integer> consumer) {
- int i = 0;
- for (A next : iter) {
- consumer.accept(next, i++);
- }
- }
- /** each element from one collection is applied to each element from another collection */
- public static <A, B> void forEachWithEach(Iterable<A> t1, Iterable<B> t2, BiConsumer<A, B> consumer) {
- for (A a : t1) {
- for (B b : t2) {
- consumer.accept(a, b);
- }
- }
- }
- /** each element from one collection is applied to the element with the same index in another collection */
- public static <A, B> void forEachSynchro(Iterable<A> t1, Iterable<B> t2, BiConsumer<A, B> consumer) {
- forEachSynchro(t1, t2, (a, b, i) -> consumer.accept(a, b));
- }
- /** each element from one collection is applied to the element with the same index in another collection */
- public static <A, B> void forEachSynchro(Iterable<A> t1, Iterable<B> t2, TriConsumer<A, B, Integer> consumerWithIndex) {
- Iterator<A> it1 = t1.iterator();
- Iterator<B> it2 = t2.iterator();
- int i = 0;
- while (it1.hasNext() || it2.hasNext()) {
- A a = (it1.hasNext()) ? it1.next() : null;
- B b = (it2.hasNext()) ? it2.next() : null;
- consumerWithIndex.accept(a, b, i++);
- }
- }
- public static <A, B> Collection<B> mapEachWithIndex(Iterable<A> iter, BiFunction<A, Integer, B> function) {
- int i = 0;
- Collection<B> toRet = new ArrayList<>();
- for (A next : iter) {
- toRet.add(function.apply(next, i++));
- }
- return toRet;
- }
- public static <A, B, C> Collection<C> mapSyncro(Iterable<A> t1, Iterable<B> t2, BiFunction<A, B, C> consumer) {
- Collection<C> result = new ArrayList<>();
- Cs.forEachSynchro(t1, t2, (a, b) -> result.add(consumer.apply(a, b)));
- return result;
- }
- public static <A, B, C> Collection<C> mapEachWithEach(Iterable<A> t1, Iterable<B> t2,
- BiFunction<A, B, C> consumer) {
- Collection<C> result = new ArrayList<>();
- Cs.forEachWithEach(t1, t2, (a, b) -> result.add(consumer.apply(a, b)));
- return result;
- }
- //endregion
- //region String joiners
- public static <A, B extends Iterable<A>> List<String> str(B a) {
- return str(a, Objects::toString);
- }
- public static <A, B extends Iterable<A>> List<String> str(B a, Function<A, String> toStringer) {
- return StreamSupport.stream(a.spliterator(), false).map(toStringer::apply).collect(toList());
- }
- public static <A, B extends Iterable<A>> String join(B a) {
- return join(",", a);
- }
- public static <A, B extends Iterable<A>> String join(String delimeter, B a) {
- return join(delimeter, a, Objects::toString);
- }
- public static <A, B extends Iterable<A>> String join(B a, Function<A, String> toStringer) {
- return join(",", a, toStringer);
- }
- public static <A, B extends Iterable<A>> String join(String delimeter, B a, Function<A, String> toStringer) {
- return String.join(delimeter, str(a, toStringer));
- }
- public static <A, B extends Stream<A>> String join(B a) {
- return join(",", a);
- }
- public static <A, B extends Stream<A>> String join(String delimeter, B a) {
- return join(delimeter, a, Objects::toString);
- }
- public static <A, B extends Stream<A>> String join(B a, Function<A, String> toStringer) {
- return join(",", a, toStringer);
- }
- public static <A, B extends Stream<A>> String join(String delimeter, B a, Function<A, String> toStringer) {
- return String.join(delimeter, str(a.collect(toList()), toStringer));
- }
- public static <K, V, B extends Map<K, V>> String
- joinMap(B a) {
- return joinMap(a, (k, v) -> k.toString(), (k, v) -> v.toString());
- }
- public static <K, V, B extends Map<K, V>> String
- joinMap(B a, BiFunction<K, V, String> toStringerKey, BiFunction<K, V, String> toStringerValue) {
- return joinMap("|", ",", a, toStringerKey, toStringerValue);
- }
- public static <K, V, B extends Map<K, V>> String
- joinMap(String itemDelimeter, String keyValDelimeter, B a) {
- return joinMap(itemDelimeter, keyValDelimeter, a, (k, v) -> k.toString(), (k, v) -> v.toString());
- }
- public static <K, V, B extends Map<K, V>> String
- joinMap(String itemDelimeter, String keyValDelimeter,
- B a, BiFunction<K, V, String> toStringerKey, BiFunction<K, V, String> toStringerValue) {
- return join(itemDelimeter, a.entrySet(),
- o -> toStringerKey.apply(o.getKey(), o.getValue()) + keyValDelimeter +
- toStringerValue.apply(o.getKey(), o.getValue()));
- }
- //endregion
- //region Creators
- @SafeVarargs
- public static <T> List<T> of(T... ts) {
- return new ArrayList<>(java.util.Arrays.asList(ts));
- }
- @SafeVarargs
- public static <T> T[] ofA(T... ts) {
- return ts;
- }
- @SafeVarargs
- public static <T> Queue<T> ofQ(T... ts) {
- return new LinkedList<>(java.util.Arrays.asList(ts));
- }
- @SafeVarargs
- public static <T> Set<T> ofS(T... ts) {
- return new HashSet<>(java.util.Arrays.asList(ts));
- }
- @SafeVarargs
- public static <T> Set<T> ofTS(T... ts) {
- return new TreeSet<>(java.util.Arrays.asList(ts));
- }
- /**
- * odd - key, even - value
- */
- public static <K, V> Map<K, V> ofM(Object... ts) {
- Map<K, V> ret = new HashMap<>();
- return fillMap(ret, ts);
- }
- /**
- * odd - key, even - value
- */
- public static <K, V> Map<K, V> ofTM(Object... ts) {
- Map<K, V> ret = new TreeMap<>();
- return fillMap(ret, ts);
- }
- private static <K, V> Map<K, V> fillMap(Map<K, V> ret, Object[] ts) {
- int step = 0;
- K key = null;
- V val = null;
- for (Object t : ts) {
- if (step == 0) {
- key = (K) t;
- step++;
- } else if (step == 1) {
- val = (V) t;
- ret.put(key, val);
- step = 0;
- }
- }
- if (step != 0) {
- throw new RuntimeException("Map unfilled!");
- }
- return ret;
- }
- //endregion
- //region Streams
- public static <T> Stream<T> stream(Iterable<T> iterable) {
- return StreamSupport.stream(iterable.spliterator(), false);
- }
- public static <T> Stream<T> stream(T... arr) {
- return Stream.of(arr);
- }
- @SuppressWarnings("unchecked")
- public static <T> Stream<T> addStream(Stream<T> stream1, Stream<T> stream2) {
- return Stream.concat(stream1, stream2);
- }
- @SuppressWarnings("unchecked")
- public static <T> Stream<T> addStream(Stream<T> stream, T... items) {
- return Stream.concat(stream, Stream.of(items));
- }
- public static <T> Stream<T> addStream(Stream<T> stream, Collection<T> items) {
- return Stream.concat(stream, items.stream());
- }
- //endregion
- //region Enumeration
- public static <E> Iterable<E> iterable(final Enumeration<E> enumeration) {
- Objects.requireNonNull(enumeration);
- return () -> new Iterator<E>() {
- public boolean hasNext() {
- return enumeration.hasMoreElements();
- }
- public E next() {
- return enumeration.nextElement();
- }
- public void remove() {
- throw new UnsupportedOperationException();
- }
- };
- }
- //endregion
- //region Private
- private Cs() {
- }
- //endregion
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement