001 /* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache license, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the license for the specific language governing permissions and 015 * limitations under the license. 016 */ 017 package org.apache.logging.log4j.util; 018 019 /** 020 * <em>Consider this class private.</em> 021 * @see <a href="http://commons.apache.org/proper/commons-lang/">Apache Commons Lang</a> 022 */ 023 public final class Strings { 024 025 /** 026 * The empty string. 027 */ 028 public static final String EMPTY = ""; 029 030 private Strings() { 031 } 032 033 /** 034 * <p>Checks if a CharSequence is empty ("") or null.</p> 035 * 036 * <pre> 037 * Strings.isEmpty(null) = true 038 * Strings.isEmpty("") = true 039 * Strings.isEmpty(" ") = false 040 * Strings.isEmpty("bob") = false 041 * Strings.isEmpty(" bob ") = false 042 * </pre> 043 * 044 * <p>NOTE: This method changed in Lang version 2.0. 045 * It no longer trims the CharSequence. 046 * That functionality is available in isBlank().</p> 047 * 048 * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isEmpty(CharSequence)</p> 049 * 050 * @param cs the CharSequence to check, may be null 051 * @return {@code true} if the CharSequence is empty or null 052 */ 053 public static boolean isEmpty(final CharSequence cs) { 054 return cs == null || cs.length() == 0; 055 } 056 057 /** 058 * <p>Checks if a CharSequence is not empty ("") and not null.</p> 059 * 060 * <pre> 061 * Strings.isNotEmpty(null) = false 062 * Strings.isNotEmpty("") = false 063 * Strings.isNotEmpty(" ") = true 064 * Strings.isNotEmpty("bob") = true 065 * Strings.isNotEmpty(" bob ") = true 066 * </pre> 067 * 068 * <p>Copied from Apache Commons Lang org.apache.commons.lang3.StringUtils.isNotEmpty(CharSequence)</p> 069 * 070 * @param cs the CharSequence to check, may be null 071 * @return {@code true} if the CharSequence is not empty and not null 072 */ 073 public static boolean isNotEmpty(final CharSequence cs) { 074 return !isEmpty(cs); 075 } 076 077 /** 078 * Checks if a String is blank. A blank string is one that is {@code null}, empty, or when trimmed using 079 * {@link String#trim()} is empty. 080 * 081 * @param s the String to check, may be {@code null} 082 * @return {@code true} if the String is {@code null}, empty, or trims to empty. 083 */ 084 public static boolean isBlank(final String s) { 085 return s == null || s.trim().isEmpty(); 086 } 087 088 /** 089 * Checks if a String is not blank. The opposite of {@link #isBlank(String)}. 090 * 091 * @param s the String to check, may be {@code null} 092 * @return {@code true} if the String is non-{@code null} and has content after being trimmed. 093 */ 094 public static boolean isNotBlank(final String s) { 095 return !isBlank(s); 096 } 097 098 /** 099 * <p>Removes control characters (char <= 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 <= 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 <= 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 }