package com.woqutz.didi.util;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import org.apache.commons.lang.ArrayUtils;
public class CollectionUtils {
/**
* 分批处理数组的工具类
*
* @param collections
* 需要处理的数组集合
* @param callBack
* 处理数组的回调函数
* @param size
* 每批处理的个数
*/
@SuppressWarnings("unchecked")
public static void splitCollectionHandle(List collections,
ListCollectionCallback callBack, int size) {
if (collections == null || collections.isEmpty())
return;
int collectionSize = collections.size();
if (collectionSize < size) {
callBack.call(collections);
return;
}
int forCount = collectionSize / size;
int mode = collectionSize % size;
Object[] objectArr = collections.toArray();
for (int i = 0; i < forCount; i++) {
Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
List objectList = (List) Arrays.asList(tempArr);
callBack.call(objectList);
}
if (mode > 0) {
Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
List objectList = (List) Arrays.asList(tempArr);
callBack.call(objectList);
}
}
/**
* 分组调用callBack里的业务,异步调用,全部组调用完后同步
*
* @param collections
* 需要处理的数组集合
* @param callBack
* 处理数组的回调函数
* @param size
* 每批处理的个数
*/
@SuppressWarnings("unchecked")
public static void splitCollectionAsyncCall(List collections,
ListCollectionCallback callBack, int size) throws InterruptedException, ExecutionException,Exception{
if (collections == null || collections.isEmpty())
return;
int collectionSize = collections.size();
if (collectionSize < size) {
callBack.call(collections);
return;
}
class AsyncCall implements Runnable{
ListCollectionCallback callBack=null;
List objectList = null;
public AsyncCall(ListCollectionCallback callBack,List objectList){
this.callBack =callBack;
this.objectList = objectList;
}
public void run(){
callBack.call(objectList);
}
}
int forCount = collectionSize / size;
int mode = collectionSize % size;
final ExecutorService exePool = Executors.newFixedThreadPool(forCount+(mode>0?1:0));//每个组一个线程
final List> dataList = new ArrayList>();
Object[] objectArr = collections.toArray();
for (int i = 0; i < forCount; i++) {
Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
List objectList = (List) Arrays.asList(tempArr);
Future> future = (Future>) exePool.submit(new AsyncCall(callBack,objectList)) ;
dataList.add(future);
}
if (mode > 0) {
Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
List objectList = (List) Arrays.asList(tempArr);
Future> future = (Future>) exePool.submit(new AsyncCall(callBack,objectList)) ;
dataList.add(future);
}
List exceptionlist = new ArrayList();
for (Future> future : dataList) {//同步
try {
future.get();
} catch (InterruptedException e) {
exceptionlist.add(e);
} catch (ExecutionException e) {
exceptionlist.add(e);
} catch (Exception e) {
exceptionlist.add(e);
}
}
if(!exceptionlist.isEmpty()){
exePool.shutdown();
throw exceptionlist.get(0);
}
exePool.shutdown();
}
public static void main(String[] args){
return;
}
public static void splitCollectionHandle(Collection collections,
CollectionCallback callBack, int size) {
if (collections == null || collections.isEmpty())
return;
int collectionSize = collections.size();
if (collectionSize < size) {
callBack.call(collections);
return;
}
int forCount = collectionSize / size;
int mode = collectionSize % size;
Object[] objectArr = collections.toArray();
for (int i = 0; i < forCount; i++) {
Object[] tempArr = ArrayUtils.subarray(objectArr, i * size, (i + 1) * size);
List objectList = (List) Arrays.asList(tempArr);
callBack.call(objectList);
}
if (mode > 0) {
Object[] tempArr = ArrayUtils.subarray(objectArr, size * forCount, size * forCount + mode);
List objectList = (List) Arrays.asList(tempArr);
callBack.call(objectList);
}
}
public static Map changeDbStyleKeyToJavaStyleKey(Map sourceMap) {
if (null == sourceMap) {
return sourceMap;
}
Map targetMap = new HashMap();
for (Entry entry : sourceMap.entrySet()) {
String key = entry.getKey();
Object value = entry.getValue();
targetMap.put(StringUtil.changeDbcloumStyleToJavaStyle(key), value);
}
return targetMap;
}
/**
* 把LIST中类型为简单对象类型INTERGER,LONG ...里的值转为逗号分隔的串 1,2,3
* 满足IN查询需要
* @param list
* @return NULL或SIZE<1时返回 ''
* @create_time 2012-9-22 下午04:43:39
*/
public static String listToString(List> list){
if(list==null || list.size()<1){
return "";
}
StringBuffer s = new StringBuffer(512);
for(Object o :list){
s.append(o.toString().trim()+",");
}
if(s.length()>0) s.deleteCharAt(s.length()-1);
return s.toString();
}
public static String collectionToString(Collection> list){
if(list==null || list.size()<1){
return "";
}
StringBuffer s = new StringBuffer(512);
for(Object o :list){
s.append(o.toString().trim()+",");
}
if(s.length()>0) s.deleteCharAt(s.length()-1);
return s.toString();
}
/***
* 根据Map-value排序
* @param oriMap
* @return
*/
public static Map sortMapByValue(Map oriMap) {
Map sortedMap = new LinkedHashMap();
if (oriMap != null && !oriMap.isEmpty()) {
List> entryList = new ArrayList>(oriMap.entrySet());
Collections.sort(entryList,
new Comparator>() {
public int compare(Entry entry1,
Entry entry2) {
return entry1.getValue() - entry2.getValue();
}
});
Iterator> iter = entryList.iterator();
Map.Entry tmpEntry = null;
while (iter.hasNext()) {
tmpEntry = iter.next();
sortedMap.put(tmpEntry.getKey(), tmpEntry.getValue());
}
}
return sortedMap;
}
}