com.github.ormfux.common.utils.ArrayUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ormfux-common-util Show documentation
Show all versions of ormfux-common-util Show documentation
A small collection of Java utilities for every-day use
package com.github.ormfux.common.utils;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.function.Function;
import java.util.function.Predicate;
public final class ArrayUtils {
private ArrayUtils() {
throw new UnsupportedOperationException("This class is not supposed to be instantiated.");
}
@SuppressWarnings("unchecked")
public static T[] filter(final T[] array, final Predicate filter) {
return Arrays.stream(array)
.filter(filter)
.toArray(length -> (T[]) Array.newInstance(array.getClass().getComponentType(), length));
}
@SuppressWarnings("unchecked")
public static R[] map(final T[] array, final Function mappingFunction, Class targetType) {
return Arrays.stream(array)
.map(mappingFunction)
.toArray(length -> (R[]) Array.newInstance(targetType, length));
}
}