1 /*
2 * Copyright (c) 2002-2026 Gargoyle Software Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 * https://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15 package org.htmlunit.util;
16
17 /**
18 * Utility functions not covered by third-party libraries.
19 *
20 * @author Ronald Brill
21 */
22 public final class ArrayUtils {
23
24 /**
25 * An empty immutable {@code byte} array.
26 */
27 public static final byte[] EMPTY_BYTE_ARRAY = {};
28
29 /**
30 * An empty immutable {@code char} array.
31 */
32 public static final char[] EMPTY_CHAR_ARRAY = {};
33
34 /**
35 * An empty immutable {@link String} array.
36 */
37 public static final String[] EMPTY_STRING_ARRAY = {};
38
39 /**
40 * Disallow instantiation of this class.
41 */
42 private ArrayUtils() {
43 // Empty.
44 }
45
46 /**
47 * Returns whether at least one element of the given array equals the expected string.
48 *
49 * @param strings the string array to check
50 * @param expected the string to search for
51 * @return {@code true} if at least one element equals the expected string
52 * @throws IllegalArgumentException if expected is null
53 */
54 public static boolean contains(final String[] strings, final String expected) {
55 if (expected == null) {
56 throw new IllegalArgumentException("Expected string can't be null");
57 }
58
59 if (strings == null) {
60 return false;
61 }
62
63 for (final String s : strings) {
64 if (expected.equals(s)) {
65 return true;
66 }
67 }
68
69 return false;
70 }
71
72 /**
73 * Returns whether at least one element of the given array equals the expected char.
74 *
75 * @param chars the char array to check
76 * @param expected the char to search for
77 * @return {@code true} if at least one element equals the expected char
78 */
79 public static boolean contains(final char[] chars, final char expected) {
80 if (chars == null) {
81 return false;
82 }
83
84 for (final char c : chars) {
85 if (expected == c) {
86 return true;
87 }
88 }
89
90 return false;
91 }
92
93 /**
94 * Returns whether at least one element of the given array equals the expected byte.
95 *
96 * @param bytes the byte array to check
97 * @param expected the byte to search for
98 * @return {@code true} if at least one element equals the expected byte
99 */
100 public static boolean contains(final byte[] bytes, final byte expected) {
101 if (bytes == null) {
102 return false;
103 }
104
105 for (final byte b : bytes) {
106 if (expected == b) {
107 return true;
108 }
109 }
110
111 return false;
112 }
113
114 /**
115 * Returns whether at least one element of the given array equals the expected string,
116 * ignoring case.
117 *
118 * @param strings the string array to check
119 * @param expected the string to search for
120 * @return {@code true} if at least one element equals the expected string, ignoring case
121 * @throws IllegalArgumentException if expected is null
122 */
123 public static boolean containsIgnoreCase(final String[] strings, final String expected) {
124 if (expected == null) {
125 throw new IllegalArgumentException("Expected string can't be null");
126 }
127
128 if (strings == null) {
129 return false;
130 }
131
132 for (final String s : strings) {
133 if (expected.equalsIgnoreCase(s)) {
134 return true;
135 }
136 }
137
138 return false;
139 }
140 }