View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2004-2010 Frederico Caldeira Knabben
4    * 
5    * == BEGIN LICENSE ==
6    * 
7    * Licensed under the terms of any of the following licenses at your
8    * choice:
9    * 
10   *  - GNU General Public License Version 2 or later (the "GPL")
11   *    http://www.gnu.org/licenses/gpl.html
12   * 
13   *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
14   *    http://www.gnu.org/licenses/lgpl.html
15   * 
16   *  - Mozilla Public License Version 1.1 or later (the "MPL")
17   *    http://www.mozilla.org/MPL/MPL-1.1.html
18   * 
19   * == END LICENSE ==
20   */
21  package net.fckeditor.tool;
22  
23  import java.util.HashSet;
24  import java.util.Set;
25  import java.util.StringTokenizer;
26  
27  /**
28   * Static helper methods for strings.
29   * 
30   * @version $Id: Utils.java 4785 2009-12-21 20:10:28Z mosipov $
31   */
32  public class Utils {
33  
34  	/** The empty string {@code ""}. */
35  	public static final String EMPTY_STRING = "";
36  	
37  	/**
38  	 * Constructs a set of lower-cased strings from a delimiter-separated
39  	 * string.
40  	 * 
41  	 * @param stringList
42  	 *            strings separated with a delimiter
43  	 * @param delimiter
44  	 *            separating delimiter
45  	 * @return a lower-cased set, empty set if stringList is empty
46  	 * @throws IllegalArgumentException
47  	 *             if <code>delimiter</code> is empty
48  	 */
49  	public static Set<String> getSet(final String stringList,
50  			final String delimiter) {
51  		if (isEmpty(delimiter))
52  			throw new IllegalArgumentException(
53  					"Argument 'delimiter' shouldn't be empty!");
54  		if (isEmpty(stringList))
55  			return new HashSet<String>();
56  
57  		Set<String> set = new HashSet<String>();
58  		StringTokenizer st = new StringTokenizer(stringList, delimiter);
59  		while (st.hasMoreTokens()) {
60  			String tmp = st.nextToken();
61  			if (isNotEmpty(tmp)) // simple empty filter
62  				set.add(tmp.toLowerCase());
63  		}
64  		return set;
65  	}
66  
67  	/**
68  	 * Constructs a set of lower-cased strings from a <code>|</code> (pipe)
69  	 * delimited string.
70  	 * 
71  	 * @see #getSet(String, String)
72  	 * @param stringList
73  	 *            strings separated with a delimiter
74  	 * @return a lower-cased set, empty set if stringList is empty
75  	 * @throws IllegalArgumentException
76  	 *             if <code>delimiter</code> is empty
77  	 */
78  	public static Set<String> getSet(final String stringList) {
79  		return getSet(stringList, "|");
80  	}
81  
82  	/**
83  	 * Checks if a string is empty ("") or null.
84  	 * 
85  	 * @param str
86  	 *            string to check, may be null
87  	 * @return <code>true</code> if the string is <code>null</code> or empty,
88  	 *         else <code>false</code>
89  	 */
90  	public static boolean isEmpty(final String str) {
91  		return str == null || str.length() == 0;
92  	}
93  
94  	/**
95  	 * Checks if a string is not empty ("") and not null.
96  	 * 
97  	 * @param str
98  	 *            string to check, may be null
99  	 * @return <code>true</code> if the string is not empty and not
100 	 *         <code>null</code>, else <code>false</code>
101 	 */
102 	public static boolean isNotEmpty(final String str) {
103 		return !isEmpty(str);
104 	}
105 
106 	/**
107 	 * Checks if a string is whitespace, empty ("") or null. Whitespace is
108 	 * checked by {@link Character#isWhitespace(char)}.
109 	 * 
110 	 * @param str
111 	 *            string to check, may be null
112 	 * @return <code>true</code> if the string is <code>null</code>, empty or
113 	 *         whitespace
114 	 */
115 	public static boolean isBlank(final String str) {
116 
117 		if (isEmpty(str))
118 			return true;
119 
120 		for (char c : str.toCharArray()) {
121 			if (!Character.isWhitespace(c))
122 				return false;
123 		}
124 
125 		return true;
126 	}
127 
128 	/**
129 	 * Checks if a string is not empty (""), not null and not whitespace.
130 	 * 
131 	 * @param str
132 	 *            string to check, may be null
133 	 * @return <code>true</code> if the string is not <code>null</code>, not
134 	 *         empty and not whitespace.
135 	 */
136 	public static boolean isNotBlank(final String str) {
137 		return !isBlank(str);
138 	}
139 }