Skip to content

Commit

Permalink
Make one method take varargs; clean up bad javadoc.
Browse files Browse the repository at this point in the history
  • Loading branch information
manning authored and Stanford NLP committed Apr 12, 2015
1 parent c5596dd commit 0f932ac
Showing 1 changed file with 62 additions and 100 deletions.
162 changes: 62 additions & 100 deletions src/edu/stanford/nlp/util/CollectionUtils.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -72,12 +72,14 @@ public static double[] asDoubleArray(Collection<Double> coll) {
} }


/** Returns a new List containing the given objects. */ /** Returns a new List containing the given objects. */
@SafeVarargs
public static <T> List<T> makeList(T... items) { public static <T> List<T> makeList(T... items) {
return new ArrayList<T>(Arrays.asList(items)); return new ArrayList<T>(Arrays.asList(items));
} }


/** Returns a new Set containing all the objects in the specified array. */ /** Returns a new Set containing all the objects in the specified array. */
public static <T> Set<T> asSet(T[] o) { @SafeVarargs
public static <T> Set<T> asSet(T... o) {
return Generics.newHashSet(Arrays.asList(o)); return Generics.newHashSet(Arrays.asList(o));
} }


Expand Down Expand Up @@ -113,6 +115,7 @@ public static <T> Set<T> unionAsSet(Collection<T> set1, Collection<T> set2) {
return union; return union;
} }


@SafeVarargs
public static <T> Set<T> unionAsSet(Collection<T>... sets) { public static <T> Set<T> unionAsSet(Collection<T>... sets) {
Set<T> union = Generics.newHashSet(); Set<T> union = Generics.newHashSet();
for(Collection<T> set: sets){ for(Collection<T> set: sets){
Expand All @@ -124,7 +127,7 @@ public static <T> Set<T> unionAsSet(Collection<T>... sets) {
} }


/** /**
* Returns all objects in list1 that are not in list2 * Returns all objects in list1 that are not in list2.
* *
* @param <T> Type of items in the collection * @param <T> Type of items in the collection
* @param list1 First collection * @param list1 First collection
Expand All @@ -142,7 +145,7 @@ public static <T> Collection<T> diff(Collection<T> list1, Collection<T> list2) {
} }


/** /**
* Returns all objects in list1 that are not in list2 * Returns all objects in list1 that are not in list2.
* *
* @param <T> Type of items in the collection * @param <T> Type of items in the collection
* @param list1 First collection * @param list1 First collection
Expand All @@ -162,21 +165,17 @@ public static <T> Set<T> diffAsSet(Collection<T> list1, Collection<T> list2) {
// Utils for loading and saving Collections to/from text files // Utils for loading and saving Collections to/from text files


/** /**
* @param filename * @param filename The path to the file to load the List from
* the path to the file to load the List from * @param c The Class to instantiate each member of the List. Must have a
* @param c
* the Class to instantiate each member of the List. Must have a
* String constructor. * String constructor.
*/ */
public static <T> Collection<T> loadCollection(String filename, Class<T> c, CollectionFactory<T> cf) throws Exception { public static <T> Collection<T> loadCollection(String filename, Class<T> c, CollectionFactory<T> cf) throws Exception {
return loadCollection(new File(filename), c, cf); return loadCollection(new File(filename), c, cf);
} }


/** /**
* @param file * @param file The file to load the List from
* the file to load the List from * @param c The Class to instantiate each member of the List. Must have a
* @param c
* the Class to instantiate each member of the List. Must have a
* String constructor. * String constructor.
*/ */
public static <T> Collection<T> loadCollection(File file, Class<T> c, CollectionFactory<T> cf) throws Exception { public static <T> Collection<T> loadCollection(File file, Class<T> c, CollectionFactory<T> cf) throws Exception {
Expand All @@ -201,15 +200,10 @@ public static <T> Collection<T> loadCollection(File file, Class<T> c, Collection
/** /**
* Adds the items from the file to the collection. * Adds the items from the file to the collection.
* *
* @param <T> * @param <T> The type of the items.
* The type of the items. * @param fileName The name of the file from which items should be loaded.
* @param fileName * @param itemClass The class of the items (must have a constructor that accepts a String).
* The name of the file from which items should be loaded. * @param collection The collection to which items should be added.
* @param itemClass
* The class of the items (must have a constructor that accepts a
* String).
* @param collection
* The collection to which items should be added.
*/ */
public static <T> void loadCollection(String fileName, Class<T> itemClass, Collection<T> collection) throws NoSuchMethodException, InstantiationException, public static <T> void loadCollection(String fileName, Class<T> itemClass, Collection<T> collection) throws NoSuchMethodException, InstantiationException,
IllegalAccessException, InvocationTargetException, IOException { IllegalAccessException, InvocationTargetException, IOException {
Expand All @@ -219,15 +213,10 @@ public static <T> void loadCollection(String fileName, Class<T> itemClass, Colle
/** /**
* Adds the items from the file to the collection. * Adds the items from the file to the collection.
* *
* @param <T> * @param <T> The type of the items.
* The type of the items. * @param file The file from which items should be loaded.
* @param file * @param itemClass The class of the items (must have a constructor that accepts a String).
* The file from which items should be loaded. * @param collection The collection to which items should be added.
* @param itemClass
* The class of the items (must have a constructor that accepts a
* String).
* @param collection
* The collection to which items should be added.
*/ */
public static <T> void loadCollection(File file, Class<T> itemClass, Collection<T> collection) throws NoSuchMethodException, InstantiationException, IllegalAccessException, public static <T> void loadCollection(File file, Class<T> itemClass, Collection<T> collection) throws NoSuchMethodException, InstantiationException, IllegalAccessException,
InvocationTargetException, IOException { InvocationTargetException, IOException {
Expand Down Expand Up @@ -310,10 +299,8 @@ public static <T> boolean removeObject(List<T> l, T o) {
* object, using object identity (==) not equality as the criterion for object * object, using object identity (==) not equality as the criterion for object
* presence. If this list does not contain the element, return -1. * presence. If this list does not contain the element, return -1.
* *
* @param l * @param l The {@link List} to find the object in.
* The {@link List} to find the object in. * @param o The sought-after object.
* @param o
* The sought-after object.
* @return Whether or not the List was changed. * @return Whether or not the List was changed.
*/ */
public static <T> int getIndex(List<T> l, T o) { public static <T> int getIndex(List<T> l, T o) {
Expand All @@ -332,12 +319,9 @@ public static <T> int getIndex(List<T> l, T o) {
* in the list of the specified object, using object equals function. If this * in the list of the specified object, using object equals function. If this
* list does not contain the element, return -1. * list does not contain the element, return -1.
* *
* @param l * @param l The {@link List} to find the object in.
* The {@link List} to find the object in. * @param o The sought-after object.
* @param o * @param fromIndex The start index
* The sought-after object.
* @param fromIndex
* The start index
* @return Whether or not the List was changed. * @return Whether or not the List was changed.
*/ */
public static <T> int getIndex(List<T> l, T o, int fromIndex) { public static <T> int getIndex(List<T> l, T o, int fromIndex) {
Expand All @@ -357,10 +341,8 @@ public static <T> int getIndex(List<T> l, T o, int fromIndex) {
/** /**
* Samples without replacement from a collection. * Samples without replacement from a collection.
* *
* @param c * @param c The collection to be sampled from
* The collection to be sampled from * @param n The number of samples to take
* @param n
* The number of samples to take
* @return a new collection with the sample * @return a new collection with the sample
*/ */
public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n) { public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n) {
Expand All @@ -371,12 +353,9 @@ public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n)
* Samples without replacement from a collection, using your own * Samples without replacement from a collection, using your own
* {@link Random} number generator. * {@link Random} number generator.
* *
* @param c * @param c The collection to be sampled from
* The collection to be sampled from * @param n The number of samples to take
* @param n * @param r The random number generator
* The number of samples to take
* @param r
* the random number generator
* @return a new collection with the sample * @return a new collection with the sample
*/ */
public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n, Random r) { public static <E> Collection<E> sampleWithoutReplacement(Collection<E> c, int n, Random r) {
Expand All @@ -401,12 +380,10 @@ public static <E> E sample(List<E> l, Random r) {
} }


/** /**
* Samples with replacement from a collection * Samples with replacement from a collection.
* *
* @param c * @param c The collection to be sampled from
* The collection to be sampled from * @param n The number of samples to take
* @param n
* The number of samples to take
* @return a new collection with the sample * @return a new collection with the sample
*/ */
public static <E> Collection<E> sampleWithReplacement(Collection<E> c, int n) { public static <E> Collection<E> sampleWithReplacement(Collection<E> c, int n) {
Expand All @@ -415,14 +392,11 @@ public static <E> Collection<E> sampleWithReplacement(Collection<E> c, int n) {


/** /**
* Samples with replacement from a collection, using your own {@link Random} * Samples with replacement from a collection, using your own {@link Random}
* number generator * number generator.
* *
* @param c * @param c The collection to be sampled from
* The collection to be sampled from * @param n The number of samples to take
* @param n * @param r The random number generator
* The number of samples to take
* @param r
* the random number generator
* @return a new collection with the sample * @return a new collection with the sample
*/ */
public static <E> Collection<E> sampleWithReplacement(Collection<E> c, int n, Random r) { public static <E> Collection<E> sampleWithReplacement(Collection<E> c, int n, Random r) {
Expand All @@ -441,7 +415,7 @@ public static <E> Collection<E> sampleWithReplacement(Collection<E> c, int n, Ra


/** /**
* Returns true iff l1 is a sublist of l (i.e., every member of l1 is in l, * Returns true iff l1 is a sublist of l (i.e., every member of l1 is in l,
* and for every e1 < e2 in l1, there is an e1 < e2 occurrence in l). * and for every e1 &lt; e2 in l1, there is an e1 &lt; e2 occurrence in l).
*/ */
public static <T> boolean isSubList(List<T> l1, List<? super T> l) { public static <T> boolean isSubList(List<T> l1, List<? super T> l) {
Iterator<? super T> it = l.iterator(); Iterator<? super T> it = l.iterator();
Expand Down Expand Up @@ -496,7 +470,7 @@ public static <T extends Comparable<T>> int compareLists(List<T> list1, List<T>
} }


public static <C extends Comparable<C>> Comparator<List<C>> getListComparator() { public static <C extends Comparable<C>> Comparator<List<C>> getListComparator() {
return (list1, list2) -> compareLists(list1, list2); return CollectionUtils::compareLists;
} }


/** /**
Expand Down Expand Up @@ -554,12 +528,9 @@ public static <T> Set<T> toSet(Iterable<T> items) {
/** /**
* Add all the items from an iterable to a collection. * Add all the items from an iterable to a collection.
* *
* @param <T> * @param <T> The type of items in the iterable and the collection
* The type of items in the iterable and the collection * @param collection The collection to which the items should be added.
* @param collection * @param items The items to add to the collection.
* The collection to which the items should be added.
* @param items
* The items to add to the collection.
*/ */
public static <T> void addAll(Collection<T> collection, Iterable<? extends T> items) { public static <T> void addAll(Collection<T> collection, Iterable<? extends T> items) {
for (T item : items) { for (T item : items) {
Expand All @@ -583,14 +554,10 @@ public static <T> void addAll(Collection<T> collection, Iterable<? extends T> it
* [[a], [a, b], [b], [b, c], [c], [c, d], [d]] * [[a], [a, b], [b], [b, c], [c], [c, d], [d]]
* </pre> * </pre>
* *
* @param <T> * @param <T> The type of items contained in the list.
* The type of items contained in the list. * @param items The list of items.
* @param items * @param minSize The minimum size of an ngram.
* The list of items. * @param maxSize The maximum size of an ngram.
* @param minSize
* The minimum size of an ngram.
* @param maxSize
* The maximum size of an ngram.
* @return All sub-lists of the given sizes. * @return All sub-lists of the given sizes.
*/ */
public static <T> List<List<T>> getNGrams(List<T> items, int minSize, int maxSize) { public static <T> List<List<T>> getNGrams(List<T> items, int minSize, int maxSize) {
Expand Down Expand Up @@ -640,21 +607,14 @@ public static <T> List<List<T>> getNGrams(List<T> items, int minSize, int maxSiz
* [[a], [a], [a, a], [a, null], [a, null], [a, null, a, null]] * [[a], [a], [a, a], [a, null], [a, null], [a, null, a, null]]
* </pre> * </pre>
* *
* @param <T> * @param <T> The type of items contained in the list.
* The type of items contained in the list. * @param items The list of items.
* @param items * @param minSize The minimum length of a prefix/suffix span (should be at least 1)
* The list of items. * @param maxSize The maximum length of a prefix/suffix span
* @param minSize * @param paddingSymbol Symbol to be included if we run out of bounds (e.g. if items has
* The minimum length of a prefix/suffix span (should be at least 1)
* @param maxSize
* The maximum length of a prefix/suffix span
* @param paddingSymbol
* Symbol to be included if we run out of bounds (e.g. if items has
* size 3 and we try to extract a span of length 4). * size 3 and we try to extract a span of length 4).
* @param includePrefixes * @param includePrefixes Whether to extract prefixes
* whether to extract prefixes * @param includeSuffixes Whether to extract suffixes
* @param includeSuffixes
* whether to extract suffixes
* @return All prefix/suffix combinations of the given sizes. * @return All prefix/suffix combinations of the given sizes.
*/ */
public static <T> List<List<T>> getPrefixesAndSuffixes(List<T> items, int minSize, int maxSize, T paddingSymbol, boolean includePrefixes, boolean includeSuffixes) { public static <T> List<List<T>> getPrefixesAndSuffixes(List<T> items, int minSize, int maxSize, T paddingSymbol, boolean includePrefixes, boolean includeSuffixes) {
Expand Down Expand Up @@ -747,7 +707,7 @@ public static <T> List<T> mergeListWithSortedMatchedPreAggregated(List<? extends
} }


/** /**
* combines all the lists in a collection to a single list * Combines all the lists in a collection to a single list.
*/ */
public static <T> List<T> flatten(Collection<List<T>> nestedList) { public static <T> List<T> flatten(Collection<List<T>> nestedList) {
List<T> result = new ArrayList<T>(); List<T> result = new ArrayList<T>();
Expand Down Expand Up @@ -829,7 +789,7 @@ public static <T> Collection<Pair<Collection<T>, Collection<T>>> trainTestFoldsF
} }
} }


trainTestPairs.add(new Pair<Collection<T>, Collection<T>>(train, test)); trainTestPairs.add(new Pair<>(train, test));
} }


return trainTestPairs; return trainTestPairs;
Expand Down Expand Up @@ -858,7 +818,7 @@ public static <T> T mode(Collection<T> values) {




/** /**
* Transforms the keyset of collection according to the given Function and returns a set of the keys * Transforms the keyset of collection according to the given Function and returns a set of the keys.
* *
*/ */
public static<T1, T2> Set<T2> transformAsSet(Collection<? extends T1> original, Function<T1, ? extends T2> f){ public static<T1, T2> Set<T2> transformAsSet(Collection<? extends T1> original, Function<T1, ? extends T2> f){
Expand All @@ -871,7 +831,7 @@ public static<T1, T2> Set<T2> transformAsSet(Collection<? extends T1> original,




/** /**
* Transforms the keyset of collection according to the given Function and returns a list * Transforms the keyset of collection according to the given Function and returns a list.
* *
*/ */
public static<T1, T2> List<T2> transformAsList(Collection<? extends T1> original, Function<T1, ? extends T2> f){ public static<T1, T2> List<T2> transformAsList(Collection<? extends T1> original, Function<T1, ? extends T2> f){
Expand All @@ -883,7 +843,7 @@ public static<T1, T2> List<T2> transformAsList(Collection<? extends T1> original
} }


/** /**
* Filters the objects in the collection according to the given Filter and returns a list * Filters the objects in the collection according to the given Filter and returns a list.
* *
*/ */
public static<T> List<T> filterAsList(Collection<? extends T> original, Predicate<? super T> f){ public static<T> List<T> filterAsList(Collection<? extends T> original, Predicate<? super T> f){
Expand Down Expand Up @@ -915,17 +875,17 @@ public static<T,V> List<V> getAll(Map<T, V> map, Collection<T> indices) {
public static<T extends Comparable<? super T>> int maxIndex(List<T> list){ public static<T extends Comparable<? super T>> int maxIndex(List<T> list){
T max = null; T max = null;
int i = 0; int i = 0;
int maxindex = -1; int maxIndex = -1;
for(T t: list) for(T t: list)
{ {
if(max == null || t.compareTo(max) > 0) if(max == null || t.compareTo(max) > 0)
{ {
max = t; max = t;
maxindex = i; maxIndex = i;
} }
i++; i++;
} }
return maxindex; return maxIndex;
} }




Expand All @@ -937,6 +897,7 @@ public static<T extends Comparable<? super T>> int maxIndex(List<T> list){
* @param <E> The type of the iterators. * @param <E> The type of the iterators.
* @return An iterator consisting of all the component iterators concatenated together in order. * @return An iterator consisting of all the component iterators concatenated together in order.
*/ */
@SafeVarargs
public static <E> Iterator<E> concatIterators(final Iterator<E>... iterators) { public static <E> Iterator<E> concatIterators(final Iterator<E>... iterators) {
return new Iterator<E>() { return new Iterator<E>() {
Iterator<E> lastIter = null; Iterator<E> lastIter = null;
Expand Down Expand Up @@ -966,4 +927,5 @@ public void remove() {
} }
}; };
} }

} }

0 comments on commit 0f932ac

Please sign in to comment.