View Javadoc

1   /*
2    * FCKeditor - The text editor for Internet - http://www.fckeditor.net
3    * Copyright (C) 2003-2008 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   * Some static helper methods.
29   * 
30   * @version $Id: Utils.java 2538 2008-10-10 18:27:19Z mosipov $
31   */
32  public class Utils {
33  
34  	/**
35  	 * Constructs a set of uppercased strings from a delimiter-separated string.
36  	 * 
37  	 * @param stringList
38  	 * @param delimiter
39  	 *            The delimiter.
40  	 * @return An emtpy list if 'stringList' is empty else a lowercased set of
41  	 *         strings.
42  	 * @throws IllegalArgumentException
43  	 *             if 'delimiter' is empty.
44  	 */
45  	public static Set<String> getSet(final String stringList,
46  			final String delimiter) {
47  		if (isEmpty(delimiter))
48  			throw new IllegalArgumentException(
49  					"Argument 'delimiter' shouldn't be empty!");
50  		if (isEmpty(stringList))
51  			return new HashSet<String>();
52  
53  		Set<String> set = new HashSet<String>();
54  		StringTokenizer st = new StringTokenizer(stringList, delimiter);
55  		while (st.hasMoreTokens()) {
56  			String tmp = st.nextToken();
57  			if (isNotEmpty(tmp)) // simple empty filter
58  				set.add(tmp.toLowerCase());
59  		}
60  		return set;
61  	}
62  
63  	/**
64  	 * Just a wrapper to {@link #getSet(String, String)} for using '&#124;' as
65  	 * delimiter.
66  	 */
67  	public static Set<String> getSet(final String stringList) {
68  		return getSet(stringList, "|");
69  	}
70  
71  	/**
72  	 * Checks if a string is <code>null</code> or empty.
73  	 * 
74  	 * @param str
75  	 *            to check
76  	 * @return <code>true</code> if the string is <code>null</code> or
77  	 *         empty.
78  	 */
79  	public static boolean isEmpty(final String str) {
80  		return str == null || str.length() == 0;
81  	}
82  
83  	/**
84  	 * Just a wrapper to {@link #isEmpty(String)}.
85  	 * 
86  	 * @param str
87  	 *            to ckeck
88  	 * @return <code>true</code> if the String is not empty or not
89  	 *         <code>null</code>.
90  	 */
91  	public static boolean isNotEmpty(final String str) {
92  		return !isEmpty(str);
93  	}
94  
95  	/**
96  	 * Checks if a String is whitespace, empty or null.
97  	 * 
98  	 * @param str
99  	 *            to check
100 	 * @return <code>true</code> if the string is <code>null</code>, empty
101 	 *         or contains whitespace only.
102 	 */
103 	public static boolean isBlank(final String str) {
104 
105 		if (isEmpty(str))
106 			return true;
107 
108 		for (char c : str.toCharArray()) {
109 			if (!Character.isWhitespace(c))
110 				return false;
111 		}
112 
113 		return true;
114 	}
115 
116 	/**
117 	 * Just a wrapper to {@link #isBlank(String)}.
118 	 * 
119 	 * @param str
120 	 *            to check
121 	 * @return <code>true</code> if the string is not <code>null</code>,
122 	 *         not empty or does not contain whitespace only.
123 	 */
124 	public static boolean isNotBlank(final String str) {
125 		return !isBlank(str);
126 	}
127 }