JsonArrayUtil(base on lambda and Stream)

来源:互联网 发布:seo是做什么 编辑:程序博客网 时间:2024/04/28 23:59
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
import java.util.stream.Collectors;
import com.google.common.base.Objects;
import com.google.common.base.Stopwatch;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.google.inject.internal.util.Sets;
/***
 * @author Andypan
 * @date 2017年7月18日 下午6:48:43
 */
public class JsonArrayUtil
{


/**

* @param jsonArray
* @param filterName
*           
* @param filterValue
*           
* @param sortName
*           
* @param sortOrder
*           
* @param limit
*          
* @return
*/
public static List<JsonObject> sortFilter(JsonArray jsonArray, String filterName, String filterValue,
String sortName, String sortOrder, long limit)
{
List<JsonObject> resultList = Lists.newArrayList();


List<JsonObject> tmpList = Lists.newArrayList();
int len = jsonArray.size();
for (int i = 0; i < len; i++)
{
tmpList.add(jsonArray.get(i).getAsJsonObject());
}
if (-1 == limit)
{
limit = Long.MAX_VALUE;
}
if (sortOrder.equalsIgnoreCase("desc") || sortOrder.equalsIgnoreCase("1"))
{
resultList = tmpList.parallelStream()
.filter(e -> Objects.equal(e.get(filterName).getAsString(), filterValue))
.sorted((p1, p2) -> p2.get(sortName).getAsString().compareTo(p1.get(sortName).getAsString()))
.limit(limit).collect(Collectors.toList());
} else if (sortOrder.equalsIgnoreCase("asc") || sortOrder.equalsIgnoreCase("-1"))
{
resultList = tmpList.parallelStream()
.filter(e -> Objects.equal(e.get(filterName).getAsString(), filterValue))
.sorted((p1, p2) -> p1.get(sortName).getAsString().compareTo(p2.get(sortName).getAsString()))
.limit(limit).collect(Collectors.toList());
} else
{
throw new IllegalArgumentException("unknow sort type!");
}
return resultList;
}


/**

* @param tmpList
* @param filterName
* @param filterValue
* @param sortName
* @param sortOrder
* @param limit
* @return
*/
public static List<JsonObject> sortFilter(List<JsonObject> tmpList, String filterName, String filterValue,
String sortName, String sortOrder, long limit)
{
if (-1 == limit)
{
limit = Long.MAX_VALUE;
}
if (sortOrder.equalsIgnoreCase("desc") || sortOrder.equalsIgnoreCase("1"))
{
tmpList = tmpList.parallelStream().filter(e -> Objects.equal(e.get(filterName).getAsString(), filterValue))
.sorted((p1, p2) -> p2.get(sortName).getAsString().compareTo(p1.get(sortName).getAsString()))
.limit(limit).collect(Collectors.toList());
} else if (sortOrder.equalsIgnoreCase("asc") || sortOrder.equalsIgnoreCase("-1"))
{
tmpList = tmpList.parallelStream().filter(e -> Objects.equal(e.get(filterName).getAsString(), filterValue))
.sorted((p1, p2) -> p1.get(sortName).getAsString().compareTo(p2.get(sortName).getAsString()))
.limit(limit).collect(Collectors.toList());
} else
{
throw new IllegalArgumentException("unknow sort type!");
}
return tmpList;
}


/**

* @param tmpList
* @param filterName
* @param filterValue
* @param sortName
* @param sortOrder
* @param limit
* @return
*/
public static Set<JsonObject> sortFilter(Set<JsonObject> tmpSet, String filterName, String filterValue,
String sortName, String sortOrder, long limit)
{
if (-1 == limit)
{
limit = Long.MAX_VALUE;
}
if (sortOrder.equalsIgnoreCase("desc") || sortOrder.equalsIgnoreCase("1"))
{
tmpSet = tmpSet.parallelStream()
.filter(e -> Objects.equal(GsonUtil.getAsString(e, filterName), filterValue)).sorted((p1,
p2) -> GsonUtil.getAsString(p2, sortName).compareTo(GsonUtil.getAsString(p1, sortName)))
.limit(limit).collect(Collectors.toSet());
} else if (sortOrder.equalsIgnoreCase("asc") || sortOrder.equalsIgnoreCase("-1"))
{
tmpSet = tmpSet.parallelStream()
.filter(e -> Objects.equal(GsonUtil.getAsString(e, filterName), filterValue)).sorted((p1,
p2) -> GsonUtil.getAsString(p1, sortName).compareTo(GsonUtil.getAsString(p2, sortName)))
.limit(limit).collect(Collectors.toSet());
} else
{
throw new IllegalArgumentException("unknow sort type!");
}
return tmpSet;
}


/**

* @param resultList
* @param sortName
* @param sortOrder
* @return
*/
public static List<JsonObject> sort(List<JsonObject> resultList, String sortName, String sortOrder)
{
if (sortOrder.equalsIgnoreCase("asc") || sortOrder.equalsIgnoreCase("-1"))
{
resultList = resultList.parallelStream()
.sorted((p1, p2) -> (p1.get(sortName).getAsString().compareTo(p2.get(sortName).getAsString())))
.collect(Collectors.toList());
} else if (sortOrder.equalsIgnoreCase("desc") || sortOrder.equalsIgnoreCase("1"))
{
resultList = resultList.parallelStream()
.sorted((p1, p2) -> (p2.get(sortName).getAsString().compareTo(p1.get(sortName).getAsString())))
.collect(Collectors.toList());
} else
{
throw new IllegalArgumentException("unknow sort type!");
}
return resultList;
}


/**

* @param resultList
* @param limit
*            -1表示不截取

* @return
*/
public static List<JsonObject> limit(List<JsonObject> resultList, long limit)
{
if (-1 == limit)
{
limit = Long.MAX_VALUE;
}
resultList = resultList.parallelStream().limit(limit).collect(Collectors.toList());
return resultList;
}




/**

* @param jsonArray
* @param filterMap
* @param isFuzzymatch  是否模糊匹配
* @return
*/
public static List<JsonObject> multiFilter(JsonArray jsonArray, Map<String, String> filterMap, boolean isFuzzyMatch)
{
List<JsonObject> tmpList = Lists.newArrayList();
for (int i = 0; i < jsonArray.size(); i++)
{
tmpList.add(jsonArray.get(i).getAsJsonObject());
}
Set<String> keySet = filterMap.keySet();


Predicate<JsonObject> predicate = json -> json != null;
if (isFuzzyMatch)
{
for (String key : keySet)
{
predicate = predicate
.and(json -> !Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(filterMap.get(key))
&& GsonUtil.getAsString(json, key).contains(filterMap.get(key).trim()));
}
} else
{
for (String key : keySet)
{
predicate = predicate
.and(json -> !Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(filterMap.get(key))
&& Objects.equal(GsonUtil.getAsString(json, key), filterMap.get(key).trim()));
}
}
tmpList = tmpList.parallelStream().filter(predicate).collect(Collectors.toList());
return tmpList;
}


/**

* @param collectionToFilter
* @param filterMap
* @param isFuzzyMatch
* @return
*/
public static List<JsonObject> multiFilterCollection(Collection<JsonObject> collectionToFilter,
Map<String, String> filterMap,boolean isFuzzyMatch)
{
List<JsonObject> tmpList = Lists.newArrayList();
Set<String> keySet = filterMap.keySet();


Predicate<JsonObject> predicate = json -> json != null;
if(isFuzzyMatch){

for (String key : keySet)
{
predicate = predicate.and(json -> !Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(filterMap.get(key))
&& GsonUtil.getAsString(json, key).contains(filterMap.get(key).trim()));
}
}else{
for (String key : keySet)
{
predicate = predicate
.and(json -> !Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(filterMap.get(key))
&& Objects.equal(GsonUtil.getAsString(json, key), filterMap.get(key).trim()));
}
}
tmpList = collectionToFilter.parallelStream().filter(predicate).collect(Collectors.toList());
return tmpList;
}


/*

*/
public static List<JsonObject> multiFilter(List<JsonObject> tmpList, Map<String, String> filterMap,boolean isFuzzyMatch)
{
Set<String> keySet = filterMap.keySet();


// keySet.removeIf(key -> Strings.isNullOrEmpty(key) ||
// Strings.isNullOrEmpty(filterMap.get(key)));
//
// Predicate<JsonObject> predicate = new Predicate<JsonObject>() {
//
// @Override
// public boolean test(JsonObject t)
// {
// boolean flag = true;
// for (String key : keySet)
// {
// flag = flag && GsonUtil.getAsString(t,
// key).contains(filterMap.get(key).trim());
// }
// return flag;
// }
// };
// 这种方式比上一种方式效率提高一倍。
Predicate<JsonObject> predicate = json -> json != null;
if(isFuzzyMatch){

for (String key : keySet)
{
predicate = predicate.and(json -> !Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(filterMap.get(key))
&& GsonUtil.getAsString(json, key).contains(filterMap.get(key).trim()));
}
}else{
for (String key : keySet)
{
predicate = predicate
.and(json -> !Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(filterMap.get(key))
&& Objects.equal(GsonUtil.getAsString(json, key), filterMap.get(key).trim()));
}
}
tmpList = tmpList.parallelStream().filter(predicate).collect(Collectors.toList());
return tmpList;
}


/**
* 为一组数据统一增加一组键值对。

* @param tmpList
* @param newKvMap
* @return
*/
public static List<JsonObject> multiAddPropertity(List<JsonObject> tmpList, Map<String, Object> newKvMap)
{
for (String key : newKvMap.keySet())
{
String value = (String) newKvMap.get(key);
if (!Strings.isNullOrEmpty(value))
{
tmpList = tmpList.parallelStream().map(e -> GsonUtil.set(e, key, value)).collect(Collectors.toList());
}
}
return tmpList;
}


/**
* 为一组数据统一增加一个键值对。

* @param tmpList
* @param newKvMap
* @return
*/
public static List<JsonObject> addPropertity(List<JsonObject> tmpList, String key, String value)
{
if (!Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(value))
{
tmpList = tmpList.parallelStream().map(e -> GsonUtil.set(e, key, value)).collect(Collectors.toList());
}
return tmpList;
}


/**
* 为一组数据统一增加一个键值对。

* @param tmpList
* @param newKvMap
* @return
*/
public static Set<JsonObject> addPropertity(Set<JsonObject> tmpLSet, String key, String value)
{
if (!Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(value))
{
tmpLSet = tmpLSet.parallelStream().map(e -> GsonUtil.set(e, key, value)).collect(Collectors.toSet());
}
return tmpLSet;
}


/**
* 为一组数据统一增加一个键值对。

* @param tmpList
* @param newKvMap
* @return
*/
public static List<JsonObject> addPropertity(JsonArray jsonArray, String key, String value)
{
List<JsonObject> tmpList = Lists.newArrayList();
int len = jsonArray.size();
for (int i = 0; i < len; i++)
{
tmpList.add(jsonArray.get(i).getAsJsonObject());
}
if (!Strings.isNullOrEmpty(key) && !Strings.isNullOrEmpty(value))
{
tmpList = tmpList.parallelStream().map(e -> GsonUtil.set(e, key, value)).collect(Collectors.toList());
}
return tmpList;
}


/**
* transfrom Jsonarray to Set<JsonObject> (filter completely same)

* @param jsonArray
* @return
*/
public static Set<JsonObject> toSet(JsonArray jsonArray)
{
Set<JsonObject> tmpSet = Sets.newHashSet();
int len = jsonArray.size();
for (int i = 0; i < len; i++)
{
tmpSet.add(jsonArray.get(i).getAsJsonObject());
}
return tmpSet;
}


/**

* @param jsonCollection
* @return
*/
public static JsonArray toJsonArray(Collection<JsonObject> jsonCollection)
{
JsonArray resultJsonArr = new JsonArray();
jsonCollection.stream().forEach(e -> resultJsonArr.add(e));
return resultJsonArr;
}


public static void main(String[] args)
{
List<JsonObject> jsonObjectList = Lists.newArrayList();


for (int i = 0; i < 2; i++)
{
JsonObject jsonObj = new JsonObject();
jsonObj = GsonUtil.set(jsonObj, "name", i + "");
jsonObj = GsonUtil.set(jsonObj, "age", i + "");
jsonObj = GsonUtil.set(jsonObj, "sex", i + "");
jsonObj = GsonUtil.set(jsonObj, "home", i + "");
jsonObjectList.add(jsonObj);
}


// addPropertity(jsonObjectList, "name",
// "aa").stream().forEach(System.out::println);
// addPropertity(jsonObjectList, "zzz",
// "8").stream().forEach(System.out::println);
// System.out.println(jsonObjectList);


JsonObject jsonObj = new JsonObject();
jsonObj = GsonUtil.set(jsonObj, "name", "a");
jsonObj = GsonUtil.set(jsonObj, "age", "23");
jsonObj = GsonUtil.set(jsonObj, "sex", "male");


JsonObject jsonObj2 = new JsonObject();
jsonObj2 = GsonUtil.set(jsonObj2, "name", "b");
jsonObj2 = GsonUtil.set(jsonObj2, "age", "24");
jsonObj2 = GsonUtil.set(jsonObj2, "sex", "female");


JsonObject jsonObj3 = new JsonObject();
jsonObj3 = GsonUtil.set(jsonObj3, "name", "c");
jsonObj3 = GsonUtil.set(jsonObj3, "age", "25");
jsonObj3 = GsonUtil.set(jsonObj3, "sex", "female");


JsonArray ja = new JsonArray();
// ja.add(jsonObj2);
// ja.add(jsonObj3);
ja.add(jsonObj2);
ja.add(jsonObj3);


JsonArray ja2 = new JsonArray();
// JsonElement je = new j
// jsonObj.add("children", new JsonPrimitive("lala"));
// jsonObj.add("children2", new JsonPrimitive(23));
// jsonObj.add("children3", ja);
System.out.println(ja);


System.out.println(jsonObjectList);
jsonObjectList.stream().forEach(e -> e.add("test", new JsonPrimitive(999)));
System.out.println(jsonObjectList);


}


}