Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2020 ActiveJ LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.activej.common.collection;
import org.jetbrains.annotations.Nullable;
import java.util.*;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.function.Supplier;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toSet;
public class CollectionUtils {
public static List concat(Collection extends D> list1, Collection extends D> list2) {
List result = new ArrayList<>(list1.size() + list2.size());
result.addAll(list1);
result.addAll(list2);
return result;
}
@SafeVarargs
public static Set set(T... items) {
return new LinkedHashSet<>(asList(items));
}
public static Set difference(Set extends T> a, Set extends T> b) {
return a.stream().filter(t -> !b.contains(t)).collect(toSet());
}
public static Set intersection(Set extends T> a, Set extends T> b) {
return a.size() < b.size() ?
a.stream().filter(b::contains).collect(toSet()) :
b.stream().filter(a::contains).collect(toSet());
}
public static boolean hasIntersection(Set extends T> a, Set extends T> b) {
return a.size() < b.size() ?
a.stream().anyMatch(b::contains) :
b.stream().anyMatch(a::contains);
}
public static Set union(Set extends T> a, Set extends T> b) {
return Stream.concat(a.stream(), b.stream()).collect(toSet());
}
public static Set union(List> sets) {
return sets.stream().flatMap(Collection::stream).collect(toSet());
}
@SafeVarargs
public static Set union(Set extends T>... sets) {
return union(asList(sets));
}
public static T first(Iterable iterable) {
return iterable.iterator().next();
}
public static List list() {
return emptyList();
}
@SuppressWarnings("unchecked")
public static List list(T... items) {
return asList(items);
}
public static Stream iterate(Supplier supplier, Predicate hasNext) {
return iterate(supplier.get(), hasNext, $ -> supplier.get());
}
public static Stream iterate(T seed, Predicate hasNext, UnaryOperator f) {
requireNonNull(f);
return iterate(
new Iterator() {
T item = seed;
@Override
public boolean hasNext() {
return hasNext.test(item);
}
@Override
public T next() {
T next = item;
item = f.apply(item);
return next;
}
});
}
public static Stream iterate(Iterator iterator) {
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator,
Spliterator.ORDERED | Spliterator.IMMUTABLE), false);
}
public static String toLimitedString(Collection collection, int limit) {
return collection.stream()
.limit(limit)
.map(element -> element == collection ? "(this Collection)" : element.toString())
.collect(joining(",", "[", collection.size() <= limit ? "]" : ", ..and " + (collection.size() - limit) + " more]"));
}
public static String toLimitedString(Map map, int limit) {
return map.entrySet().stream()
.limit(limit)
.map(element -> {
K key = element.getKey();
V value = element.getValue();
String keyString = key == map ? "(this Map)" : key.toString();
String valueString = value == map ? "(this Map)" : value.toString();
return keyString + '=' + valueString;
})
.collect(joining(",", "{", map.size() <= limit ? "}" : ", ..and " + (map.size() - limit) + " more}"));
}
private static final Iterator