Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;

public class Main {

    public static <T> List<List<T>> popAllSubList(List<T> originList, T fromObj, T toObj) {
        List<List<T>> result = new ArrayList<List<T>>();
        for (List<T> subList = popFirstSubList(originList, fromObj, toObj); isNotEmpty(
                subList); subList = popFirstSubList(originList, fromObj, toObj)) {
            result.add(subList);
        }
        return result;
    }

    public static <T> List<T> popFirstSubList(List<T> originList, T fromObj, T toObj) {
        int fromIndex = originList.indexOf(fromObj);
        if (fromIndex == -1) {
            return null;
        }
        int toIndex = originList.indexOf(toObj) + 1;
        if (toIndex == 0) {
            return null;
        }
        return popSubList(originList, fromIndex, toIndex);
    }

    public static boolean isNotEmpty(Collection<?> objs) {
        return objs != null && objs.size() > 0;
    }

    public static boolean isNotEmpty(Map<?, ?> map) {
        return !isEmpty(map);
    }

    public static <T> List<T> popSubList(List<T> originList, int fromIndex, int toIndex) {
        List<T> subList = originList.subList(fromIndex, toIndex);
        List<T> result = new ArrayList<T>(subList);
        subList.clear();
        return result;
    }

    public static boolean isEmpty(Collection<?> objs) {
        return objs == null || objs.size() == 0;
    }

    public static boolean isEmpty(Map<?, ?> map) {
        return map == null || map.size() == 0;
    }

    public static List<?> subList(List<?> origin, int count) {
        if (origin == null || count <= 0) {
            return origin;
        }
        if (origin.size() < count) {
            count = origin.size();
        }
        return origin.subList(0, count);
    }
}