org.jiemamy.utils.collection.CollectionsUtil.java Source code

Java tutorial

Introduction

Here is the source code for org.jiemamy.utils.collection.CollectionsUtil.java

Source

/*
 * Copyright 2007-2012 Jiemamy Project and the Others.
 *
 * This file is part of Jiemamy.
 *
 * 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.jiemamy.utils.collection;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Hashtable;
import java.util.IdentityHashMap;
import java.util.LinkedHashMap;
import java.util.LinkedHashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Set;
import java.util.SortedMap;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Vector;
import java.util.WeakHashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.concurrent.DelayQueue;
import java.util.concurrent.Delayed;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.PriorityBlockingQueue;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.collect.Sets;

import org.apache.commons.lang.Validate;

/**
 * Generics??????
 * 
 * @version $Id$
 * @author j5ik2o
 */
public final class CollectionsUtil {

    /**
     * {@link List}?????????? {@link List}?
     * {@code element}???{@link Object#equals(Object)}????????
     * ??????????
     * 
     * @param <E> ??
     * @param list ?? {@link Set}
     * @param element ??
     * @return ???????????????????{@code null}
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static <E> E addOrReplace(List<E> list, E element) {
        Validate.notNull(list);
        Validate.notNull(element);

        E removed = null;
        int index = list.indexOf(element);
        if (index != -1) {
            removed = list.set(index, element);
        } else {
            list.add(element);
        }
        return removed;
    }

    /**
     * {@link Set}?????????? {@link Set}?
     * {@code element}???{@link Object#equals(Object)}????????
     * ??????????
     * 
     * @param <E> ??
     * @param set ?? {@link Set}
     * @param element ??
     * @return ???????????????????{@code null}
     * @throws CollectionModificationException {@code set}??add/remove????
     * @throws IllegalArgumentException ?{@code null}???
     */
    public static <E> E addOrReplace(Set<E> set, E element) {
        Validate.notNull(set);
        Validate.notNull(element);

        E removed = null;
        if (set.contains(element)) {
            for (E oldElement : set) {
                if (element.equals(oldElement)) {
                    removed = oldElement;
                    try {
                        if (set.remove(oldElement) == false) {
                            throw new CollectionModificationException("cannot remove");
                        }
                    } catch (UnsupportedOperationException e) {
                        throw new CollectionModificationException("cannot remove", e);
                    }
                    break;
                }
            }
        }

        try {
            if (set.add(element) == false) {
                throw new CollectionModificationException("cannot add");
            }
        } catch (UnsupportedOperationException e) {
            throw new CollectionModificationException("cannot add", e);
        }
        return removed;
    }

    /**
     * {@link ArrayBlockingQueue}?????
     * 
     * @param <E> {@link ArrayBlockingQueue}??
     * @param capacity ??
     * @return {@link ArrayBlockingQueue}???
     * @see ArrayBlockingQueue#ArrayBlockingQueue(int)
     */
    public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity) {
        return new ArrayBlockingQueue<E>(capacity);
    }

    /**
     * {@link ArrayBlockingQueue}?????
     * 
     * @param <E> {@link ArrayBlockingQueue}??
     * @param capacity ??
     * @param fair {@code true}???????????
     * @return {@link ArrayBlockingQueue}???
     * @see ArrayBlockingQueue#ArrayBlockingQueue(int, boolean)
     */
    public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity, boolean fair) {
        return new ArrayBlockingQueue<E>(capacity, fair);
    }

    /**
     * {@link ArrayBlockingQueue}?????
     * 
     * @param <E> {@link ArrayBlockingQueue}??
     * @param capacity ??
     * @param fair {@code true}???????????
     * @param c ?????
     * @return {@link ArrayBlockingQueue}???
     * @throws IllegalArgumentException {@code c}?{@code null}???
     * @throws IllegalArgumentException if {@code capacity} is less than {@code c.size()}, or less than 1.
     * @see ArrayBlockingQueue#ArrayBlockingQueue(int, boolean, Collection)
     */
    public static <E> ArrayBlockingQueue<E> newArrayBlockingQueue(int capacity, boolean fair,
            Collection<? extends E> c) {
        Validate.notNull(c);
        return new ArrayBlockingQueue<E>(capacity, fair, c);
    }

    /**
     * {@link ArrayList}?????
     * 
     * @param <E> {@link ArrayList}??
     * @return {@link ArrayList}???
     * @see ArrayList#ArrayList()
     * @deprecated use {@link Lists#newArrayList()}
     */
    @Deprecated
    public static <E> ArrayList<E> newArrayList() {
        return new ArrayList<E>();
    }

    /**
     * {@link ArrayList}?????
     * 
     * @param <E> {@link ArrayList}??
     * @param c ?????
     * @return {@link ArrayList}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see ArrayList#ArrayList(Collection)
     * @deprecated use {@link Lists#newArrayList(Iterable)}
     */
    @Deprecated
    public static <E> ArrayList<E> newArrayList(Collection<? extends E> c) {
        Validate.notNull(c);
        return new ArrayList<E>(c);
    }

    /**
     * {@link ArrayList}?????
     * 
     * @param <E> {@link ArrayList}??
     * @param initialCapacity ???
     * @return {@link ArrayList}???
     * @exception IllegalArgumentException if the specified initial capacity is negative
     * @see ArrayList#ArrayList(int)
     * @deprecated use {@link Lists#newArrayListWithCapacity(int)}
     */
    @Deprecated
    public static <E> ArrayList<E> newArrayList(int initialCapacity) {
        return new ArrayList<E>(initialCapacity);
    }

    /**
     * {@link ConcurrentHashMap}?????
     * 
     * @param <K> {@link ConcurrentHashMap}??
     * @param <V> {@link ConcurrentHashMap}??
     * @return {@link ConcurrentHashMap}???
     * @see ConcurrentHashMap#ConcurrentHashMap()
     */
    public static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap() {
        return new ConcurrentHashMap<K, V>();
    }

    /**
     * {@link ConcurrentHashMap}?????
     * 
     * @param <K> {@link ConcurrentHashMap}??
     * @param <V> {@link ConcurrentHashMap}??
     * @param initialCapacity ??
     * @return {@link ConcurrentHashMap}???
     * @throws IllegalArgumentException if the initial capacity of elements is negative.
     * @see ConcurrentHashMap#ConcurrentHashMap(int)
     */
    public static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap(int initialCapacity) {
        return new ConcurrentHashMap<K, V>(initialCapacity);
    }

    /**
     * {@link ConcurrentHashMap}?????
     * 
     * @param <K> {@link ConcurrentHashMap}??
     * @param <V> {@link ConcurrentHashMap}??
     * @param initialCapacity ??
     * @param loadFactor ?????????
     * @param concurrencyLevel ???
     * @return {@link ConcurrentHashMap}???
     * @throws IllegalArgumentException if the initial capacity is negative or the load factor or concurrencyLevel
     *             are nonpositive.
     * @see ConcurrentHashMap#ConcurrentHashMap(int, float, int)
     */
    public static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap(int initialCapacity, float loadFactor,
            int concurrencyLevel) {
        return new ConcurrentHashMap<K, V>(initialCapacity, loadFactor, concurrencyLevel);
    }

    /**
     * {@link ConcurrentHashMap}?????
     * 
     * @param <K> {@link ConcurrentHashMap}??
     * @param <V> {@link ConcurrentHashMap}??
     * @param m ?????
     * @return {@link ConcurrentHashMap}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see ConcurrentHashMap#ConcurrentHashMap(Map)
     */
    public static <K, V> ConcurrentHashMap<K, V> newConcurrentHashMap(Map<? extends K, ? extends V> m) {
        Validate.notNull(m);
        return new ConcurrentHashMap<K, V>(m);
    }

    /**
     * {@link ConcurrentLinkedQueue}?????
     * 
     * @param <E> {@link ConcurrentLinkedQueue}??
     * @return {@link ConcurrentLinkedQueue}???
     * @see ConcurrentLinkedQueue#ConcurrentLinkedQueue()
     */
    public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue() {
        return new ConcurrentLinkedQueue<E>();
    }

    /**
     * {@link ConcurrentLinkedQueue}?????
     * 
     * @param <E> {@link ConcurrentLinkedQueue}??
     * @param c ?????
     * @return {@link ConcurrentLinkedQueue}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see ConcurrentLinkedQueue#ConcurrentLinkedQueue(Collection)
     */
    public static <E> ConcurrentLinkedQueue<E> newConcurrentLinkedQueue(Collection<? extends E> c) {
        Validate.notNull(c);
        return new ConcurrentLinkedQueue<E>(c);
    }

    /**
     * {@link CopyOnWriteArrayList}?????
     * 
     * @param <E> {@link CopyOnWriteArrayList}??
     * @return {@link CopyOnWriteArrayList}???
     * @see CopyOnWriteArrayList#CopyOnWriteArrayList()
     */
    public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList() {
        return new CopyOnWriteArrayList<E>();
    }

    /**
     * {@link CopyOnWriteArrayList}?????
     * 
     * @param <E> {@link CopyOnWriteArrayList}??
     * @param c ??????????
     * @return {@link CopyOnWriteArrayList}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see CopyOnWriteArrayList#CopyOnWriteArrayList(Collection)
     */
    public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(Collection<? extends E> c) {
        Validate.notNull(c);
        return new CopyOnWriteArrayList<E>(c);
    }

    /**
     * {@link CopyOnWriteArrayList}?????
     * 
     * @param <E> {@link CopyOnWriteArrayList}??
     * @param toCopyIn ? (??????????)
     * @return {@link CopyOnWriteArrayList}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see CopyOnWriteArrayList#CopyOnWriteArrayList(Object[])
     */
    public static <E> CopyOnWriteArrayList<E> newCopyOnWriteArrayList(E[] toCopyIn) {
        Validate.notNull(toCopyIn);
        return new CopyOnWriteArrayList<E>(toCopyIn);
    }

    /**
     * {@link CopyOnWriteArraySet}?????
     * 
     * @param <E> {@link CopyOnWriteArraySet}??
     * @return {@link CopyOnWriteArraySet}???
     * @see CopyOnWriteArraySet#CopyOnWriteArraySet()
     */
    public static <E> CopyOnWriteArraySet<E> newCopyOnWriteArraySet() {
        return new CopyOnWriteArraySet<E>();
    }

    /**
     * {@link CopyOnWriteArraySet}?????
     * 
     * @param <E> {@link CopyOnWriteArraySet}??
     * @param c 
     * @return {@link CopyOnWriteArraySet}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see CopyOnWriteArraySet#CopyOnWriteArraySet(Collection)
     */
    public static <E> CopyOnWriteArraySet<E> newCopyOnWriteArraySet(Collection<? extends E> c) {
        Validate.notNull(c);
        return new CopyOnWriteArraySet<E>(c);
    }

    /**
     * {@link DelayQueue}?????
     * 
     * @param <E> {@link CopyOnWriteArraySet}??
     * @return {@link DelayQueue}???
     * @see DelayQueue#DelayQueue()
     */
    public static <E extends Delayed> DelayQueue<E> newDelayQueue() {
        return new DelayQueue<E>();
    }

    /**
     * {@link DelayQueue}?????
     * 
     * @param <E> {@link CopyOnWriteArraySet}??
     * @param c 
     * @return {@link DelayQueue}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see DelayQueue#DelayQueue(Collection)
     */
    public static <E extends Delayed> DelayQueue<E> newDelayQueue(Collection<? extends E> c) {
        Validate.notNull(c);
        return new DelayQueue<E>(c);
    }

    /**
     * {@link HashMap}?????
     * 
     * @param <K> {@link HashMap}??
     * @param <V> {@link HashMap}??
     * @return {@link HashMap}???
     * @see HashMap#HashMap()
     * @deprecated use {@link Maps#newHashMap()}
     */
    @Deprecated
    public static <K, V> HashMap<K, V> newHashMap() {
        return new HashMap<K, V>();
    }

    /**
     * {@link HashMap}?????
     * 
     * @param <K> {@link HashMap}??
     * @param <V> {@link HashMap}??
     * @param initialCapacity ??
     * @return {@link HashMap}???
     * @throws IllegalArgumentException if the initial capacity is negative.
     * @see HashMap#HashMap(int)
     * @deprecated use {@link Maps#newHashMapWithExpectedSize(int)}
     */
    @Deprecated
    public static <K, V> HashMap<K, V> newHashMap(int initialCapacity) {
        return new HashMap<K, V>(initialCapacity);
    }

    /**
     * {@link HashMap}?????
     * 
     * @param <K> {@link HashMap}??
     * @param <V> {@link HashMap}??
     * @param initialCapacity ??
     * @param loadFactor ?????????
     * @return {@link HashMap}???
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive.
     * @see HashMap#HashMap(int, float)
     * @deprecated use {@link Maps#newHashMapWithExpectedSize(int)}
     */
    @Deprecated
    public static <K, V> HashMap<K, V> newHashMap(int initialCapacity, float loadFactor) {
        return new HashMap<K, V>(initialCapacity, loadFactor);
    }

    /**
     * {@link HashMap}?????
     * 
     * @param <K> {@link HashMap}??
     * @param <V> {@link HashMap}??
     * @param m ?????
     * @return {@link HashMap}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see HashMap#HashMap(int, float)
     * @deprecated use {@link Maps#newHashMap(Map)}
     */
    @Deprecated
    public static <K, V> HashMap<K, V> newHashMap(Map<? extends K, ? extends V> m) {
        Validate.notNull(m);
        return new HashMap<K, V>(m);
    }

    /**
     * {@link HashSet}?????
     * 
     * @param <E> {@link HashSet}??
     * @return {@link HashSet}???
     * @see HashSet#HashSet()
     * @deprecated use {@link Sets#newHashSet()}
     */
    @Deprecated
    public static <E> HashSet<E> newHashSet() {
        return new HashSet<E>();
    }

    /**
     * {@link HashSet}?????
     * 
     * @param <E> {@link HashSet}??
     * @param c ?????
     * @return {@link HashSet}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see HashSet#HashSet()
     * @deprecated use {@link Sets#newHashSet(Iterable)}
     */
    @Deprecated
    public static <E> HashSet<E> newHashSet(Collection<? extends E> c) {
        Validate.notNull(c);
        return new HashSet<E>(c);
    }

    /**
     * {@link HashSet}?????
     * 
     * @param <E> {@link HashSet}??
     * @param initialCapacity ??
     * @return {@link HashSet}???
     * @throws     IllegalArgumentException if the initial capacity is less
     *             than zero.
     * @see HashSet#HashSet()
     * @deprecated use {@link Sets#newHashSetWithExpectedSize(int)}
     */
    @Deprecated
    public static <E> HashSet<E> newHashSet(int initialCapacity) {
        return new HashSet<E>(initialCapacity);
    }

    /**
     * {@link HashSet}?????
     * 
     * @param <E> {@link HashSet}??
     * @param initialCapacity ??
     * @param loadFactor ?
     * @return {@link HashSet}???
     * @throws     IllegalArgumentException if the initial capacity is less
     *             than zero, or if the load factor is nonpositive.
     * @see HashSet#HashSet()
     * @deprecated use {@link Sets#newHashSetWithExpectedSize(int)}
     */
    @Deprecated
    public static <E> HashSet<E> newHashSet(int initialCapacity, float loadFactor) {
        return new HashSet<E>(initialCapacity, loadFactor);
    }

    /**
     * {@link Hashtable}?????
     * 
     * @param <K> {@link Hashtable}??
     * @param <V> {@link Hashtable}??
     * @return {@link Hashtable}???
     * @see Hashtable#Hashtable()
     */
    public static <K, V> Hashtable<K, V> newHashtable() {
        return new Hashtable<K, V>();
    }

    /**
     * {@link Hashtable}?????
     * 
     * @param <K> {@link Hashtable}??
     * @param <V> {@link Hashtable}??
     * @param initialCapacity ????
     * @return {@link Hashtable}???
     * @exception IllegalArgumentException if the initial capacity is less
     *              than zero.
     * @see Hashtable#Hashtable(int)
     */
    public static <K, V> Hashtable<K, V> newHashtable(int initialCapacity) {
        return new Hashtable<K, V>(initialCapacity);
    }

    /**
     * {@link Hashtable}?????
     * 
     * @param <K> {@link Hashtable}??
     * @param <V> {@link Hashtable}??
     * @param initialCapacity ????
     * @param loadFactor ???
     * @return {@link Hashtable}???
     * @throws IllegalArgumentException  if the initial capacity is less
     *             than zero, or if the load factor is nonpositive.
     * @see Hashtable#Hashtable(int, float)
     */
    public static <K, V> Hashtable<K, V> newHashtable(int initialCapacity, float loadFactor) {
        return new Hashtable<K, V>(initialCapacity, loadFactor);
    }

    /**
     * {@link Hashtable}?????
     * 
     * @param <K> {@link Hashtable}??
     * @param <V> {@link Hashtable}??
     * @param m ?????
     * @return {@link Hashtable}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see Hashtable#Hashtable(Map)
     */
    public static <K, V> Hashtable<K, V> newHashtable(Map<? extends K, ? extends V> m) {
        Validate.notNull(m);
        return new Hashtable<K, V>(m);
    }

    /**
     * {@link IdentityHashMap}?????
     * 
     * @param <K> {@link IdentityHashMap}??
     * @param <V> {@link IdentityHashMap}??
     * @return {@link IdentityHashMap}???
     * @see IdentityHashMap#IdentityHashMap()
     * @deprecated use {@link Maps#newIdentityHashMap()}
     */
    @Deprecated
    public static <K, V> IdentityHashMap<K, V> newIdentityHashMap() {
        return new IdentityHashMap<K, V>();
    }

    /**
     * {@link IdentityHashMap}?????
     * 
     * @param <K> {@link IdentityHashMap}??
     * @param <V> {@link IdentityHashMap}??
     * @param expectedMaxSize ?
     * @return {@link IdentityHashMap}???
     * @throws IllegalArgumentException if <tt>expectedMaxSize</tt> is negative
     * @see IdentityHashMap#IdentityHashMap(int)
     */
    public static <K, V> IdentityHashMap<K, V> newIdentityHashMap(int expectedMaxSize) {
        return new IdentityHashMap<K, V>(expectedMaxSize);
    }

    /**
     * {@link IdentityHashMap}?????
     * 
     * @param <K> {@link IdentityHashMap}??
     * @param <V> {@link IdentityHashMap}??
     * @param m ?????
     * @return {@link IdentityHashMap}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see IdentityHashMap#IdentityHashMap(Map)
     */
    public static <K, V> IdentityHashMap<K, V> newIdentityHashMap(Map<? extends K, ? extends V> m) {
        Validate.notNull(m);
        return new IdentityHashMap<K, V>(m);
    }

    /**
     * {@link LinkedBlockingQueue}?????
     * 
     * @param <E> {@link LinkedBlockingQueue}??
     * @return {@link LinkedBlockingQueue}???
     * @see LinkedBlockingQueue#LinkedBlockingQueue()
     */
    public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue() {
        return new LinkedBlockingQueue<E>();
    }

    /**
     * {@link LinkedBlockingQueue}?????
     * 
     * @param <E> {@link LinkedBlockingQueue}??
     * @param c ?????
     * @return {@link LinkedBlockingQueue}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see LinkedBlockingQueue#LinkedBlockingQueue(Collection)
     */
    public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(Collection<? extends E> c) {
        Validate.notNull(c);
        return new LinkedBlockingQueue<E>(c);
    }

    /**
     * {@link LinkedBlockingQueue}?????
     * 
     * @param <E> {@link LinkedBlockingQueue}??
     * @param initialCapacity ????
     * @return {@link LinkedBlockingQueue}???
     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is not greater
     *         than zero.
     * @see LinkedBlockingQueue#LinkedBlockingQueue(int)
     */
    public static <E> LinkedBlockingQueue<E> newLinkedBlockingQueue(int initialCapacity) {
        return new LinkedBlockingQueue<E>(initialCapacity);
    }

    /**
     * {@link LinkedHashMap}?????
     * 
     * @param <K> {@link LinkedHashMap}??
     * @param <V> {@link LinkedHashMap}??
     * @return {@link LinkedHashMap}???
     * @see LinkedHashMap#LinkedHashMap()
     * @deprecated use {@link Maps#newLinkedHashMap()}
     */
    @Deprecated
    public static <K, V> LinkedHashMap<K, V> newLinkedHashMap() {
        return new LinkedHashMap<K, V>();
    }

    /**
     * {@link LinkedHashMap}?????
     * 
     * @param <K> {@link LinkedHashMap}??
     * @param <V> {@link LinkedHashMap}??
     * @param initialCapacity ??
     * @return {@link LinkedHashMap}???
     * @throws IllegalArgumentException if the initial capacity is negative.
     * @see LinkedHashMap#LinkedHashMap(int)
     */
    public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int initialCapacity) {
        return new LinkedHashMap<K, V>(initialCapacity);
    }

    /**
     * {@link LinkedHashMap}?????
     * 
     * @param <K> {@link LinkedHashMap}??
     * @param <V> {@link LinkedHashMap}??
     * @param initialCapacity ??
     * @param loadFactor ?
     * @return {@link LinkedHashMap}???
     * @throws IllegalArgumentException if the initial capacity is negative
     *         or the load factor is nonpositive.
     * @see LinkedHashMap#LinkedHashMap(int, float)
     */
    public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(int initialCapacity, float loadFactor) {
        return new LinkedHashMap<K, V>(initialCapacity, loadFactor);
    }

    /**
     * {@link LinkedHashMap}?????
     * 
     * @param <K> {@link LinkedHashMap}??
     * @param <V> {@link LinkedHashMap}??
     * @param m ?????
     * @return {@link LinkedHashMap}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see LinkedHashMap#LinkedHashMap(Map)
     */
    public static <K, V> LinkedHashMap<K, V> newLinkedHashMap(Map<? extends K, ? extends V> m) {
        Validate.notNull(m);
        return new LinkedHashMap<K, V>(m);
    }

    /**
     * {@link LinkedHashSet}?????
     * 
     * @param <E> {@link LinkedHashSet}??
     * @return {@link LinkedHashSet}???
     * @see LinkedHashSet#LinkedHashSet()
     * @deprecated use {@link Sets#newLinkedHashSet()}
     */
    @Deprecated
    public static <E> LinkedHashSet<E> newLinkedHashSet() {
        return new LinkedHashSet<E>();
    }

    /**
     * {@link LinkedHashSet}?????
     * 
     * @param <E> {@link LinkedHashSet}??
     * @param c ?????
     * @return {@link LinkedHashSet}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see LinkedHashSet#LinkedHashSet(Collection)
     * @deprecated use {@link Sets#newLinkedHashSet(Iterable)}
     */
    @Deprecated
    public static <E> LinkedHashSet<E> newLinkedHashSet(Collection<? extends E> c) {
        Validate.notNull(c);
        return new LinkedHashSet<E>(c);
    }

    /**
     * {@link LinkedHashSet}?????
     * 
     * @param <E> {@link LinkedHashSet}??
     * @param initialCapacity ??
     * @return {@link LinkedHashSet}???
     * @throws  IllegalArgumentException if the initial capacity is less
     *              than zero.
     * @see LinkedHashSet#LinkedHashSet(int)
     */
    public static <E> LinkedHashSet<E> newLinkedHashSet(int initialCapacity) {
        return new LinkedHashSet<E>(initialCapacity);
    }

    /**
     * {@link LinkedHashSet}?????
     * 
     * @param <E> {@link LinkedHashSet}??
     * @param initialCapacity ??
     * @param loadFactor ?
     * @return {@link LinkedHashSet}???
     * @throws     IllegalArgumentException  if the initial capacity is less
     *               than zero, or if the load factor is nonpositive.
     * @see LinkedHashSet#LinkedHashSet(int, float)
     */
    public static <E> LinkedHashSet<E> newLinkedHashSet(int initialCapacity, float loadFactor) {
        return new LinkedHashSet<E>(initialCapacity, loadFactor);
    }

    /**
     * {@link LinkedList}?????
     * 
     * @param <E> {@link LinkedList}??
     * @return {@link LinkedList}???
     * @see LinkedList#LinkedList()
     * @deprecated use {@link Lists#newLinkedList()}
     */
    @Deprecated
    public static <E> LinkedList<E> newLinkedList() {
        return new LinkedList<E>();
    }

    /**
     * {@link LinkedList}?????
     * 
     * @param <E> {@link LinkedList}??
     * @param c ?????
     * @return {@link LinkedList}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see LinkedList#LinkedList(Collection)
     * @deprecated use {@link Lists#newLinkedList(Iterable)}
     */
    @Deprecated
    public static <E> LinkedList<E> newLinkedList(Collection<? extends E> c) {
        Validate.notNull(c);
        return new LinkedList<E>(c);
    }

    /**
     * {@link PriorityBlockingQueue}?????
     * 
     * @param <E> {@link PriorityBlockingQueue}??
     * @return {@link PriorityBlockingQueue}???
     * @see PriorityBlockingQueue#PriorityBlockingQueue()
     */
    public static <E> PriorityBlockingQueue<E> newPriorityBlockingQueue() {
        return new PriorityBlockingQueue<E>();
    }

    /**
     * {@link PriorityBlockingQueue}?????
     * 
     * @param <E> {@link PriorityBlockingQueue}??
     * @param c ?????
     * @return {@link PriorityBlockingQueue}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see PriorityBlockingQueue#PriorityBlockingQueue(Collection)
     */
    public static <E> PriorityBlockingQueue<E> newPriorityBlockingQueue(Collection<? extends E> c) {
        Validate.notNull(c);
        return new PriorityBlockingQueue<E>(c);
    }

    /**
     * {@link PriorityBlockingQueue}?????
     * 
     * @param <E> {@link PriorityBlockingQueue}??
     * @param initialCapacity ?????
     * @return {@link PriorityBlockingQueue}???
     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less than 1
     * @see PriorityBlockingQueue#PriorityBlockingQueue(int)
     */
    public static <E> PriorityBlockingQueue<E> newPriorityBlockingQueue(int initialCapacity) {
        return new PriorityBlockingQueue<E>(initialCapacity);
    }

    /**
     * {@link PriorityBlockingQueue}?????
     * 
     * @param <E> {@link PriorityBlockingQueue}??
     * @param initialCapacity ?????
     * @param comparator ???????
     * @return {@link PriorityBlockingQueue}???
     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less than 1
     * @see PriorityBlockingQueue#PriorityBlockingQueue(int, Comparator)
     */
    public static <E> PriorityBlockingQueue<E> newPriorityBlockingQueue(int initialCapacity,
            Comparator<? super E> comparator) {
        return new PriorityBlockingQueue<E>(initialCapacity, comparator);
    }

    /**
     * {@link PriorityQueue}?????
     * 
     * @param <E> {@link PriorityQueue}??
     * @return {@link PriorityQueue}???
     * @see PriorityQueue#PriorityQueue()
     */
    public static <E> PriorityQueue<E> newPriorityQueue() {
        return new PriorityQueue<E>();
    }

    /**
     * {@link PriorityQueue}?????
     * 
     * @param <E> {@link PriorityQueue}??
     * @param c ?????
     * @return {@link PriorityQueue}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see PriorityQueue#PriorityQueue(Collection)
     */
    public static <E> PriorityQueue<E> newPriorityQueue(Collection<? extends E> c) {
        Validate.notNull(c);
        return new PriorityQueue<E>(c);
    }

    /**
     * {@link PriorityQueue}?????
     * 
     * @param <E> {@link PriorityQueue}??
     * @param initialCapacity ?????
     * @return {@link PriorityQueue}???
     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less than 1
     * @see PriorityQueue#PriorityQueue(int)
     */
    public static <E> PriorityQueue<E> newPriorityQueue(int initialCapacity) {
        return new PriorityQueue<E>(initialCapacity);
    }

    /**
     * {@link PriorityQueue}?????
     * 
     * @param <E> {@link PriorityQueue}??
     * @param initialCapacity ?????
     * @param comparator ???????
     * @return {@link PriorityQueue}???
     * @throws IllegalArgumentException if <tt>initialCapacity</tt> is less than 1
     * @see PriorityQueue#PriorityQueue(int, Comparator)
     */
    public static <E> PriorityQueue<E> newPriorityQueue(int initialCapacity, Comparator<? super E> comparator) {
        return new PriorityQueue<E>(initialCapacity, comparator);
    }

    /**
     * {@link PriorityQueue}?????
     * 
     * @param <E> {@link PriorityQueue}??
     * @param c ?????
     * @return {@link PriorityQueue}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see PriorityQueue#PriorityQueue(PriorityQueue)
     */
    public static <E> PriorityQueue<E> newPriorityQueue(PriorityQueue<? extends E> c) {
        Validate.notNull(c);
        return new PriorityQueue<E>(c);
    }

    /**
     * {@link PriorityQueue}?????
     * 
     * @param <E> {@link PriorityQueue}??
     * @param c ?????
     * @return {@link PriorityQueue}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see PriorityQueue#PriorityQueue(SortedSet)
     */
    public static <E> PriorityQueue<E> newPriorityQueue(SortedSet<? extends E> c) {
        Validate.notNull(c);
        return new PriorityQueue<E>(c);
    }

    /**
     * {@link Stack}?????
     * 
     * @param <E> {@link Stack}??
     * @return {@link Stack}???
     * @see Stack#Stack()
     */
    public static <E> Stack<E> newStack() {
        return new Stack<E>();
    }

    /**
     * {@link TreeMap}?????
     * 
     * @param <K> {@link TreeMap}??
     * @param <V> {@link TreeMap}??
     * @return {@link TreeMap}???
     * @see TreeMap#TreeMap()
     * @deprecated use {@link Maps#newTreeMap()}
     */
    @Deprecated
    public static <K, V> TreeMap<K, V> newTreeMap() {
        return new TreeMap<K, V>();
    }

    /**
     * {@link TreeMap}?????
     * 
     * @param <K> {@link TreeMap}??
     * @param <V> {@link TreeMap}??
     * @param c {@link Comparator}
     * @return {@link TreeMap}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see TreeMap#TreeMap()
     * @deprecated use {@link Maps#newTreeMap(Comparator)}
     */
    @Deprecated
    public static <K, V> TreeMap<K, V> newTreeMap(Comparator<? super K> c) {
        Validate.notNull(c);
        return new TreeMap<K, V>(c);
    }

    /**
     * {@link TreeMap}?????
     * 
     * @param <K> {@link TreeMap}??
     * @param <V> {@link TreeMap}??
     * @param m ?????
     * @return {@link TreeMap}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see TreeMap#TreeMap(Map)
     */
    public static <K, V> TreeMap<K, V> newTreeMap(Map<? extends K, ? extends V> m) {
        Validate.notNull(m);
        return new TreeMap<K, V>(m);
    }

    /**
     * {@link TreeMap}?????
     * 
     * @param <K> {@link TreeMap}??
     * @param <V> {@link TreeMap}??
     * @param m ?????
     * @return {@link TreeMap}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see TreeMap#TreeMap(SortedMap)
     * @deprecated use {@link Maps#newTreeMap(SortedMap)}
     */
    @Deprecated
    public static <K, V> TreeMap<K, V> newTreeMap(SortedMap<K, ? extends V> m) {
        Validate.notNull(m);
        return new TreeMap<K, V>(m);
    }

    /**
     * {@link TreeSet}?????
     * 
     * @param <E> {@link TreeSet}??
     * @return {@link TreeSet}???
     * @see TreeSet#TreeSet()
     * @deprecated use {@link Sets#newTreeSet()}
     */
    @Deprecated
    public static <E> TreeSet<E> newTreeSet() {
        return new TreeSet<E>();
    }

    /**
     * {@link TreeSet}?????
     * 
     * @param <E> {@link TreeSet}??
     * @param c ?????
     * @return {@link TreeSet}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see TreeSet#TreeSet(Collection)
     * @deprecated use {@link Sets#newTreeSet(Iterable)}
     */
    @Deprecated
    public static <E> TreeSet<E> newTreeSet(Collection<? extends E> c) {
        Validate.notNull(c);
        return new TreeSet<E>(c);
    }

    /**
     * {@link TreeSet}?????
     * 
     * @param <E> {@link TreeSet}??
     * @param c ???????
     * @return {@link TreeSet}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see TreeSet#TreeSet(Comparator)
     * @deprecated use {@link Sets#newTreeSet(Comparator)}
     */
    @Deprecated
    public static <E> TreeSet<E> newTreeSet(Comparator<? super E> c) {
        Validate.notNull(c);
        return new TreeSet<E>(c);
    }

    /**
     * {@link TreeSet}?????
     * 
     * @param <E> {@link TreeSet}??
     * @param s ?????
     * @return {@link TreeSet}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see TreeSet#TreeSet(SortedSet)
     * @deprecated use {@link Sets#newTreeSet(Iterable)}
     */
    @Deprecated
    public static <E> TreeSet<E> newTreeSet(SortedSet<? extends E> s) {
        Validate.notNull(s);
        return new TreeSet<E>(s);
    }

    /**
     * {@link Vector}?????
     * 
     * @param <E> {@link Vector}??
     * @return {@link Vector}???
     * @see Vector#Vector()
     */
    public static <E> Vector<E> newVector() {
        return new Vector<E>();
    }

    /**
     * {@link Vector}?????
     * 
     * @param <E> {@link Vector}??
     * @param c ?????
     * @return {@link Vector}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see Vector#Vector(Collection)
     */
    public static <E> Vector<E> newVector(Collection<? extends E> c) {
        Validate.notEmpty(c);
        return new Vector<E>(c);
    }

    /**
     * {@link Vector}?????
     * 
     * @param <E> {@link Vector}??
     * @param initialCapacity {@link Vector}???
     * @return {@link Vector}???
     * @throws IllegalArgumentException if the specified initial capacity is negative
     * @see Vector#Vector(int)
     */
    public static <E> Vector<E> newVector(int initialCapacity) {
        return new Vector<E>(initialCapacity);
    }

    /**
     * {@link Vector}?????
     * 
     * @param <E> {@link Vector}??
     * @param initialCapacity {@link Vector}???
     * @param capacityIncrement {@link Vector}??????????
     * @return {@link Vector}???
     * @throws IllegalArgumentException if the specified initial capacity is negative
     * @see Vector#Vector(int, int)
     */
    public static <E> Vector<E> newVector(int initialCapacity, int capacityIncrement) {
        return new Vector<E>(initialCapacity, capacityIncrement);
    }

    /**
     * {@link WeakHashMap}?????
     * 
     * @param <K> {@link WeakHashMap}??
     * @param <V> {@link WeakHashMap}??
     * @return {@link WeakHashMap}???
     * @see WeakHashMap#WeakHashMap()
     */
    public static <K, V> WeakHashMap<K, V> newWeakHashMap() {
        return new WeakHashMap<K, V>();
    }

    /**
     * {@link WeakHashMap}?????
     * 
     * @param <K> {@link WeakHashMap}??
     * @param <V> {@link WeakHashMap}??
     * @param initialCapacity ??
     * @return {@link WeakHashMap}???
     * @throws IllegalArgumentException  If the initial capacity is negative.
     * @see WeakHashMap#WeakHashMap(int)
     */
    public static <K, V> WeakHashMap<K, V> newWeakHashMap(int initialCapacity) {
        return new WeakHashMap<K, V>(initialCapacity);
    }

    /**
     * {@link WeakHashMap}?????
     * 
     * @param <K> {@link WeakHashMap}??
     * @param <V> {@link WeakHashMap}??
     * @param initialCapacity ??
     * @param loadFactor ?????????
     * @return {@link WeakHashMap}???
     * @throws IllegalArgumentException  If the initial capacity is negative,
     *         or if the load factor is nonpositive.
     * @see WeakHashMap#WeakHashMap(int, float)
     */
    public static <K, V> WeakHashMap<K, V> newWeakHashMap(int initialCapacity, float loadFactor) {
        return new WeakHashMap<K, V>(initialCapacity, loadFactor);
    }

    /**
     * {@link WeakHashMap}?????
     * 
     * @param <K> {@link WeakHashMap}??
     * @param <V> {@link WeakHashMap}??
     * @param m ?????
     * @return {@link WeakHashMap}???
     * @throws IllegalArgumentException ?{@code null}???
     * @see WeakHashMap#WeakHashMap(Map)
     */
    public static <K, V> WeakHashMap<K, V> newWeakHashMap(Map<? extends K, ? extends V> m) {
        Validate.notNull(m);
        return new WeakHashMap<K, V>(m);
    }

    /**
     * ?????????????????
     * 
     * <p>
     * ???????????? ???????? ???? ??????
     * ????????????? ????? ?????????????
     * ???????????????????
     * </p>
     * 
     * @param <K> {@link HashMap}??
     * @param <V> {@link HashMap}??
     * @param map 
     * @param key ???
     * @param value ???
     * @return ???????????????
     * @throws IllegalArgumentException {@code map}?{@code null}???
     * @see ConcurrentHashMap#putIfAbsent(Object, Object)
     */
    public static <K, V> V putIfAbsent(ConcurrentMap<K, V> map, K key, V value) {
        Validate.notNull(map);
        V exists = map.putIfAbsent(key, value);
        if (exists != null) {
            return exists;
        }
        return value;
    }

    private CollectionsUtil() {
    }

}