View Javadoc
1   /*
2   * Licensed to the Apache Software Foundation (ASF) under one or more
3   * contributor license agreements. See the NOTICE file distributed with
4   * this work for additional information regarding copyright ownership.
5   * The ASF licenses this file to You under the Apache license, Version 2.0
6   * (the "License"); you may not use this file except in compliance with
7   * the License. You may obtain a copy of the License at
8   *
9   *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the license for the specific language governing permissions and
15  * limitations under the license.
16  */
17  package org.apache.logging.log4j.util;
18  
19  /**
20   * <em>Consider this class private.</em>
21   * @see <a href="http://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>
22   */
23  public final class Strings {
24  
25      /**
26       * The empty string.
27       */
28      public static final String EMPTY = "";
29  
30      private Strings() {
31      }
32  
33      /**
34       * <p>Checks if a CharSequence is empty ("") or null.</p>
35       *
36       * <pre>
37       * Strings.isEmpty(null)      = true
38       * Strings.isEmpty("")        = true
39       * Strings.isEmpty(" ")       = false
40       * Strings.isEmpty("bob")     = false
41       * Strings.isEmpty("  bob  ") = false
42       * </pre>
43       *
44       * <p>NOTE: This method changed in Lang version 2.0.
45       * It no longer trims the CharSequence.
46       * That functionality is available in isBlank().</p>
47       *
48       * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)</p>
49       *
50       * @param cs  the CharSequence to check, may be null
51       * @return {@code true} if the CharSequence is empty or null
52       */
53      public static boolean isEmpty(final CharSequence cs) {
54          return cs == null || cs.length() == 0;
55      }
56  
57      /**
58       * <p>Checks if a CharSequence is not empty ("") and not null.</p>
59       *
60       * <pre>
61       * Strings.isNotEmpty(null)      = false
62       * Strings.isNotEmpty("")        = false
63       * Strings.isNotEmpty(" ")       = true
64       * Strings.isNotEmpty("bob")     = true
65       * Strings.isNotEmpty("  bob  ") = true
66       * </pre>
67       *
68       * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)</p>
69       *
70       * @param cs  the CharSequence to check, may be null
71       * @return {@code true} if the CharSequence is not empty and not null
72       */
73      public static boolean isNotEmpty(final CharSequence cs) {
74          return !isEmpty(cs);
75      }
76  
77      /**
78       * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using
79       * {@link String#trim()} is empty.
80       *
81       * @param s the String to check, may be {@code null}
82       * @return {@code true} if the String is {@code null}, empty, or trims to empty.
83       */
84      public static boolean isBlank(final String s) {
85          return s == null || s.trim().isEmpty();
86      }
87  
88      /**
89       * Checks if a String is not blank. The opposite of {@link #isBlank(String)}.
90       *
91       * @param s the String to check, may be {@code null}
92       * @return {@code true} if the String is non-{@code null} and has content after being trimmed.
93       */
94      public static boolean isNotBlank(final String s) {
95          return !isBlank(s);
96      }
97  
98      /**
99       * <p>Removes control characters (char &lt;= 32) from both
100      * ends of this String returning {@code null} if the String is
101      * empty ("") after the trim or if it is {@code null}.
102      *
103      * <p>The String is trimmed using {@link String#trim()}.
104      * Trim removes start and end characters &lt;= 32.</p>
105      *
106      * <pre>
107      * Strings.trimToNull(null)          = null
108      * Strings.trimToNull("")            = null
109      * Strings.trimToNull("     ")       = null
110      * Strings.trimToNull("abc")         = "abc"
111      * Strings.trimToNull("    abc    ") = "abc"
112      * </pre>
113      *
114      * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String)</p>
115      *
116      * @param str  the String to be trimmed, may be null
117      * @return the trimmed String,
118      *  {@code null} if only chars &lt;= 32, empty or null String input
119      */
120     public static String trimToNull(final String str) {
121         final String ts = str == null ? null : str.trim();
122         return isEmpty(ts) ? null : ts;
123     }
124 
125 }