1 /* 2 * FCKeditor - The text editor for Internet - http://www.fckeditor.net 3 * Copyright (C) 2004-2009 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 3595 2009-06-01 20:02:02Z mosipov $ 31 */ 32 public class Utils { 33 34 /** 35 * Constructs a set of lower-cased strings from a delimiter-separated 36 * string. 37 * 38 * @param stringList 39 * strings separated with a delimiter 40 * @param delimiter 41 * separating delimiter 42 * @return a lower-cased set, empty set if stringList is empty 43 * @throws IllegalArgumentException 44 * if <code>delimiter</code> is empty 45 */ 46 public static Set<String> getSet(final String stringList, 47 final String delimiter) { 48 if (isEmpty(delimiter)) 49 throw new IllegalArgumentException( 50 "Argument 'delimiter' shouldn't be empty!"); 51 if (isEmpty(stringList)) 52 return new HashSet<String>(); 53 54 Set<String> set = new HashSet<String>(); 55 StringTokenizer st = new StringTokenizer(stringList, delimiter); 56 while (st.hasMoreTokens()) { 57 String tmp = st.nextToken(); 58 if (isNotEmpty(tmp)) // simple empty filter 59 set.add(tmp.toLowerCase()); 60 } 61 return set; 62 } 63 64 /** 65 * Constructs a set of lower-cased strings from a <code>|</code> (pipe) 66 * delimited string. 67 * 68 * @see #getSet(String, String) 69 * @param stringList 70 * strings separated with a delimiter 71 * @return a lower-cased set, empty set if stringList is empty 72 * @throws IllegalArgumentException 73 * if <code>delimiter</code> is empty 74 */ 75 public static Set<String> getSet(final String stringList) { 76 return getSet(stringList, "|"); 77 } 78 79 /** 80 * Checks if a string is empty ("") or null. 81 * 82 * @param str 83 * string to check, may be null 84 * @return <code>true</code> if the string is <code>null</code> or empty, 85 * else <code>false</code> 86 */ 87 public static boolean isEmpty(final String str) { 88 return str == null || str.length() == 0; 89 } 90 91 /** 92 * Checks if a string is not empty ("") and not null. 93 * 94 * @param str 95 * string to check, may be null 96 * @return <code>true</code> if the string is not empty and not 97 * <code>null</code>, else <code>false</code> 98 */ 99 public static boolean isNotEmpty(final String str) { 100 return !isEmpty(str); 101 } 102 103 /** 104 * Checks if a string is whitespace, empty ("") or null. Whitespace is 105 * checked by {@link Character#isWhitespace(char)}. 106 * 107 * @param str 108 * string to check, may be null 109 * @return <code>true</code> if the string is <code>null</code>, empty or 110 * whitespace 111 */ 112 public static boolean isBlank(final String str) { 113 114 if (isEmpty(str)) 115 return true; 116 117 for (char c : str.toCharArray()) { 118 if (!Character.isWhitespace(c)) 119 return false; 120 } 121 122 return true; 123 } 124 125 /** 126 * Checks if a string is not empty (""), not null and not whitespace. 127 * 128 * @param str 129 * string to check, may be null 130 * @return <code>true</code> if the string is not <code>null</code>, not 131 * empty and not whitespace. 132 */ 133 public static boolean isNotBlank(final String str) { 134 return !isBlank(str); 135 } 136 }