package com.pinghang.util;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import net.sf.json.JSONObject;
import com.mongodb.BasicDBObject;
import com.mongodb.DBObject;
public class TEST {
public static void main(String[] args) {
// String str = "{faceNumMap="+
// "{102=1, 2=1, 41=1, 79=1, 14=1, 44=2, 25=1, 484=1, 92=1, 95=1, 29=1, 31=2, 91=1, 151=1, 120=1, 30=1},"+
// "digitaMap={1=12121212,2=<1 ‘Va, \\%}"+
// "}";
// JSONObject jasonObject = JSONObject.fromObject(str);
// Map map = (Map)jasonObject;
//
// System.out.println(map);
// // Map<String, List<DBObject>> map = TEST.groupByField(TEST.init(),
// // "accountNumber");
// String[] cards = new String[] { "6220", "6221", "6222", "6223", "6224",
// "6225", "6226", "6227", "6228", "6229", };
//
// // TEST.sortByTime(TEST.init());
// TEST t = new TEST();
// List<String> list = new ArrayList<>();
// list.add("accountNumber");
// list.add("oppositeNumer");
// list.add("time");
// long start = System.currentTimeMillis();
// for (int i = 0, size = t.init().size(); i < size; i++) {
// System.out.println(t.init().get(i).toString());
// }
// long end = System.currentTimeMillis();
// System.out.println(end - start);
// t.groupByFields(t.init(), list);
// System.out.println(t.groupByFields(t.init(), list).toString());
// // System.out.println(JSONArray.fromObject(t.groupByFields(t.init(), list)));
}
public List<DBObject> init() {
String[] cards = new String[] { "6220", "6221", "6222", "6223", "6224",
"6225", "6226", "6227", "6228", "6229", };
List<DBObject> list = new ArrayList<DBObject>();
for (int i = 0; i < 10000; i++) {
int randomNum = (int) Math.floor(10 * Math.random());
int oppositeRandomNum = (int) Math.floor(10 * Math.random());
DBObject dBObject = new BasicDBObject();
dBObject.put("accountNumber", cards[randomNum]);
dBObject.put("oppositeNumer", cards[oppositeRandomNum]);
dBObject.put("time", addDate(new Date(), i * 30));
list.add(dBObject);
}
return list;
}
public static Map<String, List<DBObject>> groupByField(List<DBObject> list,
String field) {
Map<String, List<DBObject>> map = new HashMap<String, List<DBObject>>();
Set<String> keySet = new HashSet<String>();
Set<Object> valueSet = new HashSet<Object>();
for (DBObject dBObject : list) {
for (String key : dBObject.keySet()) {
keySet.add(key);
if (key.equals(field)) {
valueSet.add(dBObject.get(key));
}
}
}
for (Object value : valueSet) {
List<DBObject> returnList = new ArrayList<>();
for (DBObject dbObject : list) {
for (String key : keySet) {
if (key.equals(field)) {
if (dbObject.get(key).equals(value)) {
returnList.add(dbObject);
}
}
}
}
map.put((String) value, returnList);
}
return map;
}
public Map<DBObject, List<DBObject>> groupByFields(
List<DBObject> list, List<String> fields) {
Map<DBObject, List<DBObject>> map = new HashMap<DBObject, List<DBObject>>();
for (DBObject d : list) {
DBObject key = getGroupKey(fields, d);
if (map.get(key) != null) {
map.get(key).add(d);
} else {
List<DBObject> valList = new ArrayList<DBObject>();
valList.add(d);
map.put(key, valList);
}
}
return map;
}
public static Map<String, List<DBObject>> group(List<DBObject> list,
String field) {
Map<String, List<DBObject>> map = new HashMap<String, List<DBObject>>();
for (DBObject d : list) {
String value = String.valueOf(d.get(field));
if (map.get(value) != null) {
map.get(value).add(d);
} else {
List<DBObject> valList = new ArrayList<DBObject>();
valList.add(d);
map.put(value, valList);
}
}
return map;
}
public static Date addDate(Date date, int minutes) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(date);
calendar.add(Calendar.MINUTE, minutes);
Date endDate = calendar.getTime();
return endDate;
}
public static List<DBObject> sortByTime(List<DBObject> list) {
TimeComparator timeComparator = new TimeComparator();
Collections.sort(list, timeComparator);
return list;
}
public DBObject getGroupKey(List<String> fields, DBObject d) {
DBObject key = new BasicDBObject();
for (String f : fields) {
key.put(f, d.get(f));
}
return key;
}
public String getGroupJsonKey(List<String> fields, DBObject dBObject) {
StringBuffer stringBuffer = new StringBuffer("{");
for (int i = 0; i < fields.size(); i++) {
stringBuffer.append("\"");
stringBuffer.append(fields.get(i));
stringBuffer.append("\":\"");
stringBuffer.append(String.valueOf(dBObject.get(fields.get(i))));
if (i == fields.size() - 1) {
stringBuffer.append("\"");
} else {
stringBuffer.append("\",");
}
}
stringBuffer.append("}");
return stringBuffer.toString();
}
}
class TimeComparator implements Comparator<Object> {
@Override
public int compare(Object object1, Object object2) {
try {
DBObject dBObject1 = (DBObject) object1;
DBObject dBObject2 = (DBObject) object2;
Date date1 = (Date) dBObject1.get("time");
Date date2 = (Date) dBObject2.get("time");
Long time1 = date1.getTime();
Long time2 = date2.getTime();
int flag = time1.compareTo(time2);
return flag;
} catch (Exception e) {
e.printStackTrace();
return 0;
}
}
}