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   * 
22   * @see <a href="http://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a>
23   */
24  public final class Strings {
25  
26      /**
27       * The empty string.
28       */
29      public static final String EMPTY = "";
30  
31      /**
32       * Returns a double quoted string.
33       * 
34       * @param str
35       *        a String
36       * @return {@code "str"}
37       */
38      public static String dquote(final String str) {
39          return Chars.DQUOTE + str + Chars.DQUOTE;
40      }
41  
42      /**
43       * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using
44       * {@link String#trim()} is empty.
45       *
46       * @param s
47       *        the String to check, may be {@code null}
48       * @return {@code true} if the String is {@code null}, empty, or trims to empty.
49       */
50      public static boolean isBlank(final String s) {
51          return s == null || s.trim().isEmpty();
52      }
53  
54      /**
55       * <p>
56       * Checks if a CharSequence is empty ("") or null.
57       * </p>
58       *
59       * <pre>
60       * Strings.isEmpty(null)      = true
61       * Strings.isEmpty("")        = true
62       * Strings.isEmpty(" ")       = false
63       * Strings.isEmpty("bob")     = false
64       * Strings.isEmpty("  bob  ") = false
65       * </pre>
66       *
67       * <p>
68       * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is
69       * available in isBlank().
70       * </p>
71       *
72       * <p>
73       * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)
74       * </p>
75       *
76       * @param cs
77       *        the CharSequence to check, may be null
78       * @return {@code true} if the CharSequence is empty or null
79       */
80      public static boolean isEmpty(final CharSequence cs) {
81          return cs == null || cs.length() == 0;
82      }
83  
84      /**
85       * Checks if a String is not blank. The opposite of {@link #isBlank(String)}.
86       *
87       * @param s
88       *        the String to check, may be {@code null}
89       * @return {@code true} if the String is non-{@code null} and has content after being trimmed.
90       */
91      public static boolean isNotBlank(final String s) {
92          return !isBlank(s);
93      }
94  
95      /**
96       * <p>
97       * Checks if a CharSequence is not empty ("") and not null.
98       * </p>
99       *
100      * <pre>
101      * Strings.isNotEmpty(null)      = false
102      * Strings.isNotEmpty("")        = false
103      * Strings.isNotEmpty(" ")       = true
104      * Strings.isNotEmpty("bob")     = true
105      * Strings.isNotEmpty("  bob  ") = true
106      * </pre>
107      *
108      * <p>
109      * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)
110      * </p>
111      *
112      * @param cs
113      *        the CharSequence to check, may be null
114      * @return {@code true} if the CharSequence is not empty and not null
115      */
116     public static boolean isNotEmpty(final CharSequence cs) {
117         return !isEmpty(cs);
118     }
119 
120     /**
121      * Returns a quoted string.
122      * 
123      * @param str
124      *        a String
125      * @return {@code 'str'}
126      */
127     public static String quote(final String str) {
128         return Chars.QUOTE + str + Chars.QUOTE;
129     }
130 
131     /**
132      * <p>
133      * Removes control characters (char &lt;= 32) from both ends of this String returning {@code null} if the String is
134      * empty ("") after the trim or if it is {@code null}.
135      *
136      * <p>
137      * The String is trimmed using {@link String#trim()}. Trim removes start and end characters &lt;= 32.
138      * </p>
139      *
140      * <pre>
141      * Strings.trimToNull(null)          = null
142      * Strings.trimToNull("")            = null
143      * Strings.trimToNull("     ")       = null
144      * Strings.trimToNull("abc")         = "abc"
145      * Strings.trimToNull("    abc    ") = "abc"
146      * </pre>
147      *
148      * <p>
149      * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String)
150      * </p>
151      *
152      * @param str
153      *        the String to be trimmed, may be null
154      * @return the trimmed String, {@code null} if only chars &lt;= 32, empty or null String input
155      */
156     public static String trimToNull(final String str) {
157         final String ts = str == null ? null : str.trim();
158         return isEmpty(ts) ? null : ts;
159     }
160 
161     private Strings() {
162         // empty
163     }
164 }