Returns true if the given set is a case-insensitive set. - Android java.util

Android examples for java.util:Set

Description

Returns true if the given set is a case-insensitive set.

Demo Code

/*/*from  ww  w  . j  a  va  2 s.  co m*/
 * --------------------
 * 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://opensource.org/licenses/cddl1.php
 * 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 http://opensource.org/licenses/cddl1.php.
 * 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]"
 * --------------------
 */
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 class Main{
    /**
     * Returns true if the given set is a case-insensitive set.
     *
     * @param set
     *            The set. May be null.
     * @return true if 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;
    }
    /**
     * Equality function that properly handles arrays, lists, maps, lists of
     * arrays, and maps of arrays.
     * <p>
     * NOTE: For Sets, this relies on the equals method of the Set to do the
     * right thing. This is a reasonable assumption since, in order for Sets to
     * behave properly as Sets, their values must already have a proper
     * implementation of equals. (Or they must be specialized Sets that define a
     * custom comparator that knows how to do the right thing). The same holds
     * true for Map keys. Map values, on the other hand, are compared (so Map
     * values can be arrays).
     *
     * @param o1
     *            The first object. May be null.
     * @param o2
     *            The second object. May be null.
     * @return true if the two objects are equal.
     */
    public static boolean equals(Object o1, Object o2) {
        if (o1 == o2) { // same object or both null
            return true;
        } else if (o1 == null) {
            return false;
        } else if (o2 == null) {
            return false;
        } else if (o1.getClass().isArray()) {
            Class<?> clazz1 = o1.getClass();
            Class<?> clazz2 = o2.getClass();
            if (!clazz1.equals(clazz2)) {
                return false;
            }
            int length1 = Array.getLength(o1);
            int length2 = Array.getLength(o2);
            if (length1 != length2) {
                return false;
            }
            for (int i = 0; i < length1; i++) {
                Object el1 = Array.get(o1, i);
                Object el2 = Array.get(o2, i);
                if (!CollectionUtil.equals(el1, el2)) {
                    return false;
                }
            }
            return true;
        } else if (o1 instanceof List) {
            if (o2 instanceof List) {
                List<?> l1 = (List<?>) o1;
                List<?> l2 = (List<?>) o2;
                if (l1.size() != l2.size()) {
                    return false;
                }
                for (int i = 0; i < l1.size(); i++) {
                    Object el1 = l1.get(i);
                    Object el2 = l2.get(i);
                    if (!CollectionUtil.equals(el1, el2)) {
                        return false;
                    }
                }
                return true;
            } else {
                return false;
            }
        } else if (o1 instanceof Set) {
            if (o2 instanceof Set) {
                // rely on Set equality. this does not
                // handle the case of arrays within sets,
                // but arrays should not be placed within sets
                // unless the set is a specialized set that
                // knows how to compare arrays
                return o1.equals(o2);
            } else {
                return false;
            }
        } else if (o1 instanceof Map) {
            if (o2 instanceof Map) {
                Map<?, ?> m1 = (Map<?, ?>) o1;
                Map<?, ?> m2 = (Map<?, ?>) o2;
                if (m1.size() != m2.size()) {
                    return false;
                }
                for (Map.Entry<?, ?> entry1 : m1.entrySet()) {
                    Object key1 = entry1.getKey();
                    Object val1 = entry1.getValue();
                    if (!m2.containsKey(key1)) {
                        return false;
                    }
                    Object val2 = m2.get(key1);
                    if (!CollectionUtil.equals(val1, val2)) {
                        return false;
                    }
                }
                return true;
            } else {
                return false;
            }
        } else {
            return o1.equals(o2);
        }
    }
}

Related Tutorials