001    /*
002    * Licensed to the Apache Software Foundation (ASF) under one or more
003    * contributor license agreements. See the NOTICE file distributed with
004    * this work for additional information regarding copyright ownership.
005    * The ASF licenses this file to You under the Apache license, Version 2.0
006    * (the "License"); you may not use this file except in compliance with
007    * the License. You may obtain a copy of the License at
008    *
009    *      http://www.apache.org/licenses/LICENSE-2.0
010    *
011    * Unless required by applicable law or agreed to in writing, software
012    * distributed under the License is distributed on an "AS IS" BASIS,
013    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014    * See the license for the specific language governing permissions and
015    * limitations under the license.
016    */
017    package org.apache.logging.log4j.util;
018    
019    /**
020     * <em>Consider this class private.</em>
021     */
022    public final class Strings {
023    
024        /**
025         * The empty string.
026         */
027        public static final String EMPTY = "";
028    
029        private Strings() {
030        }
031    
032        /**
033         * <p>Checks if a CharSequence is empty ("") or null.</p>
034         *
035         * <pre>
036         * Strings.isEmpty(null)      = true
037         * Strings.isEmpty("")        = true
038         * Strings.isEmpty(" ")       = false
039         * Strings.isEmpty("bob")     = false
040         * Strings.isEmpty("  bob  ") = false
041         * </pre>
042         *
043         * <p>NOTE: This method changed in Lang version 2.0.
044         * It no longer trims the CharSequence.
045         * That functionality is available in isBlank().</p>
046         *
047         * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)</p>
048         *
049         * @param cs  the CharSequence to check, may be null
050         * @return {@code true} if the CharSequence is empty or null
051         */
052        public static boolean isEmpty(final CharSequence cs) {
053            return cs == null || cs.length() == 0;
054        }
055    
056        /**
057         * <p>Checks if a CharSequence is not empty ("") and not null.</p>
058         *
059         * <pre>
060         * Strings.isNotEmpty(null)      = false
061         * Strings.isNotEmpty("")        = false
062         * Strings.isNotEmpty(" ")       = true
063         * Strings.isNotEmpty("bob")     = true
064         * Strings.isNotEmpty("  bob  ") = true
065         * </pre>
066         *
067         * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)</p>
068         *
069         * @param cs  the CharSequence to check, may be null
070         * @return {@code true} if the CharSequence is not empty and not null
071         */
072        public static boolean isNotEmpty(final CharSequence cs) {
073            return !isEmpty(cs);
074        }
075    
076        /**
077         * <p>Removes control characters (char &lt;= 32) from both
078         * ends of this String returning {@code null} if the String is
079         * empty ("") after the trim or if it is {@code null}.
080         *
081         * <p>The String is trimmed using {@link String#trim()}.
082         * Trim removes start and end characters &lt;= 32.</p>
083         *
084         * <pre>
085         * Strings.trimToNull(null)          = null
086         * Strings.trimToNull("")            = null
087         * Strings.trimToNull("     ")       = null
088         * Strings.trimToNull("abc")         = "abc"
089         * Strings.trimToNull("    abc    ") = "abc"
090         * </pre>
091         *
092         * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String)</p>
093         *
094         * @param str  the String to be trimmed, may be null
095         * @return the trimmed String,
096         *  {@code null} if only chars &lt;= 32, empty or null String input
097         */
098        public static String trimToNull(final String str) {
099            final String ts = str == null ? null : str.trim();
100            return isEmpty(ts) ? null : ts;
101        }
102    
103    }