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