1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.primefaces.extensions.util;
23
24 import java.util.Objects;
25 import java.util.function.Predicate;
26 import java.util.regex.Pattern;
27
28 public class ExtLangUtils {
29
30 public static final int INDEX_NOT_FOUND = -1;
31
32 private static final Predicate<String> IS_DIGITS_ONLY = Pattern.compile("\\d+").asMatchPredicate();
33
34 private ExtLangUtils() {
35
36 }
37
38 public static <T> boolean contains(final T[] array, final T value) {
39 if (array == null || value == null) {
40 return false;
41 }
42
43 for (final T entry : array) {
44 if (Objects.equals(value, entry)) {
45 return true;
46 }
47 }
48
49 return false;
50 }
51
52 public static String lowerCase(final String str) {
53 if (str == null) {
54 return null;
55 }
56
57 return str.toLowerCase();
58 }
59
60 public static int countMatches(final String str, final char c) {
61 if (str == null || str.isEmpty()) {
62 return 0;
63 }
64
65 int count = 0;
66 for (final char current : str.toCharArray()) {
67 if (current == c) {
68 count++;
69 }
70 }
71 return count;
72 }
73
74 public static String defaultString(final String str) {
75 return str == null ? "" : str;
76 }
77
78 public static String defaultString(final String str, final String def) {
79 return str == null ? def : str;
80 }
81
82 public static String deleteWhitespace(final String str) {
83 if (str == null || str.isEmpty()) {
84 return str;
85 }
86
87 final int sz = str.length();
88 final char[] chs = new char[sz];
89 int count = 0;
90 for (int i = 0; i < sz; i++) {
91 if (!Character.isWhitespace(str.charAt(i))) {
92 chs[count++] = str.charAt(i);
93 }
94 }
95
96 if (count == sz) {
97 return str;
98 }
99
100 return new String(chs, 0, count);
101 }
102
103 public static String[] subarray(final String[] array, int startIndexInclusive, int endIndexExclusive) {
104 if (array == null) {
105 return new String[0];
106 }
107 if (startIndexInclusive < 0) {
108 startIndexInclusive = 0;
109 }
110 if (endIndexExclusive > array.length) {
111 endIndexExclusive = array.length;
112 }
113 final int newSize = endIndexExclusive - startIndexInclusive;
114 if (newSize <= 0) {
115 return new String[0];
116 }
117
118 final String[] subarray = new String[newSize];
119 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
120 return subarray;
121 }
122
123 public static String normalizeSpace(final String s) {
124 return s.replaceAll("\\s+", " ").trim();
125 }
126
127 public static int indexOf(final CharSequence seq, final CharSequence searchSeq, final int startPos) {
128 if (seq == null || searchSeq == null) {
129 return INDEX_NOT_FOUND;
130 }
131 return seq.toString().indexOf(searchSeq.toString(), startPos);
132 }
133
134 public static String unescapeXml(final String text) {
135 final int n = text.length();
136 final StringBuilder result = new StringBuilder(n);
137 int i = 0;
138 while (i < n) {
139 final char charAt = text.charAt(i);
140 if (charAt != '&') {
141 result.append(charAt);
142 i++;
143 }
144 else {
145 if (text.startsWith("&", i)) {
146 result.append('&');
147 i += 5;
148 }
149 else if (text.startsWith("'", i)) {
150 result.append('\'');
151 i += 6;
152 }
153 else if (text.startsWith(""", i)) {
154 result.append('"');
155 i += 6;
156 }
157 else if (text.startsWith("<", i)) {
158 result.append('<');
159 i += 4;
160 }
161 else if (text.startsWith(">", i)) {
162 result.append('>');
163 i += 4;
164 }
165 else {
166 i++;
167 }
168 }
169 }
170 return result.toString();
171 }
172
173 public static boolean isDigitsOnly(String string) {
174 return IS_DIGITS_ONLY.test(string);
175 }
176
177 }