diff --git a/CHANGES.txt b/CHANGES.txt index 62217698..064223ef 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -25,7 +25,9 @@ 12, Improve DynamoDBExecutor. -13, Improvements and bug fix. +13, Remove findFirst and findLast from N, replaced by between. + +14, Improvements and bug fix. ========Changes in 0.9.81========================================================================= diff --git a/lib/abacus-util-0.9.82.jar b/lib/abacus-util-0.9.82.jar index 513fc74f..2c5b32a6 100644 Binary files a/lib/abacus-util-0.9.82.jar and b/lib/abacus-util-0.9.82.jar differ diff --git a/lib/abacus-util-all-0.9.82.jar b/lib/abacus-util-all-0.9.82.jar index 51319749..ea2638bf 100644 Binary files a/lib/abacus-util-all-0.9.82.jar and b/lib/abacus-util-all-0.9.82.jar differ diff --git a/lib/abacus-util-all-jdk7-0.9.82.jar b/lib/abacus-util-all-jdk7-0.9.82.jar index a84fd463..718bf780 100644 Binary files a/lib/abacus-util-all-jdk7-0.9.82.jar and b/lib/abacus-util-all-jdk7-0.9.82.jar differ diff --git a/lib/abacus-util-jdk7-0.9.82.jar b/lib/abacus-util-jdk7-0.9.82.jar index 028e6600..11491c5b 100644 Binary files a/lib/abacus-util-jdk7-0.9.82.jar and b/lib/abacus-util-jdk7-0.9.82.jar differ diff --git a/src/com/landawn/abacus/util/N.java b/src/com/landawn/abacus/util/N.java index 6eba459c..616273d4 100644 --- a/src/com/landawn/abacus/util/N.java +++ b/src/com/landawn/abacus/util/N.java @@ -141,6 +141,7 @@ import com.landawn.abacus.util.function.IndexedConsumer; import com.landawn.abacus.util.function.IntFunction; import com.landawn.abacus.util.function.IntPredicate; +import com.landawn.abacus.util.function.IntUnaryOperator; import com.landawn.abacus.util.function.LongPredicate; import com.landawn.abacus.util.function.Predicate; import com.landawn.abacus.util.function.ShortPredicate; @@ -15374,160 +15375,437 @@ public static int binarySearch(final List c, final int fromInde return Array.binarySearch(c, fromIndex, toIndex, key, cmp); } + // /** + // * + // * findFirst("3[a2[c]]2[a]", "[", "]") = "a2[c" + // * + // * @param str + // * @param prefix + // * @param postfix + // * @return + // */ + // public static Optional findFirst(String str, String prefix, String postfix) { + // return findFirst(String.class, str, prefix, postfix); + // } + // + // /** + // * + // * findFirst("3[a2[c]]2[a]", "[", "]") = "a2[c" + // * + // * @param str + // * @param fromIndex + // * @param prefix + // * @param postfix + // * @return + // */ + // public static Optional findFirst(String str, int fromIndex, String prefix, String postfix) { + // return findFirst(String.class, str, fromIndex, prefix, postfix); + // } + // + // /** + // * + // * findFirst("3[a2[c]]2[a]", "[", "]") = "a2[c" + // * + // * @param targetClass + // * @param str + // * @param prefix + // * @param postfix + // * @return + // */ + // public static Optional findFirst(Class targetClass, String str, String prefix, String postfix) { + // return findFirst(targetClass, str, 0, prefix, postfix); + // } + // + // /** + // * Returns first substring between the specified prefix and postfix, or default value if there is no substring found. + // * + // * findFirst("3[a2[c]]2[a]", "[", "]") = "a2[c" + // * + // * @param targetClass + // * @param str + // * @param fromIndex + // * @param prefix + // * @param postfix + // * @return + // * @see String#indexOf(String, int) + // */ + // public static Optional findFirst(Class targetClass, String str, int fromIndex, String prefix, String postfix) { + // final Type type = typeOf(targetClass); + // + // if (N.isNullOrEmpty(str)) { + // return Optional.empty(); + // } + // + // int beginIndex = N.isNullOrEmpty(prefix) ? 0 : str.indexOf(prefix, fromIndex); + // + // if (beginIndex < 0) { + // return Optional.empty(); + // } + // + // beginIndex += N.isNullOrEmpty(prefix) ? 0 : prefix.length(); + // + // int endIndex = N.isNullOrEmpty(postfix) ? str.length() : str.indexOf(postfix, beginIndex); + // + // if (endIndex < 0) { + // return Optional.empty(); + // } + // + // return Optional.of(N.as(type, str.subSequence(beginIndex, endIndex))); + // } + // + // /** + // * + // * findLast("3[a2[c]]2a]", "[", "]") = "c]]2a" + // * + // * @param str + // * @param prefix + // * @param postfix + // * @return + // */ + // public static Optional findLast(String str, String prefix, String postfix) { + // return findLast(String.class, str, prefix, postfix); + // } + // + // /** + // * + // * findLast("3[a2[c]]2a]", "[", "]") = "c]]2a" + // * + // * @param str + // * @param fromIndex + // * @param prefix + // * @param postfix + // * @return + // */ + // public static Optional findLast(String str, int fromIndex, String prefix, String postfix) { + // return findLast(String.class, str, fromIndex, prefix, postfix); + // } + // + // /** + // * + // * findLast("3[a2[c]]2a]", "[", "]") = "c]]2a" + // * + // * @param targetClass + // * @param str + // * @param prefix + // * @param postfix + // * @return + // */ + // public static Optional findLast(Class targetClass, String str, String prefix, String postfix) { + // return findLast(targetClass, str, N.isNullOrEmpty(str) ? 0 : str.length(), prefix, postfix); + // } + // + // /** + // * Returns last substring between the specified prefix and postfix, or default value if there is no substring found. + // * + // * findLast("3[a2[c]]2a]", "[", "]") = "c]]2a" + // * + // * @param targetClass + // * @param str + // * @param fromIndex + // * @param prefix + // * @param postfix + // * @return + // * @see String#lastIndexOf(String, int) + // */ + // public static Optional findLast(Class targetClass, String str, int fromIndex, String prefix, String postfix) { + // final Type type = typeOf(targetClass); + // + // if (N.isNullOrEmpty(str)) { + // return Optional.empty(); + // } + // + // int endIndex = N.isNullOrEmpty(postfix) ? str.length() : str.lastIndexOf(postfix, fromIndex); + // + // if (endIndex < 0 || (N.notNullOrEmpty(prefix) && endIndex < prefix.length())) { + // return Optional.empty(); + // } + // + // int beginIndex = N.isNullOrEmpty(prefix) ? 0 : str.lastIndexOf(prefix, endIndex - prefix.length()); + // + // if (beginIndex < 0) { + // return Optional.empty(); + // } + // + // beginIndex += N.isNullOrEmpty(prefix) ? 0 : prefix.length(); + // + // return Optional.of(N.as(type, str.subSequence(beginIndex, endIndex))); + // } + /** - * - * findFirst("3[a2[c]]2[a]", "[", "]") = "a2[c" + * Returns an empty Optional if {@code inclusiveBeginIndex < 0 || exclusiveEndIndex < 0 || inclusiveBeginIndex > exclusiveEndIndex}, + * otherwise an {@code Optional} with String value: {@code str.substring(exclusiveBeginIndex, exclusiveEndIndex)} is returned. * * @param str - * @param prefix - * @param postfix + * @param inclusiveBeginIndex + * @param exclusiveEndIndex * @return */ - public static Optional findFirst(String str, String prefix, String postfix) { - return findFirst(String.class, str, prefix, postfix); + public static Optional substring(String str, int inclusiveBeginIndex, int exclusiveEndIndex) { + if (inclusiveBeginIndex < 0 || exclusiveEndIndex < 0 || inclusiveBeginIndex > exclusiveEndIndex) { + return Optional. empty(); + } + + return Optional.of(str.substring(inclusiveBeginIndex, exclusiveEndIndex)); } /** - * - * findFirst("3[a2[c]]2[a]", "[", "]") = "a2[c" + * Returns an empty Optional if {@code inclusiveBeginIndex < 0}, + * otherwise an {@code Optional} with String value: {@code str.substring(inclusiveBeginIndex)} is returned. * * @param str - * @param fromIndex - * @param prefix - * @param postfix + * @param inclusiveBeginIndex * @return + * @see #substring(String, int, int) */ - public static Optional findFirst(String str, int fromIndex, String prefix, String postfix) { - return findFirst(String.class, str, fromIndex, prefix, postfix); + public static Optional substring(String str, int inclusiveBeginIndex) { + if (inclusiveBeginIndex < 0) { + return Optional. empty(); + } + + return Optional.of(str.substring(inclusiveBeginIndex)); } /** + * Returns an empty Optional if {@code N.isNullOrEmpty(str) || str.indexOf(delimiterOfInclusiveBeginIndex) < 0}, + * otherwise an {@code Optional} with String value: {@code str.substring(str.indexOf(delimiterOfInclusiveBeginIndex))} is returned. * - * findFirst("3[a2[c]]2[a]", "[", "]") = "a2[c" - * - * @param targetClass * @param str - * @param prefix - * @param postfix + * @param delimiterOfInclusiveBeginIndex {@code inclusiveBeginIndex <- str.indexOf(delimiterOfInclusiveBeginIndex)} * @return + * @see #substring(String, int) */ - public static Optional findFirst(Class targetClass, String str, String prefix, String postfix) { - return findFirst(targetClass, str, 0, prefix, postfix); + public static Optional substring(String str, char delimiterOfInclusiveBeginIndex) { + if (N.isNullOrEmpty(str)) { + return Optional. empty(); + } + + return substring(str, str.indexOf(delimiterOfInclusiveBeginIndex)); } /** - * Returns first substring between the specified prefix and postfix, or default value if there is no substring found. + * Returns an empty Optional if {@code N.isNullOrEmpty(str) || str.indexOf(delimiterOfInclusiveBeginIndex) < 0}, + * otherwise an {@code Optional} with String value: {@code str.substring(str.indexOf(delimiterOfInclusiveBeginIndex))} is returned. * - * findFirst("3[a2[c]]2[a]", "[", "]") = "a2[c" - * - * @param targetClass * @param str - * @param fromIndex - * @param prefix - * @param postfix + * @param delimiterOfInclusiveBeginIndex {@code inclusiveBeginIndex <- str.indexOf(delimiterOfInclusiveBeginIndex)} * @return - * @see String#indexOf(String, int) + * @see #substring(String, int) */ - public static Optional findFirst(Class targetClass, String str, int fromIndex, String prefix, String postfix) { - final Type type = typeOf(targetClass); - + public static Optional substring(String str, String delimiterOfInclusiveBeginIndex) { if (N.isNullOrEmpty(str)) { - return Optional.empty(); + return Optional. empty(); } - int beginIndex = N.isNullOrEmpty(prefix) ? 0 : str.indexOf(prefix, fromIndex); + return substring(str, str.indexOf(delimiterOfInclusiveBeginIndex)); + } - if (beginIndex < 0) { - return Optional.empty(); + /** + * + * @param str + * @param inclusiveBeginIndex + * @param delimiterOfExclusiveEndIndex {@code exclusiveEndIndex <- str.indexOf(delimiterOfExclusiveEndIndex, inclusiveBeginIndex + 1) if inclusiveBeginIndex >= 0} + * @return + * @see #substring(String, int, int) + */ + public static Optional substring(String str, int inclusiveBeginIndex, char delimiterOfExclusiveEndIndex) { + if (inclusiveBeginIndex < 0) { + return Optional. empty(); } - beginIndex += N.isNullOrEmpty(prefix) ? 0 : prefix.length(); - - int endIndex = N.isNullOrEmpty(postfix) ? str.length() : str.indexOf(postfix, beginIndex); + return substring(str, inclusiveBeginIndex, str.indexOf(delimiterOfExclusiveEndIndex, inclusiveBeginIndex + 1)); + } - if (endIndex < 0) { - return Optional.empty(); + /** + * + * @param str + * @param inclusiveBeginIndex + * @param delimiterOfExclusiveEndIndex {@code exclusiveEndIndex <- str.indexOf(delimiterOfExclusiveEndIndex, inclusiveBeginIndex + 1) if inclusiveBeginIndex >= 0} + * @return + * @see #substring(String, int, int) + */ + public static Optional substring(String str, int inclusiveBeginIndex, String delimiterOfExclusiveEndIndex) { + if (inclusiveBeginIndex < 0) { + return Optional. empty(); } - return Optional.of(N.as(type, str.subSequence(beginIndex, endIndex))); + return substring(str, inclusiveBeginIndex, str.indexOf(delimiterOfExclusiveEndIndex, inclusiveBeginIndex + 1)); } /** - * - * findLast("3[a2[c]]2a]", "[", "]") = "c]]2a" * * @param str - * @param prefix - * @param postfix + * @param inclusiveBeginIndex + * @param funcOfExclusiveEndIndex {@code exclusiveEndIndex <- funcOfExclusiveEndIndex.applyAsInt(inclusiveBeginIndex) if inclusiveBeginIndex >= 0} * @return + * @see #substring(String, int, int) */ - public static Optional findLast(String str, String prefix, String postfix) { - return findLast(String.class, str, prefix, postfix); + public static Optional substring(String str, int inclusiveBeginIndex, IntUnaryOperator funcOfExclusiveEndIndex) { + if (inclusiveBeginIndex < 0) { + return Optional. empty(); + } + + return substring(str, inclusiveBeginIndex, funcOfExclusiveEndIndex.applyAsInt(inclusiveBeginIndex)); } /** * - * findLast("3[a2[c]]2a]", "[", "]") = "c]]2a" + * @param str + * @param delimiterOfInclusiveBeginIndex {@code inclusiveBeginIndex <- str.lastIndexOf(delimiterOfInclusiveBeginIndex, exclusiveEndIndex - 1) if exclusiveEndIndex > 0} + * @param exclusiveEndIndex + * @return + * @see #substring(String, int, int) + */ + public static Optional substring(String str, char delimiterOfInclusiveBeginIndex, int exclusiveEndIndex) { + if (exclusiveEndIndex <= 0) { + return Optional. empty(); + } + + return substring(str, str.lastIndexOf(delimiterOfInclusiveBeginIndex, exclusiveEndIndex - 1), exclusiveEndIndex); + } + + /** * * @param str - * @param fromIndex - * @param prefix - * @param postfix + * @param delimiterOfInclusiveBeginIndex {@code inclusiveBeginIndex <- str.lastIndexOf(delimiterOfInclusiveBeginIndex, exclusiveEndIndex - 1) if exclusiveEndIndex > 0} + * @param exclusiveEndIndex * @return + * @see #substring(String, int, int) */ - public static Optional findLast(String str, int fromIndex, String prefix, String postfix) { - return findLast(String.class, str, fromIndex, prefix, postfix); + public static Optional substring(String str, String delimiterOfInclusiveBeginIndex, int exclusiveEndIndex) { + if (exclusiveEndIndex <= 0) { + return Optional. empty(); + } + + return substring(str, str.lastIndexOf(delimiterOfInclusiveBeginIndex, exclusiveEndIndex - 1), exclusiveEndIndex); } /** * - * findLast("3[a2[c]]2a]", "[", "]") = "c]]2a" + * @param str + * @param funcOfInclusiveBeginIndex {@code inclusiveBeginIndex <- funcOfInclusiveBeginIndex.applyAsInt(exclusiveEndIndex)) if exclusiveEndIndex > 0} + * @param exclusiveEndIndex + * @return + * @see #substring(String, int, int) + */ + public static Optional substring(String str, IntUnaryOperator funcOfInclusiveBeginIndex, int exclusiveEndIndex) { + if (exclusiveEndIndex <= 0) { + return Optional. empty(); + } + + return substring(str, funcOfInclusiveBeginIndex.applyAsInt(exclusiveEndIndex), exclusiveEndIndex); + } + + /** + * Returns an empty Optional if {@code exclusiveBeginIndex < 0 || exclusiveEndIndex < 0 || exclusiveBeginIndex >= exclusiveEndIndex}, + * otherwise an {@code Optional} with String value: {@code str.substring(exclusiveBeginIndex + 1, exclusiveEndIndex)} is returned. * - * @param targetClass * @param str - * @param prefix - * @param postfix + * @param exclusiveBeginIndex + * @param exclusiveEndIndex * @return */ - public static Optional findLast(Class targetClass, String str, String prefix, String postfix) { - return findLast(targetClass, str, N.isNullOrEmpty(str) ? 0 : str.length(), prefix, postfix); + public static Optional between(String str, int exclusiveBeginIndex, int exclusiveEndIndex) { + if (exclusiveBeginIndex < 0 || exclusiveEndIndex < 0 || exclusiveBeginIndex >= exclusiveEndIndex) { + return Optional. empty(); + } + + return Optional.of(str.substring(exclusiveBeginIndex + 1, exclusiveEndIndex)); } /** - * Returns last substring between the specified prefix and postfix, or default value if there is no substring found. * - * findLast("3[a2[c]]2a]", "[", "]") = "c]]2a" + * @param str + * @param exclusiveBeginIndex + * @param delimiterOfExclusiveEndIndex {@code exclusiveEndIndex <- str.indexOf(delimiterOfExclusiveEndIndex, beginIndex + 1) if exclusiveBeginIndex >= 0} + * @return + * @see #between(String, int, int) + */ + public static Optional between(String str, int exclusiveBeginIndex, char delimiterOfExclusiveEndIndex) { + if (exclusiveBeginIndex < 0) { + return Optional. empty(); + } + + return between(str, exclusiveBeginIndex, str.indexOf(delimiterOfExclusiveEndIndex, exclusiveBeginIndex + 1)); + } + + /** * - * @param targetClass * @param str - * @param fromIndex - * @param prefix - * @param postfix + * @param exclusiveBeginIndex + * @param delimiterOfExclusiveEndIndex {@code exclusiveEndIndex <- str.indexOf(delimiterOfExclusiveEndIndex, beginIndex + 1) if exclusiveBeginIndex >= 0} * @return - * @see String#lastIndexOf(String, int) + * @see #between(String, int, int) */ - public static Optional findLast(Class targetClass, String str, int fromIndex, String prefix, String postfix) { - final Type type = typeOf(targetClass); + public static Optional between(String str, int exclusiveBeginIndex, String delimiterOfExclusiveEndIndex) { + if (exclusiveBeginIndex < 0) { + return Optional. empty(); + } - if (N.isNullOrEmpty(str)) { - return Optional.empty(); + return between(str, exclusiveBeginIndex, str.indexOf(delimiterOfExclusiveEndIndex, exclusiveBeginIndex + 1)); + } + + /** + * + * @param str + * @param exclusiveBeginIndex + * @param funcOfExclusiveEndIndex {@code exclusiveEndIndex <- funcOfExclusiveEndIndex.applyAsInt(inclusiveBeginIndex) if inclusiveBeginIndex >= 0} + * @return + * @see #between(String, int, int) + */ + public static Optional between(String str, int exclusiveBeginIndex, IntUnaryOperator funcOfExclusiveEndIndex) { + if (exclusiveBeginIndex < 0) { + return Optional. empty(); } - int endIndex = N.isNullOrEmpty(postfix) ? str.length() : str.lastIndexOf(postfix, fromIndex); + return between(str, exclusiveBeginIndex, funcOfExclusiveEndIndex.applyAsInt(exclusiveBeginIndex)); + } - if (endIndex < 0 || (N.notNullOrEmpty(prefix) && endIndex < prefix.length())) { - return Optional.empty(); + /** + * + * @param str + * @param delimiterOfExclusiveBeginIndex {@code exclusiveBeginIndex <- str.lastIndexOf(delimiterOfExclusiveBeginIndex, exclusiveEndIndex - 1) if exclusiveEndIndex > 0} + * @param exclusiveEndIndex + * @return + * @see #between(String, int, int) + */ + public static Optional between(String str, char delimiterOfExclusiveBeginIndex, int exclusiveEndIndex) { + if (exclusiveEndIndex <= 0) { + return Optional. empty(); } - int beginIndex = N.isNullOrEmpty(prefix) ? 0 : str.lastIndexOf(prefix, endIndex - prefix.length()); + return between(str, str.lastIndexOf(delimiterOfExclusiveBeginIndex, exclusiveEndIndex - 1), exclusiveEndIndex); + } - if (beginIndex < 0) { - return Optional.empty(); + /** + * + * @param str + * @param delimiterOfExclusiveBeginIndex {@code exclusiveBeginIndex <- str.lastIndexOf(delimiterOfExclusiveBeginIndex, exclusiveEndIndex - 1) if exclusiveEndIndex > 0} + * @param exclusiveEndIndex + * @return + * @see #between(String, int, int) + */ + public static Optional between(String str, String delimiterOfExclusiveBeginIndex, int exclusiveEndIndex) { + if (exclusiveEndIndex <= 0) { + return Optional. empty(); } - beginIndex += N.isNullOrEmpty(prefix) ? 0 : prefix.length(); + return between(str, str.lastIndexOf(delimiterOfExclusiveBeginIndex, exclusiveEndIndex - 1), exclusiveEndIndex); + } + + /** + * + * @param str + * @param funcOfExclusiveBeginIndex {@code exclusiveBeginIndex <- funcOfExclusiveBeginIndex.applyAsInt(exclusiveEndIndex)) if exclusiveEndIndex > 0} + * @param exclusiveEndIndex + * @return + * @see #between(String, int, int) + */ + public static Optional between(String str, IntUnaryOperator funcOfExclusiveBeginIndex, int exclusiveEndIndex) { + if (exclusiveEndIndex <= 0) { + return Optional. empty(); + } - return Optional.of(N.as(type, str.subSequence(beginIndex, endIndex))); + return between(str, funcOfExclusiveBeginIndex.applyAsInt(exclusiveEndIndex), exclusiveEndIndex); } /**