View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.hc.core5.util;
29  
30  import java.util.Locale;
31  
32  /**
33   * @since 4.3
34   */
35  public final class TextUtils {
36  
37      private TextUtils() {
38          // Do not allow utility class to be instantiated.
39      }
40  
41      /**
42       * Returns true if the parameter is null or of zero length
43       */
44      public static boolean isEmpty(final CharSequence s) {
45          return length(s) == 0;
46      }
47  
48      /**
49       * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
50       *
51       * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
52       *
53       * <pre>
54       * TextUtils.isBlank(null)      = true
55       * TextUtils.isBlank("")        = true
56       * TextUtils.isBlank(" ")       = true
57       * TextUtils.isBlank("abg")     = false
58       * TextUtils.isBlank("  abg  ") = false
59       * </pre>
60       *
61       * @param s  the CharSequence to check, may be null
62       * @return {@code true} if the CharSequence is null, empty or whitespace only
63       */
64      public static boolean isBlank(final CharSequence s) {
65          final int strLen = length(s);
66          if (strLen == 0) {
67              return true;
68          }
69          for (int i = 0; i < strLen; i++) {
70              if (!Character.isWhitespace(s.charAt(i))) {
71                  return false;
72              }
73          }
74          return true;
75      }
76  
77      /**
78       * Gets a CharSequence length or {@code 0} if the CharSequence is
79       * {@code null}.
80       *
81       * @param cs
82       *            a CharSequence or {@code null}
83       * @return CharSequence length or {@code 0} if the CharSequence is
84       *         {@code null}.
85       * @since 5.1
86       */
87      public static int length(final CharSequence cs) {
88          return cs == null ? 0 : cs.length();
89      }
90  
91      /**
92       * @since 4.4
93       */
94      public static boolean containsBlanks(final CharSequence s) {
95          final int strLen = length(s);
96          if (strLen == 0) {
97              return false;
98          }
99          for (int i = 0; i < s.length(); i++) {
100             if (Character.isWhitespace(s.charAt(i))) {
101                 return true;
102             }
103         }
104         return false;
105     }
106 
107     /**
108      * Returns a hexadecimal string with lowercase letters, representing the
109      * values of the {@code bytes}.
110      *
111      * @param bytes whose hex string should be created
112      * @return hex string for the bytes
113      *
114      * @since 5.0
115      */
116     public static String toHexString(final byte[] bytes) {
117         if (bytes == null) {
118             return null;
119         }
120         final StringBuilder buffer = new StringBuilder();
121         for (int i = 0; i < bytes.length; i++) {
122             final int unsignedB = bytes[i] & 0xff;
123             if (unsignedB < 16) {
124                 buffer.append('0');
125             }
126             buffer.append(Integer.toHexString(unsignedB));
127         }
128         return buffer.toString();
129     }
130 
131     /**
132      * Returns lower case representation of the given string
133      * using {@link Locale#ROOT}.
134      *
135      * @since 5.2
136      */
137     public static String toLowerCase(final String s) {
138         if (s == null) {
139             return null;
140         }
141         return s.toLowerCase(Locale.ROOT);
142     }
143 
144 }