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  /**
31   * @since 4.3
32   */
33  public final class TextUtils {
34  
35      private TextUtils() {
36          // Do not allow utility class to be instantiated.
37      }
38  
39      /**
40       * Returns true if the parameter is null or of zero length
41       */
42      public static boolean isEmpty(final CharSequence s) {
43          return length(s) == 0;
44      }
45  
46      /**
47       * <p>Checks if a CharSequence is empty (""), null or whitespace only.</p>
48       *
49       * <p>Whitespace is defined by {@link Character#isWhitespace(char)}.</p>
50       *
51       * <pre>
52       * TextUtils.isBlank(null)      = true
53       * TextUtils.isBlank("")        = true
54       * TextUtils.isBlank(" ")       = true
55       * TextUtils.isBlank("abg")     = false
56       * TextUtils.isBlank("  abg  ") = false
57       * </pre>
58       *
59       * @param s  the CharSequence to check, may be null
60       * @return {@code true} if the CharSequence is null, empty or whitespace only
61       */
62      public static boolean isBlank(final CharSequence s) {
63          final int strLen = length(s);
64          if (strLen == 0) {
65              return true;
66          }
67          for (int i = 0; i < strLen; i++) {
68              if (!Character.isWhitespace(s.charAt(i))) {
69                  return false;
70              }
71          }
72          return true;
73      }
74  
75      /**
76       * Gets a CharSequence length or {@code 0} if the CharSequence is
77       * {@code null}.
78       *
79       * @param cs
80       *            a CharSequence or {@code null}
81       * @return CharSequence length or {@code 0} if the CharSequence is
82       *         {@code null}.
83       * @since 5.1
84       */
85      public static int length(final CharSequence cs) {
86          return cs == null ? 0 : cs.length();
87      }
88  
89      /**
90       * @since 4.4
91       */
92      public static boolean containsBlanks(final CharSequence s) {
93          final int strLen = length(s);
94          if (strLen == 0) {
95              return false;
96          }
97          for (int i = 0; i < s.length(); i++) {
98              if (Character.isWhitespace(s.charAt(i))) {
99                  return true;
100             }
101         }
102         return false;
103     }
104 
105     /**
106      * Returns a hexadecimal string with lowercase letters, representing the
107      * values of the {@code bytes}.
108      *
109      * @param bytes whose hex string should be created
110      * @return hex string for the bytes
111      *
112      * @since 5.0
113      */
114     public static String toHexString(final byte[] bytes) {
115         if (bytes == null) {
116             return null;
117         }
118         final StringBuilder buffer = new StringBuilder();
119         for (int i = 0; i < bytes.length; i++) {
120             final int unsignedB = bytes[i] & 0xff;
121             if (unsignedB < 16) {
122                 buffer.append('0');
123             }
124             buffer.append(Integer.toHexString(unsignedB));
125         }
126         return buffer.toString();
127     }
128 
129 }