io.devbench.uibuilder.api.utils.CollectionUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of uibuilder-api Show documentation
Show all versions of uibuilder-api Show documentation
API definitions for the UIBuilder Framework
/*
*
* Copyright © 2018 Webvalto Ltd.
*
* 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.devbench.uibuilder.api.utils;
import org.jetbrains.annotations.NotNull;
import java.lang.reflect.InvocationTargetException;
import java.util.*;
import java.util.function.Function;
public abstract class CollectionUtil {
private CollectionUtil() {
}
public static Function tryTo(ThrowingFunction throwingFunction) {
return t -> {
try {
return throwingFunction.apply(t);
} catch (Exception e) {
throw new RuntimeException(e);
}
};
}
public interface ThrowingFunction {
R apply(T t) throws Exception;
}
public static boolean isCollectionsEqual(@NotNull Collection collection1, @NotNull Collection collection2) {
List diff1 = new ArrayList<>(collection1);
List diff2 = new ArrayList<>(collection2);
diff1.removeAll(collection2);
diff2.removeAll(collection1);
return diff1.isEmpty() && diff2.isEmpty();
}
public static Collection instantiateCollection(Class> collectionType) {
try {
@SuppressWarnings("unchecked")
Collection collection = (Collection) collectionType.getConstructor().newInstance();
return collection;
} catch (InstantiationException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
// ignored
}
return Set.class.isAssignableFrom(collectionType) ? new HashSet<>() : new ArrayList<>();
}
}