Generics.java 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. package edu.stanford.nlp.util;
  2. import java.lang.ref.WeakReference;
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.Comparator;
  6. import java.util.HashMap;
  7. import java.util.HashSet;
  8. import java.util.LinkedList;
  9. import java.util.Map;
  10. import java.util.SortedSet;
  11. import java.util.Stack;
  12. import java.util.TreeMap;
  13. import java.util.TreeSet;
  14. import java.util.WeakHashMap;
  15. import java.util.concurrent.ConcurrentHashMap;
  16. import edu.stanford.nlp.util.concurrent.SynchronizedInterner;
  17. /**
  18. * A collection of utilities to make dealing with Java generics less
  19. * painful and verbose. For example, rather than declaring
  20. *
  21. * <pre>
  22. * {@code Map<String, List<Pair<IndexedWord, GrammaticalRelation>>> = new HashMap<String, List<Pair<IndexedWord, GrammaticalRelation>>>()}
  23. * </pre>
  24. *
  25. * you just call <code>Generics.newHashMap()</code>:
  26. *
  27. * <pre>
  28. * {@code Map<String, List<Pair<IndexedWord, GrammaticalRelation>>> = Generics.newHashMap()}
  29. * </pre>
  30. *
  31. * Java type-inference will almost always just <em>do the right thing</em>
  32. * (every once in a while, the compiler will get confused before you do,
  33. * so you might still occasionally have to specify the appropriate types).
  34. *
  35. * This class is based on the examples in Brian Goetz's article
  36. * <a href="http://www.ibm.com/developerworks/library/j-jtp02216.html">Java
  37. * theory and practice: The pseudo-typedef antipattern</a>.
  38. *
  39. * @author Ilya Sherman
  40. */
  41. public class Generics {
  42. /* Collections */
  43. public static <E> ArrayList<E> newArrayList() {
  44. return new ArrayList<E>();
  45. }
  46. public static <E> ArrayList<E> newArrayList(int size) {
  47. return new ArrayList<E>(size);
  48. }
  49. public static <E> ArrayList<E> newArrayList(Collection<? extends E> c) {
  50. return new ArrayList<E>(c);
  51. }
  52. public static <E> LinkedList<E> newLinkedList() {
  53. return new LinkedList<E>();
  54. }
  55. public static <E> LinkedList<E> newLinkedList(Collection<? extends E> c) {
  56. return new LinkedList<E>(c);
  57. }
  58. public static <E> HashSet<E> newHashSet() {
  59. return new HashSet<E>();
  60. }
  61. public static <E> HashSet<E> newHashSet(int initialCapacity) {
  62. return new HashSet<E>(initialCapacity);
  63. }
  64. public static <E> HashSet<E> newHashSet(Collection<? extends E> c) {
  65. return new HashSet<E>(c);
  66. }
  67. public static <E> TreeSet<E> newTreeSet() {
  68. return new TreeSet<E>();
  69. }
  70. public static <E> TreeSet<E> newTreeSet(Comparator<? super E> comparator) {
  71. return new TreeSet<E>(comparator);
  72. }
  73. public static <E> TreeSet<E> newTreeSet(SortedSet<E> s) {
  74. return new TreeSet<E>(s);
  75. }
  76. public static <E> Stack<E> newStack() {
  77. return new Stack<E>();
  78. }
  79. public static <E> BinaryHeapPriorityQueue<E> newBinaryHeapPriorityQueue() {
  80. return new BinaryHeapPriorityQueue<E>();
  81. }
  82. /* Maps */
  83. public static <K,V> HashMap<K,V> newHashMap() {
  84. return new HashMap<K,V>();
  85. }
  86. public static <K,V> HashMap<K,V> newHashMap(int initialCapacity) {
  87. return new HashMap<K,V>(initialCapacity);
  88. }
  89. public static <K,V> HashMap<K,V> newHashMap(Map<? extends K,? extends V> m) {
  90. return new HashMap<K,V>(m);
  91. }
  92. public static <K,V> WeakHashMap<K,V> newWeakHashMap() {
  93. return new WeakHashMap<K,V>();
  94. }
  95. public static <K,V> ConcurrentHashMap<K,V> newConcurrentHashMap() {
  96. return new ConcurrentHashMap<K,V>();
  97. }
  98. public static <K,V> ConcurrentHashMap<K,V> newConcurrentHashMap(int initialCapacity) {
  99. return new ConcurrentHashMap<K,V>(initialCapacity);
  100. }
  101. public static <K,V> ConcurrentHashMap<K,V> newConcurrentHashMap(int initialCapacity,
  102. float loadFactor, int concurrencyLevel) {
  103. return new ConcurrentHashMap<K,V>(initialCapacity, loadFactor, concurrencyLevel);
  104. }
  105. public static <K,V> TreeMap<K,V> newTreeMap() {
  106. return new TreeMap<K,V>();
  107. }
  108. public static <E> Index<E> newIndex() {
  109. return new Index<E>();
  110. }
  111. /* Other */
  112. public static <T1,T2> Pair<T1,T2> newPair(T1 first, T2 second) {
  113. return new Pair<T1,T2>(first, second);
  114. }
  115. public static <T1,T2, T3> Triple<T1,T2, T3> newTriple(T1 first, T2 second, T3 third) {
  116. return new Triple<T1,T2, T3>(first, second, third);
  117. }
  118. public static <T> Interner<T> newInterner() {
  119. return new Interner<T>();
  120. }
  121. public static <T> SynchronizedInterner<T> newSynchronizedInterner(Interner<T> interner) {
  122. return new SynchronizedInterner<T>(interner);
  123. }
  124. public static <T> SynchronizedInterner<T> newSynchronizedInterner(Interner<T> interner,
  125. Object mutex) {
  126. return new SynchronizedInterner<T>(interner, mutex);
  127. }
  128. public static <T> WeakReference<T> newWeakReference(T referent) {
  129. return new WeakReference<T>(referent);
  130. }
  131. }
  132. /*
  133. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
  134. *
  135. * Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
  136. *
  137. * The contents of this file are subject to the terms of either the GNU
  138. * General Public License Version 2 only ("GPL") or the Common
  139. * Development and Distribution License("CDDL") (collectively, the
  140. * "License"). You may not use this file except in compliance with the
  141. * License. You can obtain a copy of the License at
  142. * http://www.netbeans.org/cddl-gplv2.html
  143. * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
  144. * specific language governing permissions and limitations under the
  145. * License. When distributing the software, include this License Header
  146. * Notice in each file and include the License file at
  147. * nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
  148. * particular file as subject to the "Classpath" exception as provided
  149. * by Sun in the GPL Version 2 section of the License file that
  150. * accompanied this code. If applicable, add the following below the
  151. * License Header, with the fields enclosed by brackets [] replaced by
  152. * your own identifying information:
  153. * "Portions Copyrighted [year] [name of copyright owner]"
  154. *
  155. * Contributor(s):
  156. *
  157. * The Original Software is NetBeans. The Initial Developer of the Original
  158. * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
  159. * Microsystems, Inc. All Rights Reserved.
  160. *
  161. * If you wish your version of this file to be governed by only the CDDL
  162. * or only the GPL Version 2, indicate your decision by adding
  163. * "[Contributor] elects to include this software in this distribution
  164. * under the [CDDL or GPL Version 2] license." If you do not indicate a
  165. * single choice of license, a recipient has the option to distribute
  166. * your version of this file under either the CDDL, the GPL Version 2 or
  167. * to extend the choice of license to its licensees as provided above.
  168. * However, if you add GPL Version 2 code and therefore, elected the GPL
  169. * Version 2 license, then the option applies only if the new code is
  170. * made subject to such option by the copyright holder.
  171. */
  172. package org.netbeans.test.codegen;
  173. import java.util.Collection;
  174. import java.util.List;
  175. /**
  176. *
  177. * @author Martin Matula
  178. */
  179. public class Generics<T extends Collection> extends java.util.AbstractList<T> implements List<T> {
  180. private final List<T> inner;
  181. /** Creates a new instance of Generics */
  182. public Generics(List<T> innerList) {
  183. inner = innerList;
  184. }
  185. public T get(int index) {
  186. return inner.get(index);
  187. }
  188. public int size() {
  189. return inner.size();
  190. }
  191. }