1 /*
2 * Copyright (c) 2002-2025 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 * @param strings the string[] to check
48 * @param expected the string that we expect
49 * @return true if at least one element of the array equals to the expected string
50 */
51 public static boolean contains(final String[] strings, final String expected) {
52 if (expected == null) {
53 throw new IllegalArgumentException("Expected string can't be null");
54 }
55
56 if (strings == null) {
57 return false;
58 }
59
60 for (final String s : strings) {
61 if (expected.equals(s)) {
62 return true;
63 }
64 }
65
66 return false;
67 }
68
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
88 /**
89 * @param bytes the byte[] to check
90 * @param expected the byte that we expect
91 * @return true if at least one element of the array equals to the expected byte
92 */
93 public static boolean contains(final byte[] bytes, final byte expected) {
94 if (bytes == null) {
95 return false;
96 }
97
98 for (final byte b : bytes) {
99 if (expected == b) {
100 return true;
101 }
102 }
103
104 return false;
105 }
106
107 /**
108 * @param strings the string[] to check
109 * @param expected the string that we expect
110 * @return true if at least one element of the array equalsIgnoreCase to the expected string
111 */
112 public static boolean containsIgnoreCase(final String[] strings, final String expected) {
113 if (expected == null) {
114 throw new IllegalArgumentException("Expected string can't be null");
115 }
116
117 if (strings == null) {
118 return false;
119 }
120
121 for (final String s : strings) {
122 if (expected.equalsIgnoreCase(s)) {
123 return true;
124 }
125 }
126
127 return false;
128 }
129 }