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      private Strings() {
32      }
33  
34      /**
35       * <p>
36       * Checks if a CharSequence is empty ("") or null.
37       * </p>
38       *
39       * <pre>
40       * Strings.isEmpty(null)      = true
41       * Strings.isEmpty("")        = true
42       * Strings.isEmpty(" ")       = false
43       * Strings.isEmpty("bob")     = false
44       * Strings.isEmpty("  bob  ") = false
45       * </pre>
46       *
47       * <p>
48       * NOTE: This method changed in Lang version 2.0. It no longer trims the CharSequence. That functionality is
49       * available in isBlank().
50       * </p>
51       *
52       * <p>
53       * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)
54       * </p>
55       *
56       * @param cs
57       *        the CharSequence to check, may be null
58       * @return {@code true} if the CharSequence is empty or null
59       */
60      public static boolean isEmpty(final CharSequence cs) {
61          return cs == null || cs.length() == 0;
62      }
63  
64      /**
65       * <p>
66       * Checks if a CharSequence is not empty ("") and not null.
67       * </p>
68       *
69       * <pre>
70       * Strings.isNotEmpty(null)      = false
71       * Strings.isNotEmpty("")        = false
72       * Strings.isNotEmpty(" ")       = true
73       * Strings.isNotEmpty("bob")     = true
74       * Strings.isNotEmpty("  bob  ") = true
75       * </pre>
76       *
77       * <p>
78       * Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)
79       * </p>
80       *
81       * @param cs
82       *        the CharSequence to check, may be null
83       * @return {@code true} if the CharSequence is not empty and not null
84       */
85      public static boolean isNotEmpty(final CharSequence cs) {
86          return !isEmpty(cs);
87      }
88  
89      /**
90       * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using
91       * {@link String#trim()} is empty.
92       *
93       * @param s
94       *        the String to check, may be {@code null}
95       * @return {@code true} if the String is {@code null}, empty, or trims to empty.
96       */
97      public static boolean isBlank(final String s) {
98          return s == null || s.trim().isEmpty();
99      }
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 }