Java LinkedBlockingQueue getCollection(Class propertyType)

Here you can find the source of getCollection(Class propertyType)

Description

get Collection

License

Open Source License

Declaration

public static Collection<Object> getCollection(Class<?> propertyType) 

Method Source Code

//package com.java2s;
/*/*from w  w  w . ja v  a2s  .  co m*/
 * The MIT License (MIT)
 *
 * Copyright (c) 2013 AgileApes, Ltd.
 *
 * 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.
 */

import java.util.*;
import java.util.concurrent.*;

public class Main {
    public static Collection<Object> getCollection(Class<?> propertyType) {
        if (!Collection.class.isAssignableFrom(propertyType)) {
            throw new Error("Expected property to be a collection while it was " + propertyType.getCanonicalName());
        }
        if (Set.class.isAssignableFrom(propertyType)) {
            if (TreeSet.class.isAssignableFrom(propertyType)) {
                return new TreeSet<Object>();
            } else if (ConcurrentSkipListSet.class.isAssignableFrom(propertyType)) {
                return new ConcurrentSkipListSet<Object>();
            } else if (CopyOnWriteArraySet.class.isAssignableFrom(propertyType)) {
                return new CopyOnWriteArraySet<Object>();
            }
            return new HashSet<Object>();
        } else if (List.class.isAssignableFrom(propertyType)) {
            if (LinkedList.class.isAssignableFrom(propertyType)) {
                return new LinkedList<Object>();
            } else if (CopyOnWriteArrayList.class.isAssignableFrom(propertyType)) {
                return new CopyOnWriteArrayList<Object>();
            }
            return new ArrayList<Object>();
        } else if (Queue.class.isAssignableFrom(propertyType)) {
            if (ConcurrentLinkedQueue.class.isAssignableFrom(propertyType)) {
                return new ConcurrentLinkedQueue<Object>();
            } else if (PriorityQueue.class.isAssignableFrom(propertyType)) {
                return new PriorityQueue<Object>();
            } else if (ConcurrentLinkedQueue.class.isAssignableFrom(propertyType)) {
                return new ConcurrentLinkedQueue<Object>();
            } else if (LinkedBlockingQueue.class.isAssignableFrom(propertyType)) {
                return new LinkedBlockingQueue<Object>();
            } else if (SynchronousQueue.class.isAssignableFrom(propertyType)) {
                return new SynchronousQueue<Object>();
            } else if (PriorityBlockingQueue.class.isAssignableFrom(propertyType)) {
                return new PriorityBlockingQueue<Object>();
            }
            return new PriorityQueue<Object>();
        } else if (Collection.class.equals(propertyType)) {
            return new ArrayList<Object>();
        } else {
            throw new UnsupportedOperationException(
                    "Cannot instantiate a collection of type " + propertyType.getCanonicalName());
        }
    }
}

Related

  1. createLinkedBlockingQueue(Collection collection)
  2. getCollection(Class collectionType)
  3. newLinkedBlockingQeque(int capacity)
  4. newLinkedBlockingQueue(final Collection c)