1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.util;
24  
25  import com.liferay.portal.kernel.util.GetterUtil;
26  
27  import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap;
28  import edu.emory.mathcs.backport.java.util.concurrent.CopyOnWriteArrayList;
29  
30  import gnu.trove.THashMap;
31  import gnu.trove.THashSet;
32  import gnu.trove.TLinkedList;
33  
34  import java.util.HashMap;
35  import java.util.HashSet;
36  import java.util.LinkedList;
37  import java.util.List;
38  import java.util.Map;
39  import java.util.Set;
40  
41  /**
42   * <a href="CollectionFactory.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Brian Wing Shun Chan
45   *
46   */
47  public class CollectionFactory {
48  
49      static boolean useTrove = GetterUtil.getBoolean(
50          SystemProperties.get("trove"), true);
51  
52      static {
53          if (useTrove) {
54              try {
55                  Class.forName("gnu.trove.THashMap");
56              }
57              catch (Exception e) {
58                  useTrove = false;
59              }
60          }
61      }
62  
63      public static Map getHashMap() {
64          if (useTrove) {
65              return new THashMap();
66          }
67          else {
68              return new HashMap();
69          }
70      }
71  
72      public static Map getHashMap(int capacity) {
73          if (useTrove) {
74              return new THashMap(capacity);
75          }
76          else {
77              return new HashMap(capacity);
78          }
79      }
80  
81      public static Set getHashSet() {
82          if (useTrove) {
83              return new THashSet();
84          }
85          else {
86              return new HashSet();
87          }
88      }
89  
90      public static Set getHashSet(int capacity) {
91          if (useTrove) {
92              return new THashSet(capacity);
93          }
94          else {
95              return new HashSet(capacity);
96          }
97      }
98  
99      public static List getLinkedList() {
100         if (useTrove) {
101             return new TLinkedList();
102         }
103         else {
104             return new LinkedList();
105         }
106     }
107 
108     public static List getSyncArrayList() {
109         return new CopyOnWriteArrayList();
110     }
111 
112     public static List getSyncArrayList(int capacity) {
113         return new CopyOnWriteArrayList();
114     }
115 
116     public static List getSyncArrayList(List list) {
117         return new CopyOnWriteArrayList(list);
118     }
119 
120     public static Map getSyncHashMap() {
121         return new ConcurrentHashMap();
122     }
123 
124     public static Map getSyncHashMap(int capacity) {
125         return new ConcurrentHashMap(capacity);
126     }
127 
128     public static Map getSyncHashMap(Map map) {
129         return new ConcurrentHashMap(map);
130     }
131 
132     public static Set getSyncHashSet() {
133         return new ConcurrentHashSet();
134     }
135 
136     public static Set getSyncHashSet(int capacity) {
137         return new ConcurrentHashSet(capacity);
138     }
139 
140     public static Set getSyncHashSet(Set set) {
141         return new ConcurrentHashSet(set);
142     }
143 
144 }