package com.pictc.utils;

import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
import java.util.concurrent.CopyOnWriteArrayList;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ListUtils {
	
    public static enum ListType{
    	ArrayList,LinkedList,Vector,CopyOnWriteArrayList
    }
    
    public static ListType parseType(Class<?> cls) {
    	if(LinkedList.class.equals(cls)) {
    		return ListType.LinkedList;
    	}else if(Vector.class.equals(cls)) {
    		return ListType.Vector;
    	}else if(CopyOnWriteArrayList.class.equals(cls)) {
    		return ListType.CopyOnWriteArrayList;
    	}else {
    		return ListType.ArrayList;
    	}
    }
    
 
    public static <T> List<T> getMapNewInstance(ListType type) {
    	List<T> result=null;
    	switch (type) {
		case LinkedList:
			result=CollectionUtils.newLinkedList();
			break;
		case Vector:
			result=CollectionUtils.newVector();
			break;
		case CopyOnWriteArrayList:
			result=CollectionUtils.newConcurrentArrayList();
			break;
		default:
			result=CollectionUtils.newArrayList();
			break;
		}
		return result;
    }
    
    public static <T>List<T> newInstance(Class<?> cls) {
    	return getMapNewInstance(parseType(cls));
    }
    

	public static <T> void forEach(@NonNull List<T> list,ListForEach<? super T> action){
		int i=0;
		Iterator<T> iterator=list.iterator();
		while (iterator.hasNext()) {
			action.accept(iterator.next(),i);
			i++;
		}
	}
	
	@FunctionalInterface
	public interface ListForEach<T>{
		
		void accept(T t,int index);
	}

    @SuppressWarnings("unchecked")
	public static <T> List<T> ofArray(T... objs){
    	List<T> result=null;
    	if(objs!=null && objs.length>0) {
    		result=CollectionUtils.newArrayList();
    		for (int i = 0; i < objs.length; i++) {
    			result.add(objs[i]);
			}
    	}
		return result;
    }
    
    
    @SuppressWarnings("unchecked")
    public static <T> List<T> newArrayList(T... objs){
    	List<T> result=null;
    	if(objs!=null && objs.length>0) {
    		result=CollectionUtils.newArrayList();
    		for (int i = 0; i < objs.length; i++) {
    			result.add(objs[i]);
    		}
    	}
    	return result;
    }

    @SuppressWarnings("unchecked")
	public static <T> List<T> ofCollection(Collection<?> list){
    	List<T> result=null;
    	if(list!=null && list.size()>0) {
    		result=CollectionUtils.newArrayList();
    		for (Object t : list) {
    			result.add((T)t);
			}
    	}
		return result;
    }

	public static <T> List<T> ofArrays(T[] objs){
    	List<T> result=null;
    	if(objs!=null && objs.length>0) {
    		result=CollectionUtils.newArrayList();
    		for (int i = 0; i < objs.length; i++) {
    			result.add(objs[i]);
			}
    	}
		return result;
    }
	
    public static <T>T getFirst(List<T> list){
		if(CollectionUtils.isNotEmpty(list)) {
			return list.get(0);
		}
		return null;
    }

}

