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 public final class Strings {
23
24 /**
25 * The empty string.
26 */
27 public static final String EMPTY = "";
28
29 private Strings() {
30 }
31
32 /**
33 * <p>Checks if a CharSequence is empty ("") or null.</p>
34 *
35 * <pre>
36 * Strings.isEmpty(null) = true
37 * Strings.isEmpty("") = true
38 * Strings.isEmpty(" ") = false
39 * Strings.isEmpty("bob") = false
40 * Strings.isEmpty(" bob ") = false
41 * </pre>
42 *
43 * <p>NOTE: This method changed in Lang version 2.0.
44 * It no longer trims the CharSequence.
45 * That functionality is available in isBlank().</p>
46 *
47 * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)</p>
48 *
49 * @param cs the CharSequence to check, may be null
50 * @return {@code true} if the CharSequence is empty or null
51 */
52 public static boolean isEmpty(final CharSequence cs) {
53 return cs == null || cs.length() == 0;
54 }
55
56 /**
57 * <p>Checks if a CharSequence is not empty ("") and not null.</p>
58 *
59 * <pre>
60 * Strings.isNotEmpty(null) = false
61 * Strings.isNotEmpty("") = false
62 * Strings.isNotEmpty(" ") = true
63 * Strings.isNotEmpty("bob") = true
64 * Strings.isNotEmpty(" bob ") = true
65 * </pre>
66 *
67 * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)</p>
68 *
69 * @param cs the CharSequence to check, may be null
70 * @return {@code true} if the CharSequence is not empty and not null
71 */
72 public static boolean isNotEmpty(final CharSequence cs) {
73 return !isEmpty(cs);
74 }
75
76 /**
77 * <p>Removes control characters (char <= 32) from both
78 * ends of this String returning {@code null} if the String is
79 * empty ("") after the trim or if it is {@code null}.
80 *
81 * <p>The String is trimmed using {@link String#trim()}.
82 * Trim removes start and end characters <= 32.</p>
83 *
84 * <pre>
85 * Strings.trimToNull(null) = null
86 * Strings.trimToNull("") = null
87 * Strings.trimToNull(" ") = null
88 * Strings.trimToNull("abc") = "abc"
89 * Strings.trimToNull(" abc ") = "abc"
90 * </pre>
91 *
92 * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.trimToNull(String)</p>
93 *
94 * @param str the String to be trimmed, may be null
95 * @return the trimmed String,
96 * {@code null} if only chars <= 32, empty or null String input
97 */
98 public static String trimToNull(final String str) {
99 final String ts = str == null ? null : str.trim();
100 return isEmpty(ts) ? null : ts;
101 }
102
103 }