diff --git a/src/main/java/ua/net/uid/utils/comparators/ComparableComparator.java b/src/main/java/ua/net/uid/utils/comparators/ComparableComparator.java deleted file mode 100644 index 479b5cb..0000000 --- a/src/main/java/ua/net/uid/utils/comparators/ComparableComparator.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 2019 nightfall. - * - * 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 - * - * http://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 ua.net.uid.utils.comparators; - -import java.util.Comparator; - -class ComparableComparator> implements Comparator { - private final boolean nullFirst; - - public ComparableComparator(boolean nullFirst) { this.nullFirst = nullFirst; } - public ComparableComparator() { this(true); } - - @Override - public int compare(T o1, T o2) { - if (o1 == o2) return 0; - if (o1 == null) return nullFirst ? -1 : 1; - if (o2 == null) return nullFirst ? 1 : -1; - return o1.compareTo(o2); - } -} \ No newline at end of file diff --git a/src/main/java/ua/net/uid/utils/comparators/NullComparator.java b/src/main/java/ua/net/uid/utils/comparators/NullComparator.java deleted file mode 100644 index 0e8a2d0..0000000 --- a/src/main/java/ua/net/uid/utils/comparators/NullComparator.java +++ /dev/null @@ -1,37 +0,0 @@ -/* - * Copyright 2019 nightfall. - * - * 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 - * - * http://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 ua.net.uid.utils.comparators; - -import java.util.Comparator; - -class NullComparator implements Comparator { - private final Comparator notNullComparator; - private final boolean nullFirst; - - public NullComparator(Comparator notNullComparator, boolean nullFirst) { - this.notNullComparator = notNullComparator; - this.nullFirst = nullFirst; - } - public NullComparator(Comparator notNullComparator) { this(notNullComparator, true); } - - @Override - public int compare(T o1, T o2) { - if (o1 == o2) return 0; - if (o1 == null) return nullFirst ? -1 : 1; - if (o2 == null) return nullFirst ? 1 : -1; - return notNullComparator.compare(o1, o2); - } -} \ No newline at end of file diff --git a/src/main/java/ua/net/uid/utils/comparators/NumberComparator.java b/src/main/java/ua/net/uid/utils/comparators/NumberComparator.java deleted file mode 100644 index 8f228ef..0000000 --- a/src/main/java/ua/net/uid/utils/comparators/NumberComparator.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * Copyright 2019 nightfall. - * - * 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 - * - * http://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 ua.net.uid.utils.comparators; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.util.Comparator; -import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; - -/** - * - * @author nightfall - */ -public class NumberComparator implements Comparator { - public static NumberComparator getInstance() { - return Holder.INSTANCE; - } - - private NumberComparator() {} - - @Override - //@SuppressWarnings("unchecked") - public int compare(Number o1, Number o2) { - if (o1 == o2) return 0; - if (o1 == null) return -1; - if (o2 == null) return 1; - if (o1.getClass().equals(o2.getClass()) && o1 instanceof Comparable) - return cmp(Comparable.class.cast(o1), Comparable.class.cast(o2)); - if (o1 instanceof BigDecimal) return compareBig((BigDecimal)o1, o2); - if (o2 instanceof BigDecimal) return -compareBig((BigDecimal) o2, o1); - if (o1 instanceof BigInteger) return compareBig((BigInteger) o1, o2); - if (o2 instanceof BigInteger) return -compareBig((BigInteger) o2, o1); - - return isInteger(o1) && isInteger(o2) - ? Long.compare(o1.longValue(), o1.longValue()) - : Double.compare(o1.doubleValue(), o2.doubleValue()); - } - - private static > int cmp(T o1, T o2) { - return o1.compareTo(o2); - } - - private static boolean isInteger(Number val) { - return val instanceof Byte || val instanceof Short - || val instanceof Integer || val instanceof Long - || val instanceof AtomicInteger || val instanceof AtomicLong; - } - - private static int compareBig(BigDecimal o1, Number o2) { - if (o2 instanceof BigInteger) - return o1.compareTo(new BigDecimal((BigInteger) o2)); - if (o2 instanceof Double || o2 instanceof Float) - return o1.compareTo(BigDecimal.valueOf(o2.doubleValue())); - return o1.compareTo(BigDecimal.valueOf(o2.longValue())); - } - - private static int compareBig(BigInteger o1, Number o2) { - return o2 instanceof Double || o2 instanceof Float - ? Double.compare(o1.doubleValue(), o2.doubleValue()) - : o1.compareTo(BigInteger.valueOf(o2.longValue())); - } - - private static final class Holder { - private static final NumberComparator INSTANCE = new NumberComparator(); - } -} diff --git a/src/main/java/ua/net/uid/utils/helpers/StringHelper.java b/src/main/java/ua/net/uid/utils/helpers/StringHelper.java index ca69d9e..1b9dbd1 100644 --- a/src/main/java/ua/net/uid/utils/helpers/StringHelper.java +++ b/src/main/java/ua/net/uid/utils/helpers/StringHelper.java @@ -48,24 +48,12 @@ for (int e = start; e < end; ++e) { char chr = string.charAt(e); switch (chr) { - case '"': - case '\\': - break; - case '\t': - chr = 't'; - break; - case '\b': - chr = 'b'; - break; - case '\n': - chr = 'n'; - break; - case '\r': - chr = 'r'; - break; - case '\f': - chr = 'f'; - break; + case '"': case '\\': break; + case '\t': chr = 't'; break; + case '\b': chr = 'b'; break; + case '\n': chr = 'n'; break; + case '\r': chr = 'r'; break; + case '\f': chr = 'f'; break; default: if (chr < 32) { if (start < e) builder.append(string, start, e); @@ -105,24 +93,12 @@ if (start < end) builder.append(string, start, end); chr = string.charAt(++end); switch (chr) { - case '"': - case '\\': - break; - case 't': - chr = '\t'; - break; - case 'b': - chr = '\b'; - break; - case 'n': - chr = '\n'; - break; - case 'r': - chr = '\r'; - break; - case 'f': - chr = '\f'; - break; + case '"': case '\\': break; + case 't': chr = '\t'; break; + case 'b': chr = '\b'; break; + case 'n': chr = '\n'; break; + case 'r': chr = '\r'; break; + case 'f': chr = '\f'; break; case 'u': chr = 0; for (int i = 1; i <= 4; ++i) { @@ -263,4 +239,20 @@ } return true; } + + public static int length(CharSequence str) { + return str == null ? 0 : str.length(); + } + + public static int[] toCodePoints(String str) { + if (str == null) return null; + int length = str.codePointCount(0, str.length()); + int[] result = new int[length]; + for (int i = 0, j = 0; i < length; ++i) { + int chr = str.codePointAt(j); + j += Character.charCount(chr); + result[i] = chr; + } + return result; + } } diff --git a/src/test/java/ua/net/uid/utils/helpers/StringHelperTest.java b/src/test/java/ua/net/uid/utils/helpers/StringHelperTest.java index 26b4da7..4880448 100644 --- a/src/test/java/ua/net/uid/utils/helpers/StringHelperTest.java +++ b/src/test/java/ua/net/uid/utils/helpers/StringHelperTest.java @@ -239,4 +239,21 @@ assertFalse(StringHelper.isBlank(" \t\r\n\u005Ctq")); assertFalse(StringHelper.isBlank(" \t\r\n\0x2F81A")); } + + @Test + void testLength() { + assertEquals(0, StringHelper.length(null)); + assertEquals(0, StringHelper.length("")); + assertEquals(3, StringHelper.length("123")); + } + + @Test + void testToCodePoints() { + assertNull(StringHelper.toCodePoints(null)); + assertEquals(0, StringHelper.toCodePoints("").length); + String src = "Пří視客øĥäΘい파ป็م♛"; + int[] codes = {1055, 345, 237, 35222, 23458, 248, 293, 228, 920, 12356, 54028, 3611, 3655, 1605, 9819}; + // String dst = new String(codes, 0, codes.length); + assertArrayEquals(codes, StringHelper.toCodePoints(src)); + } }