io.github.pellse.util.collection.CollectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of assembler Show documentation
Show all versions of assembler Show documentation
Small library allowing to efficiently assemble entities from querying/merging external datasources or aggregating microservices
The newest version!
/*
* Copyright 2024 Sebastien Pelletier
*
* 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.github.pellse.util.collection;
import java.util.*;
import java.util.Map.Entry;
import java.util.function.*;
import java.util.stream.Stream;
import static io.github.pellse.util.ObjectUtils.also;
import static io.github.pellse.util.ObjectUtils.ifNotNull;
import static java.util.Collections.emptySet;
import static java.util.LinkedHashMap.newLinkedHashMap;
import static java.util.Map.entry;
import static java.util.function.Function.identity;
import static java.util.stream.Collectors.*;
import static java.util.stream.StreamSupport.stream;
public interface CollectionUtils {
static > Stream toStream(C iterable) {
return iterable != null ? stream(iterable.spliterator(), false) : Stream.empty();
}
static > Stream concat(C collection1, C collection2) {
return Stream.concat(toStream(collection1), toStream(collection2));
}
static boolean isEmpty(Iterable> iterable) {
return iterable == null ||
(iterable instanceof Collection> coll && coll.isEmpty()) ||
!iterable.iterator().hasNext();
}
static boolean isNotEmpty(Iterable> iterable) {
return !isEmpty(iterable);
}
static boolean isEmpty(Map, ?> map) {
return map == null || map.isEmpty();
}
static boolean isNotEmpty(Map, ?> map) {
return !isEmpty(map);
}
static Map nullToEmptyMap(Map value) {
return nullToEmptyMap(value, Map::of);
}
static > T nullToEmptyMap(T value, Supplier defaultFactory) {
return value != null ? value : defaultFactory.get();
}
static List transform(Iterable extends T> from, Function mappingFunction) {
return toStream(from).map(mappingFunction).toList();
}
static Collection asCollection(Iterable iterable) {
return iterable instanceof Collection coll ? coll : stream(iterable.spliterator(), false).toList();
}
static > C translate(Iterable extends E> from, Supplier collectionFactory) {
return asCollection(from).stream().collect(toCollection(collectionFactory));
}
static int size(Iterable> iterable) {
return iterable == null ? 0 : asCollection(iterable).size();
}
static Map transformMapKeys(Map map, Function keyMapper) {
return transformMapKeys(map, (key, __) -> keyMapper.apply(key));
}
static Map transformMapKeys(Map map, BiFunction keyMapper) {
return transformMap(map, keyMapper, (key, value) -> value);
}
static Map transformMapValues(Map map, Function valueMapper) {
return transformMapValues(map, (__, value) -> valueMapper.apply(value));
}
static Map transformMapValues(Map map, BiFunction valueMapper) {
return transformMap(map, (key, value) -> key, valueMapper);
}
static Map transformMap(Map map, BiFunction keyMapper, BiFunction valueMapper) {
return toLinkedHashMap(map.entrySet(), e -> keyMapper.apply(e.getKey(), e.getValue()), e -> valueMapper.apply(e.getKey(), e.getValue()));
}
@SafeVarargs
static Map newMap(Consumer