Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
StringUtil.java
1 package org.bukkit.util;
2 
3 import java.util.Collection;
4 import org.apache.commons.lang.Validate;
5 
6 public class StringUtil {
7 
8  /**
9  * Copies all elements from the iterable collection of originals to the
10  * collection provided.
11  *
12  * @param token String to search for
13  * @param originals An iterable collection of strings to filter.
14  * @param collection The collection to add matches to
15  * @return the collection provided that would have the elements copied
16  * into
17  * @throws UnsupportedOperationException if the collection is immutable
18  * and originals contains a string which starts with the specified
19  * search string.
20  * @throws IllegalArgumentException if any parameter is is null
21  * @throws IllegalArgumentException if originals contains a null element.
22  * <b>Note: the collection may be modified before this is thrown</b>
23  */
24  public static <T extends Collection<? super String>> T copyPartialMatches(final String token, final Iterable<String> originals, final T collection) throws UnsupportedOperationException, IllegalArgumentException {
25  Validate.notNull(token, "Search token cannot be null");
26  Validate.notNull(collection, "Collection cannot be null");
27  Validate.notNull(originals, "Originals cannot be null");
28 
29  for (String string : originals) {
30  if (startsWithIgnoreCase(string, token)) {
31  collection.add(string);
32  }
33  }
34 
35  return collection;
36  }
37 
38  /**
39  * This method uses a region to check case-insensitive equality. This
40  * means the internal array does not need to be copied like a
41  * toLowerCase() call would.
42  *
43  * @param string String to check
44  * @param prefix Prefix of string to compare
45  * @return true if provided string starts with, ignoring case, the prefix
46  * provided
47  * @throws NullPointerException if prefix is null
48  * @throws IllegalArgumentException if string is null
49  */
50  public static boolean startsWithIgnoreCase(final String string, final String prefix) throws IllegalArgumentException, NullPointerException {
51  Validate.notNull(string, "Cannot check a null string for a match");
52  if (string.length() < prefix.length()) {
53  return false;
54  }
55  return string.regionMatches(true, 0, prefix, 0, prefix.length());
56  }
57 }
static< TextendsCollection<?superString > T copyPartialMatches(final String token, final Iterable< String > originals, final T collection)
Definition: StringUtil.java:24
static boolean startsWithIgnoreCase(final String string, final String prefix)
Definition: StringUtil.java:50