package edu.stanford.nlp.util;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.SortedSet;
import java.util.Stack;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.WeakHashMap;
import java.util.concurrent.ConcurrentHashMap;
import edu.stanford.nlp.util.concurrent.SynchronizedInterner;
/**
* A collection of utilities to make dealing with Java generics less
* painful and verbose. For example, rather than declaring
*
*
* {@code Map>> = new HashMap>>()}
*
*
* you just call Generics.newHashMap()
:
*
*
* {@code Map>> = Generics.newHashMap()}
*
*
* Java type-inference will almost always just do the right thing
* (every once in a while, the compiler will get confused before you do,
* so you might still occasionally have to specify the appropriate types).
*
* This class is based on the examples in Brian Goetz's article
* Java
* theory and practice: The pseudo-typedef antipattern.
*
* @author Ilya Sherman
*/
public class Generics {
/* Collections */
public static ArrayList newArrayList() {
return new ArrayList();
}
public static ArrayList newArrayList(int size) {
return new ArrayList(size);
}
public static ArrayList newArrayList(Collection extends E> c) {
return new ArrayList(c);
}
public static LinkedList newLinkedList() {
return new LinkedList();
}
public static LinkedList newLinkedList(Collection extends E> c) {
return new LinkedList(c);
}
public static HashSet newHashSet() {
return new HashSet();
}
public static HashSet newHashSet(int initialCapacity) {
return new HashSet(initialCapacity);
}
public static HashSet newHashSet(Collection extends E> c) {
return new HashSet(c);
}
public static TreeSet newTreeSet() {
return new TreeSet();
}
public static TreeSet newTreeSet(Comparator super E> comparator) {
return new TreeSet(comparator);
}
public static TreeSet newTreeSet(SortedSet s) {
return new TreeSet(s);
}
public static Stack newStack() {
return new Stack();
}
public static BinaryHeapPriorityQueue newBinaryHeapPriorityQueue() {
return new BinaryHeapPriorityQueue();
}
/* Maps */
public static HashMap newHashMap() {
return new HashMap();
}
public static HashMap newHashMap(int initialCapacity) {
return new HashMap(initialCapacity);
}
public static HashMap newHashMap(Map extends K,? extends V> m) {
return new HashMap(m);
}
public static WeakHashMap newWeakHashMap() {
return new WeakHashMap();
}
public static ConcurrentHashMap newConcurrentHashMap() {
return new ConcurrentHashMap();
}
public static ConcurrentHashMap newConcurrentHashMap(int initialCapacity) {
return new ConcurrentHashMap(initialCapacity);
}
public static ConcurrentHashMap newConcurrentHashMap(int initialCapacity,
float loadFactor, int concurrencyLevel) {
return new ConcurrentHashMap(initialCapacity, loadFactor, concurrencyLevel);
}
public static TreeMap newTreeMap() {
return new TreeMap();
}
public static Index newIndex() {
return new Index();
}
/* Other */
public static Pair newPair(T1 first, T2 second) {
return new Pair(first, second);
}
public static Triple newTriple(T1 first, T2 second, T3 third) {
return new Triple(first, second, third);
}
public static Interner newInterner() {
return new Interner();
}
public static SynchronizedInterner newSynchronizedInterner(Interner interner) {
return new SynchronizedInterner(interner);
}
public static SynchronizedInterner newSynchronizedInterner(Interner interner,
Object mutex) {
return new SynchronizedInterner(interner, mutex);
}
public static WeakReference newWeakReference(T referent) {
return new WeakReference(referent);
}
}
/*
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
*
* Copyright 1997-2009 Sun Microsystems, Inc. All rights reserved.
*
* The contents of this file are subject to the terms of either the GNU
* General Public License Version 2 only ("GPL") or the Common
* Development and Distribution License("CDDL") (collectively, the
* "License"). You may not use this file except in compliance with the
* License. You can obtain a copy of the License at
* http://www.netbeans.org/cddl-gplv2.html
* or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
* specific language governing permissions and limitations under the
* License. When distributing the software, include this License Header
* Notice in each file and include the License file at
* nbbuild/licenses/CDDL-GPL-2-CP. Sun designates this
* particular file as subject to the "Classpath" exception as provided
* by Sun in the GPL Version 2 section of the License file that
* accompanied this code. If applicable, add the following below the
* License Header, with the fields enclosed by brackets [] replaced by
* your own identifying information:
* "Portions Copyrighted [year] [name of copyright owner]"
*
* Contributor(s):
*
* The Original Software is NetBeans. The Initial Developer of the Original
* Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
* Microsystems, Inc. All Rights Reserved.
*
* If you wish your version of this file to be governed by only the CDDL
* or only the GPL Version 2, indicate your decision by adding
* "[Contributor] elects to include this software in this distribution
* under the [CDDL or GPL Version 2] license." If you do not indicate a
* single choice of license, a recipient has the option to distribute
* your version of this file under either the CDDL, the GPL Version 2 or
* to extend the choice of license to its licensees as provided above.
* However, if you add GPL Version 2 code and therefore, elected the GPL
* Version 2 license, then the option applies only if the new code is
* made subject to such option by the copyright holder.
*/
package org.netbeans.test.codegen;
import java.util.Collection;
import java.util.List;
/**
*
* @author Martin Matula
*/
public class Generics extends java.util.AbstractList implements List {
private final List inner;
/** Creates a new instance of Generics */
public Generics(List innerList) {
inner = innerList;
}
public T get(int index) {
return inner.get(index);
}
public int size() {
return inner.size();
}
}