All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.ormfux.common.utils.ArrayUtils Maven / Gradle / Ivy

There is a newer version: 1.6.2
Show newest version
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));
    }
    
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy