Skip to content

Commit

Permalink
micro optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Nov 12, 2024
1 parent b0310fe commit fae9051
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/main/java/org/htmlunit/html/HtmlLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.htmlunit.BrowserVersion;
Expand All @@ -34,6 +32,7 @@
import org.htmlunit.javascript.PostponedAction;
import org.htmlunit.javascript.host.event.Event;
import org.htmlunit.javascript.host.html.HTMLLinkElement;
import org.htmlunit.util.ArrayUtils;
import org.htmlunit.util.StringUtils;
import org.htmlunit.xml.XmlPage;

Expand Down Expand Up @@ -344,10 +343,9 @@ public CssStyleSheet getSheet() {
* @return true if the rel attribute is 'stylesheet'
*/
public boolean isStyleSheetLink() {
String rel = getRelAttribute();
final String rel = getRelAttribute();
if (rel != null) {
rel = rel.toLowerCase(Locale.ROOT);
return ArrayUtils.contains(StringUtils.splitAtBlank(rel), "stylesheet");
return ArrayUtils.containsIgnoreCase(StringUtils.splitAtBlank(rel), "stylesheet");
}
return false;
}
Expand All @@ -356,10 +354,9 @@ public boolean isStyleSheetLink() {
* @return true if the rel attribute is 'modulepreload'
*/
public boolean isModulePreloadLink() {
String rel = getRelAttribute();
final String rel = getRelAttribute();
if (rel != null) {
rel = rel.toLowerCase(Locale.ROOT);
return ArrayUtils.contains(StringUtils.splitAtBlank(rel), "modulepreload");
return ArrayUtils.containsIgnoreCase(StringUtils.splitAtBlank(rel), "modulepreload");
}
return false;
}
Expand Down
54 changes: 54 additions & 0 deletions src/main/java/org/htmlunit/util/ArrayUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2002-2024 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.util;

/**
* Utility functions not covered by third party libraries.
*
* @author Ronald Brill
*/
public final class ArrayUtils {

/**
* Disallow instantiation of this class.
*/
private ArrayUtils() {
// Empty.
}

/**
* @param s the string[] to check
* @param expected the string that we expect
* @return true if at least one element of the array equalsIgnoreCase to the expected string
*/
public static boolean containsIgnoreCase(final String[] s, final String expected) {
if (expected == null) {
throw new IllegalArgumentException("Expected string can't be null");
}

if (s == null) {
return false;
}

for (int i = 0; i < s.length; i++) {
final String string = s[i];
if (expected.equalsIgnoreCase(string)) {
return true;
}
}

return false;
}
}
45 changes: 45 additions & 0 deletions src/test/java/org/htmlunit/util/ArrayUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright (c) 2002-2024 Gargoyle Software Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.htmlunit.util;

import static org.junit.Assert.assertThrows;

import org.htmlunit.SimpleWebTestCase;
import org.junit.Test;

/**
* Tests for {@link ArrayUtils}.
*
* @author Ronald Brill
*/
public class ArrayUtilsTest extends SimpleWebTestCase {

/**
* @throws Exception if the test fails
*/
@Test
public void containsIgnoreCase() throws Exception {
assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"ab"}, "ab"));
assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"o", "ab", "cd"}, "ab"));
assertTrue(ArrayUtils.containsIgnoreCase(new String[] {"cd", "ab"}, "ab"));

assertFalse(ArrayUtils.containsIgnoreCase(null, "ab"));
assertFalse(ArrayUtils.containsIgnoreCase(new String[] {}, "ab"));
assertFalse(ArrayUtils.containsIgnoreCase(new String[] {"cd", "ab"}, "x"));

assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase("AB", null));
assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase(null, null));
}
}

0 comments on commit fae9051

Please sign in to comment.