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 */
53 public static boolean contains(final String[] strings, final String expected) {
54 if (expected == null) {
55 throw new IllegalArgumentException("Expected string can't be null");
56 }
57
58 if (strings == null) {
59 return false;
60 }
61
62 for (final String s : strings) {
63 if (expected.equals(s)) {
64 return true;
65 }
66 }
67
68 return false;
69 }
70
71 /**
72 * Returns whether at least one element of the given array equals the expected char.
73 *
74 * @param chars the char array to check
75 * @param expected the char to search for
76 * @return {@code true} if at least one element equals the expected char
77 */
78 public static boolean contains(final char[] chars, final char expected) {
79 if (chars == null) {
80 return false;
81 }
82
83 for (final char c : chars) {
84 if (expected == c) {
85 return true;
86 }
87 }
88
89 return false;
90 }
91
92 /**
93 * Returns whether at least one element of the given array equals the expected byte.
94 *
95 * @param bytes the byte array to check
96 * @param expected the byte to search for
97 * @return {@code true} if at least one element equals the expected byte
98 */
99 public static boolean contains(final byte[] bytes, final byte expected) {
100 if (bytes == null) {
101 return false;
102 }
103
104 for (final byte b : bytes) {
105 if (expected == b) {
106 return true;
107 }
108 }
109
110 return false;
111 }
112
113 /**
114 * Returns whether at least one element of the given array equals the expected string,
115 * ignoring case.
116 *
117 * @param strings the string array to check
118 * @param expected the string to search for
119 * @return {@code true} if at least one element equals the expected string, ignoring case
120 */
121 public static boolean containsIgnoreCase(final String[] strings, final String expected) {
122 if (expected == null) {
123 throw new IllegalArgumentException("Expected string can't be null");
124 }
125
126 if (strings == null) {
127 return false;
128 }
129
130 for (final String s : strings) {
131 if (expected.equalsIgnoreCase(s)) {
132 return true;
133 }
134 }
135
136 return false;
137 }
138 }