com.sun.electric.tool.util.concurrent.utils.ConcurrentCollectionFactory Maven / Gradle / Ivy
/* -*- tab-width: 4 -*-
*
* Electric(tm) VLSI Design System
*
* File: ConcurrentCollectionFactory.java
*
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
*
* Electric(tm) is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* Electric(tm) is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Electric(tm); see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, Mass 02111-1307, USA.
*/
package com.sun.electric.tool.util.concurrent.utils;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
import com.sun.electric.tool.util.concurrent.datastructures.BDEQueue;
import com.sun.electric.tool.util.concurrent.datastructures.FCQueue;
import com.sun.electric.tool.util.concurrent.datastructures.LockFreeQueue;
import com.sun.electric.tool.util.concurrent.datastructures.LockFreeStack;
import com.sun.electric.tool.util.concurrent.datastructures.UnboundedDEQueue;
/**
* @author Felix Schmidt
*
*/
public class ConcurrentCollectionFactory {
private static final int LOG_CAPACITY = 4;
protected ConcurrentCollectionFactory() {
}
public static ArrayList createArrayList() {
return new ArrayList();
}
/**
* Create a new hash set
*
* @param
* @return HashSet of type T
*/
public static HashSet createHashSet() {
return new HashSet();
}
@SuppressWarnings("unchecked")
public static List createConcurrentList() {
return (List) Collections.synchronizedList(createArrayList());
}
public static ConcurrentLinkedQueue createConcurrentLinkedQueue() {
return new ConcurrentLinkedQueue();
}
public static ConcurrentHashMap createConcurrentHashMap() {
return new ConcurrentHashMap();
}
@SuppressWarnings("unchecked")
public static Set createConcurrentHashSet() {
return (Set) Collections.synchronizedSet(ConcurrentCollectionFactory.createHashSet());
}
public static Set copySetToConcurrent(Set source) {
Set result = ConcurrentCollectionFactory.createConcurrentHashSet();
doCopyCollection(source, result);
return result;
}
public static LinkedList createLinkedList() {
return new LinkedList();
}
public static LockFreeQueue createLockFreeQueue() {
return new LockFreeQueue();
}
public static LockFreeStack createLockFreeStack() {
return new LockFreeStack();
}
/**
* Create a new double ended queue (concurrent).
*/
public static UnboundedDEQueue createUnboundedDoubleEndedQueue() {
return new UnboundedDEQueue(LOG_CAPACITY);
}
/**
* Create a new double ended queue (concurrent).
*/
public static BDEQueue createBoundedDoubleEndedQueue(int capacity) {
return new BDEQueue(capacity);
}
public static FCQueue createFCQueue() {
return new FCQueue();
}
protected static void doCopyCollection(Collection source, Collection dest) {
for (Iterator it = source.iterator(); it.hasNext();) {
dest.add(it.next());
}
}
public static T threadSafeListGet(int index, List list) {
synchronized (list) {
return list.get(index);
}
}
public static T threadSafeListRemove(int index, List list) {
synchronized (list) {
return list.remove(index);
}
}
/**
*
* @param
* @param item
* @param list
*/
public static void threadSafeListAdd(T item, List list) {
synchronized (list) {
list.add(item);
}
}
}