Skip to content

Commit 26f19ca

Browse files
committed
use our own ArrayUtils at more places
1 parent 2cfa8c3 commit 26f19ca

File tree

3 files changed

+45
-3
lines changed

3 files changed

+45
-3
lines changed

src/main/java/org/htmlunit/html/HtmlNumberInput.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
import org.apache.commons.lang3.StringUtils;
2727
import org.htmlunit.SgmlPage;
28+
import org.htmlunit.util.ArrayUtils;
2829

2930
/**
3031
* Wrapper for the HTML element "input" with type is "number".
@@ -38,8 +39,8 @@
3839
*/
3940
public class HtmlNumberInput extends HtmlSelectableTextInput implements LabelableElement {
4041

41-
private static final char[] VALID_INT_CHARS = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '-'};
42-
private static final String VALID_CHARS = "0123456789-+.eE";
42+
private static final char[] VALID_INT_CHARS = "0123456789-".toCharArray();
43+
private static final char[] VALID_CHARS = "0123456789-+.eE".toCharArray();
4344

4445
/**
4546
* Creates an instance.
@@ -82,10 +83,11 @@ public void setDefaultChecked(final boolean defaultChecked) {
8283
@Override
8384
protected void doType(final char c, final boolean lastType) {
8485
if (!hasFeature(JS_INPUT_NUMBER_ACCEPT_ALL)) {
85-
if (VALID_CHARS.indexOf(c) == -1) {
86+
if (!ArrayUtils.contains(VALID_CHARS, c)) {
8687
return;
8788
}
8889
}
90+
8991
super.doType(c, lastType);
9092
}
9193

src/main/java/org/htmlunit/util/ArrayUtils.java

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,17 @@
2020
* @author Ronald Brill
2121
*/
2222
public final class ArrayUtils {
23+
2324
/**
2425
* An empty immutable {@code byte} array.
2526
*/
2627
public static final byte[] EMPTY_BYTE_ARRAY = {};
2728

29+
/**
30+
* An empty immutable {@code char} array.
31+
*/
32+
public static final char[] EMPTY_CHAR_ARRAY = {};
33+
2834
/**
2935
* An empty immutable {@link String} array.
3036
*/
@@ -60,6 +66,25 @@ public static boolean contains(final String[] strings, final String expected) {
6066
return false;
6167
}
6268

69+
/**
70+
* @param chars the char[] to check
71+
* @param expected the char that we expect
72+
* @return true if at least one element of the array equals to the expected char
73+
*/
74+
public static boolean contains(final char[] chars, final char expected) {
75+
if (chars == null) {
76+
return false;
77+
}
78+
79+
for (final char c : chars) {
80+
if (expected == c) {
81+
return true;
82+
}
83+
}
84+
85+
return false;
86+
}
87+
6388
/**
6489
* @param bytes the byte[] to check
6590
* @param expected the byte that we expect

src/test/java/org/htmlunit/util/ArrayUtilsTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,21 @@ public void containsIgnoreCase() throws Exception {
6868
() -> ArrayUtils.containsIgnoreCase(ArrayUtils.EMPTY_STRING_ARRAY, null));
6969
}
7070

71+
/**
72+
* @throws Exception if the test fails
73+
*/
74+
@Test
75+
public void containsChar() throws Exception {
76+
assertTrue(ArrayUtils.contains(new char[] {1}, (char) 1));
77+
assertTrue(ArrayUtils.contains(new char[] {7, 1, 9}, (char) 1));
78+
assertTrue(ArrayUtils.contains(new char[] {5, 2}, (char) 2));
79+
80+
assertFalse(ArrayUtils.contains(null, (char) 7));
81+
assertFalse(ArrayUtils.contains(new char[] {}, (char) 1));
82+
assertFalse(ArrayUtils.contains(ArrayUtils.EMPTY_CHAR_ARRAY, (char) 1));
83+
assertFalse(ArrayUtils.contains(new char[] {7, 9}, (char) 4));
84+
}
85+
7186
/**
7287
* @throws Exception if the test fails
7388
*/

0 commit comments

Comments
 (0)