package com.github.hualuomoli.commons.util;

import java.util.List;

import com.google.common.collect.Lists;

/**
 * 集合工具类
 * @author hualuomoli
 *
 */
public class CollectionUtils {

	/**
	 * 从集合中获取指定位置的数据
	 * @param list 集合数据
	 * @param config 配置偏移和长度
	 * @return 指定位置的数据
	 */
	public static <T> List<T> fetchDatas(List<T> list, Config config) {
		if (list == null || list.size() == 0) {
			return list;
		}
		List<T> retList = Lists.newArrayList();

		// 开始位置和截取长度
		int offset = config.offset;
		int length = config.length;

		// 判断偏移量
		if (offset < 0) {
			offset = 0;
		}
		// 判断长度
		if (length <= 0) {
			length = 1;
		}
		// 获取数据的最后位置
		int end = offset + length;
		// 如果计算的位置比数据的长度大,最后的位置就是数据的长度
		if (end >= list.size()) {
			end = list.size();
		}
		for (int i = offset; i < end; i++) {
			retList.add(list.get(i));
		}

		// reset
		config.offset = end;
		config.length = length;

		return retList;
	}

	// 集合切分器
	public static class Config {

		private int offset;
		private int length;

		public Config() {
		}

		public Config(int length) {
			this.offset = 0;
			this.length = length;
		}

		public Config(int offset, int length) {
			this.offset = offset;
			this.length = length;
		}

	}

}

@@ -1201,10 +1201,508 @@
 %09%7D%0A%0A%09%7D%0A%0A
+%09/**%0A%09 * %E8%BF%87%E6%BB%A4%E9%9B%86%E5%90%88%0A%09 * @param list %E9%9B%86%E5%90%88%E6%95%B0%E6%8D%AE%0A%09 * @param filter %E8%BF%87%E6%BB%A4%E5%99%A8%0A%09 * @return %E8%BF%87%E6%BB%A4%E5%90%8E%E7%9A%84%E9%9B%86%E5%90%88%0A%09 */%0A%09public static %3CT%3E List%3CT%3E filter(List%3CT%3E list, Filter%3CT%3E filter) %7B%0A%0A%09%09if (list == null %7C%7C list.size() == 0 %7C%7C filter == null) %7B%0A%09%09%09return list;%0A%09%09%7D%0A%0A%09%09List%3CT%3E retList = Lists.newArrayList();%0A%09%09for (int i = 0; i %3C list.size(); i++) %7B%0A%09%09%09T t = list.get(i);%0A%09%09%09if (filter.valid(t)) %7B%0A%09%09%09%09retList.add(t);%0A%09%09%09%7D%0A%09%09%7D%0A%09%09return retList;%0A%0A%09%7D%0A%0A%09// %E8%BF%87%E6%BB%A4%E5%99%A8%0A%09public static interface Filter%3CT%3E %7B%0A%0A%09%09// %E6%98%AF%E5%90%A6%E6%9C%89%E6%95%88%0A%09%09boolean valid(T t);%0A%0A%09%7D%0A%0A
 %7D%0A

add filter