Skip to content

Commit 2ac6cb9

Browse files
committed
refactor: avoid use of commons.lang3
Close #204
1 parent 102eed0 commit 2ac6cb9

File tree

3 files changed

+158
-1
lines changed

3 files changed

+158
-1
lines changed
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
/*-
2+
* #%L
3+
* TwinColGrid add-on
4+
* %%
5+
* Copyright (C) 2017 - 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.twincolgrid;
21+
/*
22+
* Licensed to the Apache Software Foundation (ASF) under one or more
23+
* contributor license agreements. See the NOTICE file distributed with
24+
* this work for additional information regarding copyright ownership.
25+
* The ASF licenses this file to You under the Apache License, Version 2.0
26+
* (the "License"); you may not use this file except in compliance with
27+
* the License. You may obtain a copy of the License at
28+
*
29+
* https://www.apache.org/licenses/LICENSE-2.0
30+
*
31+
* Unless required by applicable law or agreed to in writing, software
32+
* distributed under the License is distributed on an "AS IS" BASIS,
33+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34+
* See the License for the specific language governing permissions and
35+
* limitations under the License.
36+
*/
37+
38+
import lombok.experimental.UtilityClass;
39+
40+
@UtilityClass
41+
class CharSequenceUtils {
42+
43+
/**
44+
* Green implementation of regionMatches.
45+
*
46+
* @param cs the {@link CharSequence} to be processed
47+
* @param ignoreCase whether or not to be case-insensitive
48+
* @param thisStart the index to start on the {@code cs} CharSequence
49+
* @param substring the {@link CharSequence} to be looked for
50+
* @param start the index to start on the {@code substring} CharSequence
51+
* @param length character length of the region
52+
* @return whether the region matched
53+
* @see String#regionMatches(boolean, int, String, int, int)
54+
*/
55+
static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
56+
final CharSequence substring, final int start, final int length) {
57+
if (cs instanceof String && substring instanceof String) {
58+
return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
59+
}
60+
int index1 = thisStart;
61+
int index2 = start;
62+
int tmpLen = length;
63+
64+
// Extract these first so we detect NPEs the same as the java.lang.String version
65+
final int srcLen = cs.length() - thisStart;
66+
final int otherLen = substring.length() - start;
67+
68+
// Check for invalid parameters
69+
if (thisStart < 0 || start < 0 || length < 0) {
70+
return false;
71+
}
72+
73+
// Check that the regions are long enough
74+
if (srcLen < length || otherLen < length) {
75+
return false;
76+
}
77+
78+
while (tmpLen-- > 0) {
79+
final char c1 = cs.charAt(index1++);
80+
final char c2 = substring.charAt(index2++);
81+
82+
if (c1 == c2) {
83+
continue;
84+
}
85+
86+
if (!ignoreCase) {
87+
return false;
88+
}
89+
90+
// The real same check as in String#regionMatches(boolean, int, String, int, int):
91+
final char u1 = Character.toUpperCase(c1);
92+
final char u2 = Character.toUpperCase(c2);
93+
if (u1 != u2 && Character.toLowerCase(u1) != Character.toLowerCase(u2)) {
94+
return false;
95+
}
96+
}
97+
98+
return true;
99+
}
100+
101+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*-
2+
* #%L
3+
* TwinColGrid add-on
4+
* %%
5+
* Copyright (C) 2017 - 2025 Flowing Code
6+
* %%
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
* #L%
19+
*/
20+
package com.flowingcode.vaadin.addons.twincolgrid;
21+
/*
22+
* Licensed to the Apache Software Foundation (ASF) under one or more
23+
* contributor license agreements. See the NOTICE file distributed with
24+
* this work for additional information regarding copyright ownership.
25+
* The ASF licenses this file to You under the Apache License, Version 2.0
26+
* (the "License"); you may not use this file except in compliance with
27+
* the License. You may obtain a copy of the License at
28+
*
29+
* https://www.apache.org/licenses/LICENSE-2.0
30+
*
31+
* Unless required by applicable law or agreed to in writing, software
32+
* distributed under the License is distributed on an "AS IS" BASIS,
33+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
34+
* See the License for the specific language governing permissions and
35+
* limitations under the License.
36+
*/
37+
38+
import lombok.experimental.UtilityClass;
39+
40+
@UtilityClass
41+
class StringUtils {
42+
43+
public static boolean containsIgnoreCase(String str, String searchStr) {
44+
if (str == null || searchStr == null) {
45+
return false;
46+
}
47+
final int len = searchStr.length();
48+
final int max = str.length() - len;
49+
for (int i = 0; i <= max; i++) {
50+
if (CharSequenceUtils.regionMatches(str, true, i, searchStr, 0, len)) {
51+
return true;
52+
}
53+
}
54+
return false;
55+
}
56+
57+
}

src/main/java/com/flowingcode/vaadin/addons/twincolgrid/TwinColGrid.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,6 @@
7272
import java.util.stream.Collectors;
7373
import java.util.stream.Stream;
7474
import lombok.NonNull;
75-
import org.apache.commons.lang3.StringUtils;
7675

7776
@SuppressWarnings("serial")
7877
@JsModule(value = "./src/fc-twin-col-grid-auto-resize.js")

0 commit comments

Comments
 (0)