All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.usergrid.utils.MapUtils Maven / Gradle / Ivy

There is a newer version: 0.0.27.1
Show newest version
/*******************************************************************************
 * 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 map = (Map) obj;
		if (map.size() == 1) {
			Iterator> i = map.entrySet().iterator();
			return i.next().getValue();
		}
		return map;
	}

	public static Object trimAllParentSingleKeyMaps(Object obj) {
		Object prev_obj = obj;
		Object new_obj = trimSingleKeyMap(prev_obj);
		while ((new_obj != prev_obj) && (new_obj != null)) {
			prev_obj = new_obj;
			new_obj = trimSingleKeyMap(prev_obj);
		}
		return new_obj;
	}

	public static  Entry getFirst(Map map) {
		if (map == null) {
			return null;
		}
		return map.entrySet().iterator().next();
	}

	public static  K getFirstKey(Map map) {
		if (map == null) {
			return null;
		}
		Entry e = map.entrySet().iterator().next();
		if (e != null) {
			return e.getKey();
		}
		return null;
	}

	public static  Map filter(Map map, String prefix,
			boolean remove_prefix) {
		Map filteredMap = new LinkedHashMap();
		for (Entry entry : map.entrySet()) {
			if (entry.getKey().startsWith(prefix)) {
				if (remove_prefix) {
					filteredMap.put(entry.getKey().substring(prefix.length()),
							entry.getValue());
				} else {
					filteredMap.put(entry.getKey(), entry.getValue());
				}
			}
		}
		return filteredMap;
	}

	public static  Map filter(Map map, String prefix) {
		return filter(map, prefix, false);
	}

	public static Properties filter(Properties properties, String prefix,
			boolean remove_prefix) {
		Properties filteredProperties = new Properties();
		for (Entry entry : asMap(properties).entrySet()) {
			if (entry.getKey().startsWith(prefix)) {
				if (remove_prefix) {
					filteredProperties.put(
							entry.getKey().substring(prefix.length()),
							entry.getValue());
				} else {
					filteredProperties.put(entry.getKey(), entry.getValue());
				}
			}
		}
		return filteredProperties;
	}

	public static Properties filter(Properties properties, String prefix) {
		return filter(properties, prefix, false);
	}

	public static Properties asProperties(Map map) {
		Properties properties = new Properties();
		properties.putAll(map);
		return properties;
	}

	@SuppressWarnings("unchecked")
	public static Map asMap(Properties properties) {
		return (Map) cast(properties);
	}

	public static  HashMapBuilder hashMap(S key, T value) {
		return new HashMapBuilder().map(key, value);
	}

	public static class HashMapBuilder extends HashMap {
		private static final long serialVersionUID = 1L;

		public HashMapBuilder() {
		}

		public HashMapBuilder map(S key, T value) {
			put(key, value);
			return this;
		}
	}

	public static  ConcurrentHashMapBuilder concurrentHashMap(
			S key, T value) {
		return new ConcurrentHashMapBuilder().map(key, value);
	}

	public static class ConcurrentHashMapBuilder extends HashMap {
		private static final long serialVersionUID = 1L;

		public ConcurrentHashMapBuilder() {
		}

		public ConcurrentHashMapBuilder map(S key, T value) {
			put(key, value);
			return this;
		}
	}

	public static  LinkedHashMapBuilder linkedHashMap(S key, T value) {
		return new LinkedHashMapBuilder().map(key, value);
	}

	public static class LinkedHashMapBuilder extends HashMap {
		private static final long serialVersionUID = 1L;

		public LinkedHashMapBuilder() {
		}

		public LinkedHashMapBuilder map(S key, T value) {
			put(key, value);
			return this;
		}
	}

	@SuppressWarnings("unchecked")
	public static Map> toMapList(Map m) {
		Map> mapList = new LinkedHashMap>();

		for (Entry e : m.entrySet()) {
			if (e.getValue() instanceof List) {
				addListToMapList(mapList, e.getKey(),
						(List) e.getValue());
			} else {
				addMapList(mapList, e.getKey(), e.getValue());
			}
		}

		return cast(mapList);
	}

	public static Map putPath(String path, Object value) {
		return putPath(null, path, value);
	}

	@SuppressWarnings("unchecked")
	public static Map putPath(Map map, String path,
			Object value) {

		if (map == null) {
			map = new HashMap();
		}

		int i = path.indexOf('.');
		if (i < 0) {
			((Map) map).put(path, value);
			return map;
		}
		String segment = path.substring(0, i).trim();
		if (isNotBlank(segment)) {
			Object o = map.get(segment);
			if ((o != null) && (!(o instanceof Map))) {
				return map;
			}
			Map subMap = (Map) o;
			if (subMap == null) {
				subMap = new HashMap();
				((Map) map).put(segment, subMap);
			}
			String subPath = path.substring(i + 1);
			if (isNotBlank(subPath)) {
				putPath(subMap, subPath, value);
			}
		}

		return map;
	}

	public static  Map emptyMapWithKeys(Map map) {
		Map newMap = new HashMap();

		for (K k : map.keySet()) {
			newMap.put(k, null);
		}

		return newMap;
	}

	public static boolean hasKeys(Map map, String... keys) {
		if (map == null) {
			return false;
		}
		for (String key : keys) {
			if (!map.containsKey(key)) {
				return false;
			}
		}
		return true;
	}

	public static boolean hasKeys(Map map, Set keys) {
		if (map == null) {
			return false;
		}
		return map.keySet().containsAll(keys);
	}

}