org.identityconnectors.common.CollectionUtil Maven / Gradle / Ivy
The newest version!
/*
* ====================
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of the Common Development
* and Distribution License("CDDL") (the "License"). You may not use this file
* except in compliance with the License.
*
* You can obtain a copy of the License at
* http://IdentityConnectors.dev.java.net/legal/license.txt
* See the License for the specific language governing permissions and limitations
* under the License.
*
* When distributing the Covered Code, include this CDDL Header Notice in each file
* and include the License file at identityconnectors/legal/license.txt.
* If applicable, add the following below this CDDL Header, with the fields
* enclosed by brackets [] replaced by your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
* ====================
*/
package org.identityconnectors.common;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.TreeMap;
import java.util.TreeSet;
public final class CollectionUtil {
/**
* Never allow this to be instantiated.
*/
private CollectionUtil() {
throw new AssertionError();
}
/**
* Creates a case-insensitive set
* @return An empty case-insensitive set
*/
public static SortedSet newCaseInsensitiveSet() {
TreeSet rv = new TreeSet(String.CASE_INSENSITIVE_ORDER);
return rv;
}
/**
* Returns true if the given set is a case-insensitive set
* @param set The set. May be null.
* @return true iff the given set is a case-insensitive set
*/
public static boolean isCaseInsensitiveSet(Set> set) {
if ( set instanceof SortedSet ) {
SortedSet> sortedSet =
(SortedSet>)set;
Comparator> comp = sortedSet.comparator();
if ( comp.equals(String.CASE_INSENSITIVE_ORDER) ) {
return true;
}
}
return false;
}
/**
* Creates a case-insenstive map
* @param The object type of the map
* @return An empty case-insensitive map
*/
public static SortedMap newCaseInsensitiveMap() {
TreeMap rv = new TreeMap(String.CASE_INSENSITIVE_ORDER);
return rv;
}
/**
* Returns true if the given map is a case-insensitive map
* @param map The map. May be null.
* @return true iff the given map is a case-insensitive map
*/
public static boolean isCaseInsensitiveMap(Map,?> map) {
if ( map instanceof SortedMap ) {
SortedMap,?> sortedMap =
(SortedMap,?>)map;
Comparator> comp = sortedMap.comparator();
if ( comp.equals(String.CASE_INSENSITIVE_ORDER) ) {
return true;
}
}
return false;
}
/**
* Protects from null
and returns a new instance of
* {@link ArrayList} if the parameter c is null.
* Otherwise return the parameter that was passed in.
*/
public static Collection nullAsEmpty(Collection c) {
return c == null ? new HashSet() : c;
}
/**
* Protects from null
and returns a new instance of
* {@link HashMap} if the parameter map
is null
.
* Otherwise return the parameter that was passed in.
*/
public static Map nullAsEmpty(Map map) {
return (map == null) ? new HashMap() : map;
}
/**
* Protects from null
and returns a new instance of
* {@link HashSet} if the parameter set
is null
.
* Otherwise return the parameter that was passed in.
*/
public static Set nullAsEmpty(Set set) {
return (set == null) ? new HashSet() : set;
}
/**
* Protects from null
and returns a new instance of
* {@link ArrayList} if the parameter list
is
* null
. Otherwise return the parameter that was passed in.
*/
public static List nullAsEmpty(final List list) {
return (list == null) ? new ArrayList() : list;
}
/**
* Determine if {@link Collection} is empty or not, protects against null
* being passed in.
*/
public static boolean isEmpty(Collection c) {
return c == null || c.isEmpty();
}
/**
* Use {@link HashSet} to create a unique {@link Collection} based on the
* one passed in. The method protects against null. The
* returned {@link Collection} is unmodifiable.
*/
public static Collection unique(final Collection c) {
return Collections. unmodifiableSet(newSet(c));
}
public static Map newReadOnlyMap(Map map) {
return Collections.unmodifiableMap(new HashMap(nullAsEmpty(map)));
}
public static Map asReadOnlyMap(Map map) {
if ( map instanceof SortedMap ) {
@SuppressWarnings("unchecked")
SortedMap sortedMap =
(SortedMap)map;
return Collections.unmodifiableSortedMap(sortedMap);
}
else {
return Collections.unmodifiableMap(nullAsEmpty(map));
}
}
public static Map newReadOnlyMap(T[][] kv) {
Map map = new HashMap();
for (int i = 0; kv != null && i < kv.length; i++) {
T key = kv[i][0];
T value = kv[i][1];
map.put(key, value);
}
return Collections. unmodifiableMap(map);
}
/**
* Converts two {@link List} to a map. The order is important here because
* each key will map to one value.
*/
public static Map newMapFromLists(List keys, List values) {
// throw if there's invalid input..
if (keys.size() != values.size()) {
throw new IllegalArgumentException();
}
Map map = new HashMap(keys.size());
Iterator keyIter = keys.iterator();
Iterator valueIter = values.iterator();
while (keyIter.hasNext() && valueIter.hasNext()) {
T key = keyIter.next();
K value = valueIter.next();
map.put(key, value);
}
return map;
}
public static Map newMap(Properties properties) {
Map rv = new HashMap();
for (Map.Entry