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

com.gitee.feizns.ArrayUtils Maven / Gradle / Ivy

package com.gitee.feizns;

import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Objects;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 数组
 * @author feizns
 * @since 2019/5/13
 */
public abstract class ArrayUtils {

    /**
     * 查找第一个元素的下标
     * @param test
     * @param array
     * @return
     */
    public static  int findIndex(Predicate test, T... array) {
        if ( array != null ) {
            for (int i = 0; i < array.length; i++) {
                if ( test.test(array[i]) )
                    return i;
            }
        }
        return -1;
    }

    /**
     * @see ArrayUtils#findIndex(java.util.function.Predicate, java.lang.Object[])
     * @param test
     * @param array
     * @return
     */
    public static final  int indexOf(Predicate test, T... array) {
        return findIndex(test, array);
    }

    /**
     * 查找第一个元素
     * @param test
     * @param array
     * @return
     */
    public static  T findFirst(Predicate test, T... array) {
        int index = findIndex(test, array);
        return index != -1 ? array[index] : null;
    }

    /**
     * 将数组包装成iterator
     * @param ele
     * @param 
     * @return
     */
    public static  Iterator iterator(T... ele) {
        return ele != null ? Arrays.stream(ele).iterator() : null;
    }

    /**
     * 将数组包装成iterable
     * @param ele
     * @param 
     * @return
     */
    public static  Iterable iterable(T... ele) {
        return () -> iterator(ele);
    }

    /**
     * 数组是否为空
     * @param ele
     * @param 
     * @return
     */
    public static final  boolean isEmpty(T... ele) {
        return ele == null || ele.length == 0;
    }

    /**
     * 遍历一个未知的数组
     * @param array
     * @param consumer
     */
    public static final void forEach(Object array, Consumer consumer) {
        if ( array != null && array.getClass().isArray() ) {
            for (int i = 0; i < Array.getLength(array); i++) {
                consumer.accept(Array.get(array, i));
            }
        }
    }

    /**
     *
     * @param array
     * @param 
     * @return
     */
    public static final  Stream stream(Object array) {
        Stream.Builder builder = Stream.builder();
        if ( array != null && array.getClass().isArray() ) {
            for (int i = 0; i < Array.getLength(array); i++) {
                builder.add((T) Array.get(array, i));
            }
        }
        return builder.build();
    }

    /**
     * 将数组转换成用逗号分隔的字符串
     * @param arr
     * @param 
     * @return [1,2,3] => "1,2,3"
     */
    public static  String toString(T... arr) {
        return toString(",", arr);
    }

    /**
     * 将数组转换成用逗号分隔的字符串
     * @param delimiter
     * @param arr
     * @param 
     * @return [1,2,3] if delimiter = "?" => "1?2?3"
     */
    public static  String toString(String delimiter, T... arr) {
        return  arr != null
                ? Arrays.stream(arr)
                .map(Objects::toString)
                .collect(Collectors.joining(delimiter))
                : null;
    }

}