package com.sheep.gamegroup.util;


import java.util.ArrayList;
import java.util.List;

/**
 * Created by realicing on 2018/5/3.
 * realicing@sina.com
 */
public class ListUtil {
    public static <T> boolean hasIndex(List<T> list, int index) {
        return !ListUtil.isEmpty(list) && list.size() > index;
    }

    private static <T> boolean isEmpty(List<T> list) {
        return list == null || list.isEmpty();
    }

    public static <T> T getIndex(List<T> list, int index) {
        return hasIndex(list, index) ? list.get(index) : null;
    }


    public static int getTotalPage(int count, int per_page) {
        return (count - 1) / per_page + 1;
    }

    public static <T> ArrayList<T> emptyList() {
        return new ArrayList<>();
    }

    public static <T> int addAll(List<T> list, List<? extends T> elements) {
        int count = 0;
        for (T element : elements) {
            int index = list.indexOf(element);
            if(index != -1){
                list.set(index, element);
            } else if(list.add(element)){
                count++;
            }
        }
        return count;
    }

    public static <T> boolean isLastPosition(List<T> list, int position) {
        return size(list) == position + 1;
    }

    private static <T> int size(List<T> list) {
        return isEmpty(list) ? 0 : list.size();
    }
}
