All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.pipelite.expression.support.StringUtils Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (C) 2023-2024 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package io.pipelite.expression.support;

import io.pipelite.expression.core.el.Constants;

public class StringUtils {

    // ---------------------------------------------------------------------
    // General convenience methods for working with Strings
    // ---------------------------------------------------------------------
    private StringUtils() {
        super();
    }

    /**
     * Check whether the given {@code String} is empty.
     * 

* This method accepts any Object as an argument, comparing it to {@code null} and the empty String. As a * consequence, this method will never return {@code true} for a non-null non-String object. *

* The Object signature is useful for general attribute handling code that commonly deals with Strings but generally * has to iterate over Objects since attributes may e.g. be primitive value objects as well. * * @param str the candidate String */ public static boolean isEmpty(Object str) { return (str == null || "".equals(str)); } /** * Check that the given {@code CharSequence} is neither {@code null} nor of length 0. *

* Note: this method returns {@code true} for a {@code CharSequence} that purely consists of whitespace. *

* *

     * StringUtils.hasLength(null) = false
     * StringUtils.hasLength("") = false
     * StringUtils.hasLength(" ") = true
     * StringUtils.hasLength("Hello") = true
     * 
* * @param str the {@code CharSequence} to check (may be {@code null}) * @return {@code true} if the {@code CharSequence} is not {@code null} and has length * @see #hasText(String) */ public static boolean hasLength(CharSequence str) { return (str != null && str.length() > 0); } /** * Check that the given {@code String} is neither {@code null} nor of length 0. *

* Note: this method returns {@code true} for a {@code String} that purely consists of whitespace. * * @param str the {@code String} to check (may be {@code null}) * @return {@code true} if the {@code String} is not {@code null} and has length * @see #hasLength(CharSequence) * @see #hasText(String) */ public static boolean hasLength(String str) { return hasLength((CharSequence) str); } /** * Check whether the given {@code CharSequence} contains actual text. *

* More specifically, this method returns {@code true} if the {@code CharSequence} is not {@code null}, its length * is greater than 0, and it contains at least one non-whitespace character. *

* *

     * StringUtils.hasText(null) = false
     * StringUtils.hasText("") = false
     * StringUtils.hasText(" ") = false
     * StringUtils.hasText("12345") = true
     * StringUtils.hasText(" 12345 ") = true
     * 
* * @param str the {@code CharSequence} to check (may be {@code null}) * @return {@code true} if the {@code CharSequence} is not {@code null}, its length is greater than 0, and it does * not contain whitespace only * @see Character#isWhitespace */ public static boolean hasText(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; } /** * Check whether the given {@code String} contains actual text. *

* More specifically, this method returns {@code true} if the {@code String} is not {@code null}, its length is * greater than 0, and it contains at least one non-whitespace character. * * @param str the {@code String} to check (may be {@code null}) * @return {@code true} if the {@code String} is not {@code null}, its length is greater than 0, and it does not * contain whitespace only * @see #hasText(CharSequence) */ public static boolean hasText(String str) { return hasText((CharSequence) str); } /** * Check whether the given {@code CharSequence} contains any whitespace characters. * * @param str the {@code CharSequence} to check (may be {@code null}) * @return {@code true} if the {@code CharSequence} is not empty and contains at least 1 whitespace character * @see 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; } /** * Check whether the given {@code String} contains any whitespace characters. * * @param str the {@code String} to check (may be {@code null}) * @return {@code true} if the {@code String} is not empty and contains at least 1 whitespace character * @see #containsWhitespace(CharSequence) */ public static boolean containsWhitespace(String str) { return containsWhitespace((CharSequence) str); } /** * Trim leading and trailing whitespace from the given {@code String}. * * @param str the {@code String} to check * @return the trimmed {@code String} * @see 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(); } /** * Trim all whitespace from the given {@code String}: leading, trailing, and in between characters. * * @param str the {@code String} to check * @return the trimmed {@code String} * @see Character#isWhitespace */ public static String trimAllWhitespace(String str) { if (!hasLength(str)) { return str; } int len = str.length(); StringBuilder sb = new StringBuilder(str.length()); for (int i = 0; i < len; i++) { char c = str.charAt(i); if (!Character.isWhitespace(c)) { sb.append(c); } } return sb.toString(); } /** * Is the string a number? * * @param st The string. * @return true, if the input string is a number. */ public static boolean isNumber(String st) { if (st.charAt(0) == Constants.MINUS_SIGN && st.length() == 1) return false; if (st.charAt(0) == '+' && st.length() == 1) return false; if (st.charAt(0) == 'e' || st.charAt(0) == 'E') return false; for (char ch : st.toCharArray()) { if (!Character.isDigit(ch) && ch != Constants.MINUS_SIGN && ch != Constants.DECIMAL_SEPARATOR && ch != 'e' && ch != 'E' && ch != '+') return false; } return true; } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy