/** * Check that the given String is neither <code>null</code> nor of length 0. * Note: Will return <code>true</code> for a String that purely consists of whitespace. * @param str the String to check (may be <code>null</code>) * @return <code>true</code> if the String is not null and has length * @see #hasLength(CharSequence) */ public static boolean hasLength(String str) { return hasLength((CharSequence) str); }
/** * Assert that the given String is not empty; that is, * it must not be <code>null</code> and not the empty String. * <pre class="code">Assert.hasLength(name, "Name must not be empty");</pre> * @param text the String to check * @param message the exception message to use if the assertion fails * @see Strings#hasLength */ public static void hasLength(String text, String message) { if (!Strings.hasLength(text)) { throw new IllegalArgumentException(message); } }
/** * Assert that the given text does not contain the given substring. * <pre class="code">Assert.doesNotContain(name, "rod", "Name must not contain 'rod'");</pre> * @param textToSearch the text to search * @param substring the substring to find within the text * @param message the exception message to use if the assertion fails */ public static void doesNotContain(String textToSearch, String substring, String message) { if (Strings.hasLength(textToSearch) && Strings.hasLength(substring) && textToSearch.indexOf(substring) != -1) { throw new IllegalArgumentException(message); } }
/** * Delete any character in a given String. * @param inString the original String * @param charsToDelete a set of characters to delete. * E.g. "az\n" will delete 'a's, 'z's and new lines. * @return the resulting String */ public static String deleteAny(String inString, String charsToDelete) { if (!hasLength(inString) || !hasLength(charsToDelete)) { return inString; } StringBuilder sb = new StringBuilder(); for (int i = 0; i < inString.length(); i++) { char c = inString.charAt(i); if (charsToDelete.indexOf(c) == -1) { sb.append(c); } } return sb.toString(); }
/** * Check whether the given CharSequence contains any whitespace characters. * @param str the CharSequence to check (may be <code>null</code>) * @return <code>true</code> if the CharSequence is not empty and * contains at least 1 whitespace character * @see java.lang.Character#isWhitespace */ public static boolean containsWhitespace(CharSequence str) { if (!hasLength(str)) { return false; } int strLen = str.length(); for (int i = 0; i < strLen; i++) { if (Character.isWhitespace(str.charAt(i))) { return true; } } return false; }
/** * Trim all occurences of the supplied leading character from the given String. * @param str the String to check * @param leadingCharacter the leading character to be trimmed * @return the trimmed String */ public static String trimLeadingCharacter(String str, char leadingCharacter) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && sb.charAt(0) == leadingCharacter) { sb.deleteCharAt(0); } return sb.toString(); }
/** * Trim leading whitespace from the given String. * @param str the String to check * @return the trimmed String * @see java.lang.Character#isWhitespace */ public static String trimLeadingWhitespace(String str) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) { sb.deleteCharAt(0); } return sb.toString(); }
/** * Trim all occurences of the supplied trailing character from the given String. * @param str the String to check * @param trailingCharacter the trailing character to be trimmed * @return the trimmed String */ public static String trimTrailingCharacter(String str, char trailingCharacter) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && sb.charAt(sb.length() - 1) == trailingCharacter) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); }
/** * Trim trailing whitespace from the given String. * @param str the String to check * @return the trimmed String * @see java.lang.Character#isWhitespace */ public static String trimTrailingWhitespace(String str) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); }
/** * Trim leading and trailing whitespace from the given String. * @param str the String to check * @return the trimmed String * @see java.lang.Character#isWhitespace */ public static String trimWhitespace(String str) { if (!hasLength(str)) { return str; } StringBuilder sb = new StringBuilder(str); while (sb.length() > 0 && Character.isWhitespace(sb.charAt(0))) { sb.deleteCharAt(0); } while (sb.length() > 0 && Character.isWhitespace(sb.charAt(sb.length() - 1))) { sb.deleteCharAt(sb.length() - 1); } return sb.toString(); }
/** * Parse the given, comma-separated string into a list of {@code MediaType} objects. * <p>This method can be used to parse an Accept or Content-Type header. * @param mediaTypes the string to parse * @return the list of media types * @throws IllegalArgumentException if the string cannot be parsed */ public static List<MediaType> parseMediaTypes(String mediaTypes) { if (!Strings.hasLength(mediaTypes)) { return Collections.emptyList(); } String[] tokens = mediaTypes.split(",\\s*"); List<MediaType> result = new ArrayList<MediaType>(tokens.length); for (String token : tokens) { result.add(parseMediaType(token)); } return result; }
/** * Parse the given, comma-separated string into a list of {@code MediaType} objects. * <p>This method can be used to parse an Accept or Content-Type header. * @param mediaTypes the string to parse * @return the list of media types * @throws IllegalArgumentException if the string cannot be parsed */ public static List<MediaType> parseMediaTypes(String mediaTypes) { if (!Strings.hasLength(mediaTypes)) { return java.util.Collections.emptyList(); } String[] tokens = mediaTypes.split(",\\s*"); List<MediaType> result = new ArrayList<MediaType>(tokens.length); for (String token : tokens) { result.add(parseMediaType(token)); } return result; }
/** * Parse the given, comma-separated string into a list of {@code MimeType} objects. * * @param mimeTypes the string to parse * @return the list of mime types * @throws IllegalArgumentException if the string cannot be parsed */ public static List<MimeType> parseMimeTypes(String mimeTypes) { if (!Strings.hasLength(mimeTypes)) { return Collections.emptyList(); } String[] tokens = mimeTypes.split(",\\s*"); List<MimeType> result = new ArrayList<MimeType>(tokens.length); for (String token : tokens) { result.add(parseMimeType(token)); } return result; }
/** * Parse the given, comma-separated string into a list of {@code MediaType} objects. * <p>This method can be used to parse an Accept or Content-Type header. * @param mediaTypes the string to parse * @return the list of media types * @throws IllegalArgumentException if the string cannot be parsed */ public static List<MediaType> parseMediaTypes(String mediaTypes) { if (!Strings.hasLength(mediaTypes)) { return Collections.emptyList(); } String[] tokens = mediaTypes.split(",\\s*"); List<MediaType> result = new ArrayList<MediaType>(tokens.length); for (String token : tokens) { result.add(parseMediaType(token)); } return result; }
/** * Parse the given, comma-separated string into a list of {@code MimeType} objects. * * @param mimeTypes the string to parse * @return the list of mime types * @throws IllegalArgumentException if the string cannot be parsed */ public static List<MimeType> parseMimeTypes(String mimeTypes) { if (!Strings.hasLength(mimeTypes)) { return Collections.emptyList(); } String[] tokens = mimeTypes.split(",\\s*"); List<MimeType> result = new ArrayList<MimeType>(tokens.length); for (String token : tokens) { result.add(parseMimeType(token)); } return result; }
/** * Parse the given, comma-separated string into a list of {@code MediaType} objects. * <p>This method can be used to parse an Accept or Content-Type header. * @param mediaTypes the string to parse * @return the list of media types * @throws IllegalArgumentException if the string cannot be parsed */ public static List<MediaType> parseMediaTypes(String mediaTypes) { if (!Strings.hasLength(mediaTypes)) { return java.util.Collections.emptyList(); } String[] tokens = mediaTypes.split(",\\s*"); List<MediaType> result = new ArrayList<MediaType>(tokens.length); for (String token : tokens) { result.add(parseMediaType(token)); } return result; }
@Override public String getProviderId() { if (Strings.hasLength(getProviderType())) { return getProviderType().toLowerCase(); } return "default"; }
@Override public String getProviderId() { if (Strings.hasLength(getProviderType())) { return getProviderType().toLowerCase(); } return "default"; }
public static QueryString create(String query) { if (!Strings.hasLength(query)) { return null; } QueryString queryString = new QueryString(); // only returns null if string is null String[] tokens = Strings.tokenizeToStringArray(query, "&", false, false); for( String token : tokens) { applyKeyValuePair(queryString, token); } return queryString; }
public static QueryString create(String query) { if (!Strings.hasLength(query)) { return null; } QueryString queryString = new QueryString(); // only returns null if string is null String[] tokens = Strings.tokenizeToStringArray(query, "&", false, false); for( String token : tokens) { applyKeyValuePair(queryString, token); } return queryString; }