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 */
017package org.apache.logging.log4j.util;
018
019/**
020 * <em>Consider this class private.</em>
021 * 
022 * @see <a href="http://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>
023 */
024public final class Strings {
025
026    /**
027     * The empty string.
028     */
029    public static final String EMPTY = "";
030
031    private Strings() {
032    }
033
034    /**
035     * <p>
036     * Checks if a CharSequence is empty ("") or null.
037     * </p>
038     *
039     * <pre>
040     * Strings.isEmpty(null)      = true
041     * Strings.isEmpty("")        = true
042     * Strings.isEmpty(" ")       = false
043     * Strings.isEmpty("bob")     = false
044     * Strings.isEmpty("  bob  ") = false
045     * </pre>
046     *
047     * <p>
048     * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is
049     * available in isBlank().
050     * </p>
051     *
052     * <p>
053     * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)
054     * </p>
055     *
056     * @param cs
057     *        the CharSequence to check, may be null
058     * @return {@code true} if the CharSequence is empty or null
059     */
060    public static boolean isEmpty(final CharSequence cs) {
061        return cs == null || cs.length() == 0;
062    }
063
064    /**
065     * <p>
066     * Checks if a CharSequence is not empty ("") and not null.
067     * </p>
068     *
069     * <pre>
070     * Strings.isNotEmpty(null)      = false
071     * Strings.isNotEmpty("")        = false
072     * Strings.isNotEmpty(" ")       = true
073     * Strings.isNotEmpty("bob")     = true
074     * Strings.isNotEmpty("  bob  ") = true
075     * </pre>
076     *
077     * <p>
078     * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)
079     * </p>
080     *
081     * @param cs
082     *        the CharSequence to check, may be null
083     * @return {@code true} if the CharSequence is not empty and not null
084     */
085    public static boolean isNotEmpty(final CharSequence cs) {
086        return !isEmpty(cs);
087    }
088
089    /**
090     * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using
091     * {@link String#trim()} is empty.
092     *
093     * @param s
094     *        the String to check, may be {@code null}
095     * @return {@code true} if the String is {@code null}, empty, or trims to empty.
096     */
097    public static boolean isBlank(final String s) {
098        return s == null || s.trim().isEmpty();
099    }
100
101    /**
102     * Checks if a String is not blank. The opposite of {@link #isBlank(String)}.
103     *
104     * @param s
105     *        the String to check, may be {@code null}
106     * @return {@code true} if the String is non-{@code null} and has content after being trimmed.
107     */
108    public static boolean isNotBlank(final String s) {
109        return !isBlank(s);
110    }
111
112    /**
113     * <p>
114     * Removes control characters (char &lt;= 32) from both ends of this String returning {@code null} if the String is
115     * empty ("") after the trim or if it is {@code null}.
116     *
117     * <p>
118     * The String is trimmed using {@link String#trim()}. Trim removes start and end characters &lt;= 32.
119     * </p>
120     *
121     * <pre>
122     * Strings.trimToNull(null)          = null
123     * Strings.trimToNull("")            = null
124     * Strings.trimToNull("     ")       = null
125     * Strings.trimToNull("abc")         = "abc"
126     * Strings.trimToNull("    abc    ") = "abc"
127     * </pre>
128     *
129     * <p>
130     * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String)
131     * </p>
132     *
133     * @param str
134     *        the String to be trimmed, may be null
135     * @return the trimmed String, {@code null} if only chars &lt;= 32, empty or null String input
136     */
137    public static String trimToNull(final String str) {
138        final String ts = str == null ? null : str.trim();
139        return isEmpty(ts) ? null : ts;
140    }
141
142    /**
143     * Returns a quoted string.
144     * 
145     * @param str
146     *        a String
147     * @return {@code 'str'}
148     */
149    public static String quote(final String str) {
150        return Chars.QUOTE + str + Chars.QUOTE;
151    }
152
153    /**
154     * Returns a double quoted string.
155     * 
156     * @param str
157     *        a String
158     * @return {@code "str"}
159     */
160    public static String dquote(final String str) {
161        return Chars.DQUOTE + str + Chars.DQUOTE;
162    }
163}