001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.util;
022:
023: import com.liferay.portal.kernel.util.GetterUtil;
024:
025: import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
026: import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
027:
028: import gnu.trove.THashMap;
029: import gnu.trove.THashSet;
030: import gnu.trove.TLinkedList;
031:
032: import java.util.HashMap;
033: import java.util.HashSet;
034: import java.util.LinkedList;
035: import java.util.List;
036: import java.util.Map;
037: import java.util.Set;
038:
039: /**
040: * <a href="CollectionFactory.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Brian Wing Shun Chan
043: *
044: */
045: public class CollectionFactory {
046:
047: static boolean useTrove = GetterUtil.getBoolean(SystemProperties
048: .get("trove"), true);
049:
050: static {
051: if (useTrove) {
052: try {
053: Class.forName("gnu.trove.THashMap");
054: } catch (Exception e) {
055: useTrove = false;
056: }
057: }
058: }
059:
060: public static Map getHashMap() {
061: if (useTrove) {
062: return new THashMap();
063: } else {
064: return new HashMap();
065: }
066: }
067:
068: public static Map getHashMap(int capacity) {
069: if (useTrove) {
070: return new THashMap(capacity);
071: } else {
072: return new HashMap(capacity);
073: }
074: }
075:
076: public static Set getHashSet() {
077: if (useTrove) {
078: return new THashSet();
079: } else {
080: return new HashSet();
081: }
082: }
083:
084: public static Set getHashSet(int capacity) {
085: if (useTrove) {
086: return new THashSet(capacity);
087: } else {
088: return new HashSet(capacity);
089: }
090: }
091:
092: public static List getLinkedList() {
093: if (useTrove) {
094: return new TLinkedList();
095: } else {
096: return new LinkedList();
097: }
098: }
099:
100: public static List getSyncArrayList() {
101: return new CopyOnWriteArrayList();
102: }
103:
104: public static List getSyncArrayList(int capacity) {
105: return new CopyOnWriteArrayList();
106: }
107:
108: public static List getSyncArrayList(List list) {
109: return new CopyOnWriteArrayList(list);
110: }
111:
112: public static Map getSyncHashMap() {
113: return new ConcurrentHashMap();
114: }
115:
116: public static Map getSyncHashMap(int capacity) {
117: return new ConcurrentHashMap(capacity);
118: }
119:
120: public static Map getSyncHashMap(Map map) {
121: return new ConcurrentHashMap(map);
122: }
123:
124: public static Set getSyncHashSet() {
125: return new ConcurrentHashSet();
126: }
127:
128: public static Set getSyncHashSet(int capacity) {
129: return new ConcurrentHashSet(capacity);
130: }
131:
132: public static Set getSyncHashSet(Set set) {
133: return new ConcurrentHashSet(set);
134: }
135:
136: }
|