
com.indoqa.lang.util.CollectionUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of indoqa-lang Show documentation
Show all versions of indoqa-lang Show documentation
Stuff that would be expected being part of the Java standard library or Apache Commons Lang | IO.
The newest version!
/*
* Licensed to the Indoqa Software Design und Beratung GmbH (Indoqa) under
* one or more contributor license agreements. See the NOTICE file distributed
* with this work for additional information regarding copyright ownership.
* Indoqa licenses this file to You 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 com.indoqa.lang.util;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
public final class CollectionUtils {
private CollectionUtils() {
// hide utility constructor
}
@SafeVarargs
public static Set asSet(T... values) {
Set result = new HashSet<>();
for (T eachValue : values) {
result.add(eachValue);
}
return result;
}
/**
* Returns up to maxCount
elements from the beginning of items
.
* If items
contains less than or exactly maxCount
elements this method returns items
.
*
* This returned list will NOT be independent from items
! See {@link List#subList(int, int)}.
*
* @param the class of the objects in the list
* @param items The list to retrieve up to maxCount
elements from.
* @param maxCount The maximum number of elements to retrieve.
* @return The list of elements.
*/
public static List getHead(List items, int maxCount) {
if (items.size() <= maxCount) {
return items;
}
return items.subList(0, Math.min(items.size(), maxCount));
}
public static Map map(K key, V value) {
ConcurrentHashMap map = new ConcurrentHashMap();
map.put(key, value);
return map;
}
public static > T max(T... values) {
return getBest(new MaxComparison(), values);
}
public static > T min(T... values) {
return getBest(new MinComparison(), values);
}
private static > T getBest(Comparison comparison, T... values) {
if (values == null || values.length == 0) {
throw new IllegalArgumentException("No values provided!");
}
T result = null;
for (T value : values) {
if (value == null) {
continue;
}
if (comparison.isBetter(result, value)) {
result = value;
}
}
return result;
}
protected interface Comparison> {
boolean isBetter(T current, T candidate);
}
protected static final class MaxComparison> implements Comparison {
@Override
public boolean isBetter(T current, T candidate) {
return current == null || current.compareTo(candidate) < 0;
}
}
protected static final class MinComparison> implements Comparison {
@Override
public boolean isBetter(T current, T candidate) {
return current == null || current.compareTo(candidate) > 0;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy