View Javadoc
1   /*
2    * CSVeed (https://github.com/42BV/CSVeed)
3    *
4    * Copyright 2013-2023 CSVeed.
5    *
6    * All rights reserved. This program and the accompanying materials
7    * are made available under the terms of The Apache Software License,
8    * Version 2.0 which accompanies this distribution, and is available at
9    * https://www.apache.org/licenses/LICENSE-2.0.txt
10   */
11  package org.csveed.bean.conversion;
12  
13  /**
14   * The Class ConversionUtil.
15   */
16  public final class ConversionUtil {
17  
18      /**
19       * Instantiates a new conversion util.
20       */
21      private ConversionUtil() {
22          // Prevent Instantiation of static utils class
23      }
24  
25      /**
26       * Checks for length.
27       *
28       * @param str
29       *            the str
30       *
31       * @return true, if successful
32       */
33      public static boolean hasLength(CharSequence str) {
34          return str != null && str.length() > 0;
35      }
36  
37      /**
38       * Checks for text.
39       *
40       * @param str
41       *            the str
42       *
43       * @return true, if successful
44       */
45      public static boolean hasText(String str) {
46          return hasText((CharSequence) str);
47      }
48  
49      /**
50       * Checks for text.
51       *
52       * @param str
53       *            the str
54       *
55       * @return true, if successful
56       */
57      public static boolean hasText(CharSequence str) {
58          if (!hasLength(str)) {
59              return false;
60          }
61          int strLen = str.length();
62          for (int i = 0; i < strLen; i++) {
63              if (!Character.isWhitespace(str.charAt(i))) {
64                  return true;
65              }
66          }
67          return false;
68      }
69  
70      /**
71       * Trim all whitespace.
72       *
73       * @param str
74       *            the str
75       *
76       * @return the string
77       */
78      public static String trimAllWhitespace(String str) {
79          if (!hasLength(str)) {
80              return str;
81          }
82          StringBuilder sb = new StringBuilder(str);
83          int index = 0;
84          while (sb.length() > index) {
85              if (Character.isWhitespace(sb.charAt(index))) {
86                  sb.deleteCharAt(index);
87              } else {
88                  index++;
89              }
90          }
91          return sb.toString();
92      }
93  
94  }