/*
 *  @author Ranides Atterwim <ranides@gmail.com>
 *  @copyright Ranides Atterwim
 *  @license WTFPL
 *  @url http://ranides.net/projects/assira
 */

package net.ranides.assira.generic;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.function.ToIntFunction;
import net.ranides.assira.collection.maps.IntMap;
import net.ranides.assira.collection.maps.IntMapCollectors;
import net.ranides.assira.collection.maps.MapCollectors;

public final class EnumUtils {

    private EnumUtils() {
        // utility class
    }

    public static <K,E extends Enum<E>> Map<K, E> map(Class<E> eenum, Function<E,? extends K> function) {
        return Arrays.stream(eenum.getEnumConstants()).collect(MapCollectors.unique(function));
    }
    
    public static <E extends Enum<E>> IntMap<E> intmap(Class<E> eenum, ToIntFunction<E> function) {
        return Arrays.stream(eenum.getEnumConstants()).collect(IntMapCollectors.unique(function));
    }
    
    public static <K,E extends Enum<E>> Map<E,K> apply(Class<E> eenum, Function<E,? extends K> function) {
        return Arrays.stream(eenum.getEnumConstants()).collect(MapCollectors.unique(e -> e, e -> function.apply(e)));
    }
    
    public static <K,E extends Enum<E>> Map<E,K> apply(Class<E> eenum, K[] values) {
        E[] keys = eenum.getEnumConstants();
        if(keys.length != values.length) {
            throw new IllegalArgumentException("Different size of enum ("+keys.length+" and values array (" + values.length + ")");
        }
        return Arrays.stream(keys).collect(MapCollectors.unique(e -> e, e -> values[e.ordinal()]));
    }

}
