org.usergrid.utils.MapUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of usergrid-core Show documentation
Show all versions of usergrid-core Show documentation
Core services for Usergrid system.
/*******************************************************************************
* Copyright 2012 Apigee Corporation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package org.usergrid.utils;
import static org.apache.commons.lang.StringUtils.isNotBlank;
import static org.usergrid.utils.ClassUtils.cast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* @author edanuff
*
*/
public class MapUtils extends org.apache.commons.collections.MapUtils {
/**
* @param
* @param
* @param map
* @param a
* @param b
*/
public static void addMapSet(Map> map, A a, B b) {
addMapSet(map, false, a, b);
}
/**
* @param
* @param
* @param map
* @param ignore_case
* @param a
* @param b
*/
@SuppressWarnings("unchecked")
public static void addMapSet(Map> map,
boolean ignore_case, A a, B b) {
Set set_b = map.get(a);
if (set_b == null) {
if (ignore_case && (b instanceof String)) {
set_b = (Set) new TreeSet(
String.CASE_INSENSITIVE_ORDER);
} else {
set_b = new LinkedHashSet();
}
map.put(a, set_b);
}
set_b.add(b);
}
/**
* @param
* @param
* @param
* @param map
* @param a
* @param b
* @param c
*/
public static void addMapMapSet(Map>> map, A a,
B b, C c) {
addMapMapSet(map, false, a, b, c);
}
/**
* @param
* @param
* @param
* @param map
* @param ignore_case
* @param a
* @param b
* @param c
*/
@SuppressWarnings("unchecked")
public static void addMapMapSet(Map>> map,
boolean ignore_case, A a, B b, C c) {
Map> map_b = map.get(a);
if (map_b == null) {
if (ignore_case && (b instanceof String)) {
map_b = (Map>) new TreeMap>(
String.CASE_INSENSITIVE_ORDER);
} else {
map_b = new LinkedHashMap>();
}
map.put(a, map_b);
}
addMapSet(map_b, ignore_case, b, c);
}
/**
* @param
* @param
* @param
* @param map
* @param a
* @param b
* @return
*/
public static Set getMapMapSet(Map>> map,
A a, B b) {
Map> map_b = map.get(a);
if (map_b == null) {
return null;
}
Set list_c = map_b.get(b);
return list_c;
}
/**
* @param
* @param
* @param
* @param
* @param map
* @param a
* @param b
* @param c
* @param d
*/
public static void addMapMapMapSet(
Map>>> map, A a, B b, C c, D d) {
addMapMapMapSet(map, false, a, b, c, d);
}
/**
* @param
* @param
* @param
* @param
* @param map
* @param ignore_case
* @param a
* @param b
* @param c
* @param d
*/
@SuppressWarnings("unchecked")
public static void addMapMapMapSet(
Map>>> map, boolean ignore_case, A a, B b,
C c, D d) {
Map>> map_b = map.get(a);
if (map_b == null) {
if (ignore_case && (b instanceof String)) {
map_b = (Map>>) new TreeMap>>(
String.CASE_INSENSITIVE_ORDER);
} else {
map_b = new LinkedHashMap>>();
}
map.put(a, map_b);
}
addMapMapSet(map_b, ignore_case, b, c, d);
}
/**
* @param
* @param
* @param
* @param
* @param map
* @param a
* @param b
* @param c
* @return
*/
public static Set getMapMapMapSet(
Map>>> map, A a, B b, C c) {
Map>> map_b = map.get(a);
if (map_b == null) {
return null;
}
return getMapMapSet(map_b, b, c);
}
public static void putMapMap(Map> map, A a, B b, C c) {
putMapMap(map, false, a, b, c);
}
@SuppressWarnings("unchecked")
public static void putMapMap(Map> map,
boolean ignore_case, A a, B b, C c) {
Map map_b = map.get(a);
if (map_b == null) {
if (ignore_case && (b instanceof String)) {
map_b = (Map) new TreeMap(
String.CASE_INSENSITIVE_ORDER);
} else {
map_b = new LinkedHashMap();
}
map.put(a, map_b);
}
map_b.put(b, c);
}
public static C getMapMap(Map> map, A a, B b) {
Map map_b = map.get(a);
if (map_b == null) {
return null;
}
return map_b.get(b);
}
/**
* @param
* @param
* @param map
* @param a
* @param b
*/
public static void addMapList(Map> map, A a, B b) {
List list_b = map.get(a);
if (list_b == null) {
list_b = new ArrayList();
map.put(a, list_b);
}
list_b.add(b);
}
public static void addListToMapList(Map> map, A a,
List b) {
List list_b = map.get(a);
if (list_b == null) {
list_b = new ArrayList();
map.put(a, list_b);
}
list_b.addAll(b);
}
/**
* @param
* @param
* @param
* @param map
* @param a
* @param b
* @param c
*/
public static void addMapMapList(Map>> map,
A a, B b, C c) {
addMapMapList(map, false, a, b, c);
}
/**
* @param
* @param
* @param
* @param map
* @param ignore_case
* @param a
* @param b
* @param c
*/
@SuppressWarnings("unchecked")
public static void addMapMapList(Map>> map,
boolean ignore_case, A a, B b, C c) {
Map> map_b = map.get(a);
if (map_b == null) {
if (ignore_case && (b instanceof String)) {
map_b = (Map>) new TreeMap>(
String.CASE_INSENSITIVE_ORDER);
} else {
map_b = new LinkedHashMap>();
}
map.put(a, map_b);
}
addMapList(map_b, b, c);
}
/**
* @param
* @param
* @param
* @param map
* @param a
* @param b
* @return
*/
public static List getMapMapList(Map>> map,
A a, B b) {
Map> map_b = map.get(a);
if (map_b == null) {
return null;
}
List list_c = map_b.get(b);
return list_c;
}
/**
* @param
* @param
* @param
* @param
* @param map
* @param a
* @param b
* @param c
* @param d
*/
public static void addMapMapMapList(
Map>>> map, A a, B b, C c, D d) {
addMapMapMapList(map, false, a, b, c, d);
}
/**
* @param
* @param
* @param
* @param
* @param map
* @param ignore_case
* @param a
* @param b
* @param c
* @param d
*/
@SuppressWarnings("unchecked")
public static void addMapMapMapList(
Map>>> map, boolean ignore_case, A a, B b,
C c, D d) {
Map>> map_b = map.get(a);
if (map_b == null) {
if (ignore_case && (b instanceof String)) {
map_b = (Map>>) new TreeMap>>(
String.CASE_INSENSITIVE_ORDER);
} else {
map_b = new LinkedHashMap>>();
}
map.put(a, map_b);
}
addMapMapList(map_b, ignore_case, b, c, d);
}
/**
* @param
* @param
* @param
* @param
* @param map
* @param a
* @param b
* @param c
* @return
*/
public static List getMapMapMapList(
Map>>> map, A a, B b, C c) {
Map>> map_b = map.get(a);
if (map_b == null) {
return null;
}
return getMapMapList(map_b, b, c);
}
@SuppressWarnings("unchecked")
public static V getValue(Map map, K k) {
V v = null;
try {
v = (V) map.get(k);
} catch (ClassCastException e) {
}
return v;
}
public static Map merge(Map... maps) {
Map merged = new TreeMap(
String.CASE_INSENSITIVE_ORDER);
for (Map m : maps) {
merged.putAll(m);
}
return merged;
}
public static Map superMerge(Map a, Map b) {
for (K k : b.keySet()) {
V v = b.get(k);
if (!a.containsKey(k)) {
a.put(k, v);
return a;
} else {
V av = a.get(k);
if (av instanceof Map) {
}
}
}
return a;
}
@SuppressWarnings("unchecked")
public static Map map(Object... objects) {
Map map = new LinkedHashMap();
int i = 0;
while (i < objects.length) {
if (objects[i] instanceof Map.Entry) {
Map.Entry entry = (Entry) objects[i];
map.put(entry.getKey(), entry.getValue());
i++;
} else if (objects[i] instanceof Map) {
map.putAll((Map) objects[i]);
i++;
} else if (i < (objects.length - 1)) {
K k = (K) objects[i];
V v = (V) objects[i + 1];
map.put(k, v);
i += 2;
} else {
break;
}
}
return map;
}
private static class SimpleMapEntry implements Map.Entry {
private final K k;
private V v;
public SimpleMapEntry(K k, V v) {
this.k = k;
this.v = v;
}
@Override
public K getKey() {
return k;
}
@Override
public V getValue() {
return v;
}
@Override
public V setValue(V v) {
V oldV = this.v;
this.v = v;
return oldV;
}
}
public static Map.Entry entry(K k, V v) {
return new SimpleMapEntry(k, v);
}
public static Object trimSingleKeyMap(Object obj) {
if (obj == null) {
return null;
}
if (!(obj instanceof Map)) {
return obj;
}
@SuppressWarnings("unchecked")
Map