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

org.spongepowered.api.util.CollectionUtils Maven / Gradle / Ivy

The newest version!
/*
 * This file is part of SpongeAPI, licensed under the MIT License (MIT).
 *
 * Copyright (c) SpongePowered 
 * Copyright (c) contributors
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.spongepowered.api.util;

import java.util.ArrayList;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;

public final class CollectionUtils {

    /**
     * Attempts to use native {@link Object#clone()} methods on available map
     * types. If a map cannot be properly cloned, a new {@link HashMap} is
     * returned.
     *
     * @param map The map input
     * @param  The key type
     * @param  The value type
     * @return A copied map
     */
    @SuppressWarnings("unchecked")
    public static  Map copyMap(Map map) {
        try {
            if (map instanceof HashMap) {
                return (Map) ((HashMap) map).clone();
            } else if (map instanceof IdentityHashMap) {
                return (Map) ((IdentityHashMap) map).clone();
            } else if (map instanceof EnumMap) {
                return (Map) ((EnumMap) map).clone();
            } else if (map instanceof TreeMap) {
                return (Map) ((TreeMap) map).clone();
            } else if (map instanceof ConcurrentHashMap) {
                return (Map) new ConcurrentHashMap<>(map);
            }
        } catch (final Exception ignored) {
            // fall back to HashMap
        }
        return new HashMap<>(map);
    }

    /**
     * Attempts to use native {@link Object#clone()} methods on available map
     * types. If a list cannot be properly cloned, a new {@link ArrayList} is
     * returned.
     *
     * @param list The list input
     * @param  The value type
     * @return A copied list
     */
    @SuppressWarnings("unchecked")
    public static  List copyList(List list) {
        try {
            if (list instanceof ArrayList) {
                return (List) ((ArrayList) list).clone();
            } else if (list instanceof LinkedList) {
                return (List) ((LinkedList) list).clone();
            } else if (list instanceof CopyOnWriteArrayList) {
                return (List) ((CopyOnWriteArrayList) list).clone();
            }
        } catch (final Exception ignored) {
            // fall back to a standard copy
        }
        return new ArrayList<>(list);
    }

    // Suppress default constructor to ensure non-instantiability.
    private CollectionUtils() {
        throw new AssertionError("You should not be attempting to instantiate this class.");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy