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 EDU.oswego.cs.dl.util.concurrent.SyncMap;
26  import EDU.oswego.cs.dl.util.concurrent.SyncSet;
27  import EDU.oswego.cs.dl.util.concurrent.WriterPreferenceReadWriteLock;
28  
29  import gnu.trove.THashMap;
30  import gnu.trove.THashSet;
31  import gnu.trove.TLinkedList;
32  
33  import java.util.HashMap;
34  import java.util.HashSet;
35  import java.util.LinkedList;
36  import java.util.List;
37  import java.util.Map;
38  import java.util.Set;
39  
40  /**
41   * <a href="CollectionFactory.java.html"><b><i>View Source</i></b></a>
42   *
43   * @author  Brian Wing Shun Chan
44   *
45   */
46  public class CollectionFactory {
47  
48      static boolean useTrove = GetterUtil.getBoolean(
49          SystemProperties.get("trove"), true);
50  
51      static {
52          if (useTrove) {
53              try {
54                  Class.forName("gnu.trove.THashMap");
55              }
56              catch (Exception e) {
57                  useTrove = false;
58              }
59          }
60      }
61  
62      public static Map getHashMap() {
63          if (useTrove) {
64              return new THashMap();
65          }
66          else {
67              return new HashMap();
68          }
69      }
70  
71      public static Map getHashMap(int capacity) {
72          if (useTrove) {
73              return new THashMap(capacity);
74          }
75          else {
76              return new HashMap(capacity);
77          }
78      }
79  
80      public static Set getHashSet() {
81          if (useTrove) {
82              return new THashSet();
83          }
84          else {
85              return new HashSet();
86          }
87      }
88  
89      public static Set getHashSet(int capacity) {
90          if (useTrove) {
91              return new THashSet(capacity);
92          }
93          else {
94              return new HashSet(capacity);
95          }
96      }
97  
98      public static List getLinkedList() {
99          if (useTrove) {
100             return new TLinkedList();
101         }
102         else {
103             return new LinkedList();
104         }
105     }
106 
107     public static Map getSyncHashMap() {
108         return new SyncMap(getHashMap(), new WriterPreferenceReadWriteLock());
109     }
110 
111     public static Map getSyncHashMap(int capacity) {
112         return new SyncMap(
113             getHashMap(capacity), new WriterPreferenceReadWriteLock());
114     }
115 
116     public static Map getSyncHashMap(Map map) {
117         return new SyncMap(map, new WriterPreferenceReadWriteLock());
118     }
119 
120     public static Set getSyncHashSet() {
121         return new SyncSet(getHashSet(), new WriterPreferenceReadWriteLock());
122     }
123 
124     public static Set getSyncHashSet(int capacity) {
125         return new SyncSet(
126             getHashSet(capacity), new WriterPreferenceReadWriteLock());
127     }
128 
129     public static Set getSyncHashSet(Set set) {
130         return new SyncSet(set, new WriterPreferenceReadWriteLock());
131     }
132 
133 }