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

org.apache.ode.utils.CollectionUtils Maven / Gradle / Ivy

There is a newer version: 1.3.8
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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.apache.ode.utils;

import java.util.Collection;
import java.util.Iterator;
import java.util.Map;

/**
 * Utility class for dealing with arrays.
 */
public class CollectionUtils {

    public static final Object[] EMPTY_OBJECT_ARRAY = new Object[]{};
    public static final Class[] EMPTY_CLASS_ARRAY = new Class[]{};

    /**
     * Make a {@link Collection} out of an array.
     *
     * @param type     the type of {@link Collection} to make.
     * @param elements objects to put into the collection.
     * @return a {@link Collection} of the type given in the type argument containing elements
     */
    @SuppressWarnings("unchecked")
    public static  Collection makeCollection(Class type, T[] elements) {
        if (elements == null) {
            return null;
        }
        try {
            Collection c = type.newInstance();

            for (int i = 0; i < elements.length; ++i) {
                c.add(elements[i]);
            }

            return c;
        } catch (Exception ex) {
            throw new IllegalArgumentException("Invalid arguments.", ex);
        }
    }

    /**
     * Compares the two specified maps for equality.  Returns
     * true if the two maps represent the same mappings.  More formally, two maps m1 and
     * m2 represent the same mappings if
     * m1.keySet().equals(m2.keySet()) and for every key k
     * in m1.keySet(),  (m1.get(k)==null ? m2.get(k)==null :
     * m1.get(k).equals(m2.get(k))) .
     * 

* This implementation first checks if the m1 and m2 are the same object; * if so it returns true. Then, it checks if the two maps have the same sizw; if * not, it returns false. If so, it iterates over m1's * entrySet collection, and checks that map m1 * contains each mapping that map m2 contains. If map m1 * fails to contain such a mapping, false is returned. If the * iteration completes, true is returned. * * @return true if the specified object is equal to this map. */ public static boolean equals(Map m1, Map m2) { if (m2 == m1) return true; if (m1 == null) return false; if (m2 == null) return false; if (m2.size() != m1.size()) return false; try { for (Iterator it = m1.entrySet().iterator(); it.hasNext();) { Map.Entry e = (Map.Entry) it.next(); Object key = e.getKey(); Object value = e.getValue(); if (value == null) { if (!(m2.get(key) == null && m2.containsKey(key))) return false; } else { if (!value.equals(m2.get(key))) return false; } } } catch (ClassCastException unused) { return false; } catch (NullPointerException unused) { return false; } return true; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy