com.sun.electric.util.CollectionFactory Maven / Gradle / Ivy
The newest version!
/* -*- 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.util;
import com.sun.electric.util.collections.ImmutableList;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedQueue;
/**
* This class provides factory methods for creating data structures. The
* intension is that the generic generation of data structures should be
* hidden to make the code readable.
*
* @author Felix Schmidt
*
*/
public class CollectionFactory {
private CollectionFactory() {
}
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(createHashSet());
}
public static Set copySetToConcurrent(Set source) {
Set result = createConcurrentHashSet();
doCopyCollection(source, result);
return result;
}
public static LinkedList createLinkedList() {
return new LinkedList();
}
private 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);
}
}
public static HashMap createHashMap() {
return new HashMap();
}
/**
*
* @param
* @param source
* @return
*/
public static Set copySet(Set source) {
Set result = CollectionFactory.createHashSet();
doCopyCollection(source, result);
return result;
}
public static Set copyListToSet(List source) {
Set result = CollectionFactory.createHashSet();
doCopyCollection(source, result);
return result;
}
public static List copySetToList(Set source) {
List result = CollectionFactory.createArrayList();
doCopyCollection(source, result);
return result;
}
public static ImmutableList copyListToImmutableList(List source) {
ImmutableList immutableList = null;
for (T element : source) {
immutableList = ImmutableList.add(immutableList, element);
}
return immutableList;
}
public static T[] arrayMerge(T[]... arrays) {
Class objectClass = null;
int count = 0;
for(T[] array: arrays) {
if(array != null) {
count +=array.length;
if(array.length > 0)
objectClass = array[0].getClass();
}
}
if(objectClass == null)
return null;
List mergedList = new ArrayList();
for(T[] array: arrays)
if(array != null)
mergedList.addAll(Arrays.asList(array));
return mergedList.toArray((T[])Array.newInstance(objectClass, count));
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy