+ * Throws @{@link IllegalArgumentException} if min value greater than max value.
+ *
+ * @throws IllegalStateException if min value greater than max value.
+ */
+ void validate();
+
+ /**
+ * Returns the min value of the range - the bottom boundary of the range
+ *
+ * @return the left boundary of the range
+ */
+ T from();
+
+ /**
+ * Adjust top boundary to specified value. Set top boundary to value
if it equals to the undefinedValue
.
+ *
+ * @param value the value for undefined boundary
+ */
+ T adjustTopBoundary(T value);
+
+ /**
+ * Adjust bottom boundary to specified value. Set bottom boundary to value
if it equals to the undefinedValue
.
+ *
+ * @param value the value for undefined boundary
+ */
+ T adjustBottomBoundary(T value);
+
+ /**
+ * Returns the max value of the range - the top boundary of the range
+ *
+ * @return the right boundary of the range
+ */
+ T to();
+
+ default T top() {
+ return to();
+ }
+
+ default T max() {
+ return to();
+ }
+}
diff --git a/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/api/Sort.java b/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/api/Sort.java
new file mode 100644
index 0000000000..5d8081a964
--- /dev/null
+++ b/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/api/Sort.java
@@ -0,0 +1,59 @@
+/*
+ * Copyright © 2018-2021 Apollo Foundation
+ */
+
+package com.apollocurrency.aplwallet.apl.util.api;
+
+/**
+ * @author Andrii Boiarskyi
+ * @see
+ * @since 1.50.31
+ */
+public class Sort {
+ public static final String ASC = "ASC";
+ public static final String DESC = "DESC";
+ private final String order;
+
+ public Sort(String order) {
+ this.order = order;
+ if (!isASC() && !isDESC()) {
+ throw new IllegalArgumentException("Not allowed sort value: " + order);
+ }
+ }
+
+ /**
+ * Returns the Sort object with default values for unknown ordering.
+ * The default value is ASC.
+ *
+ * @param order the given order
+ * @return the Sort object
+ */
+ public static Sort of(String order) {
+ if (ASC.equalsIgnoreCase(order) || DESC.equalsIgnoreCase(order)) {
+ return new Sort(order);
+ } else {
+ return desc();
+ }
+ }
+
+ public boolean isASC() {
+ return ASC.equalsIgnoreCase(order);
+ }
+
+ public boolean isDESC() {
+ return DESC.equalsIgnoreCase(order);
+ }
+
+ @Override
+ public String toString() {
+ return order;
+ }
+
+ public static Sort desc() {
+ return new Sort(DESC);
+ }
+
+ public static Sort asc() {
+ return new Sort(ASC);
+ }
+}
diff --git a/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/api/parameter/FirstLastIndexBeanParam.java b/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/api/parameter/FirstLastIndexBeanParam.java
index 8dab756acd..716aab0cfb 100644
--- a/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/api/parameter/FirstLastIndexBeanParam.java
+++ b/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/api/parameter/FirstLastIndexBeanParam.java
@@ -4,6 +4,7 @@
package com.apollocurrency.aplwallet.apl.util.api.parameter;
+import com.apollocurrency.aplwallet.apl.util.api.PositiveRange;
import io.swagger.v3.oas.annotations.Parameter;
import lombok.AllArgsConstructor;
import lombok.Getter;
@@ -27,7 +28,8 @@ public class FirstLastIndexBeanParam {
@PositiveOrZero
private int firstIndex = 0;
@Parameter(description = "A zero-based index to the last record ID to retrieve (optional).")
- @QueryParam("lastIndex") @DefaultValue("-1")
+ @QueryParam("lastIndex")
+ @DefaultValue("-1")
private int lastIndex = -1;
public void adjustIndexes(int maxAPIrecords) {
@@ -40,4 +42,7 @@ public void adjustIndexes(int maxAPIrecords) {
this.lastIndex = tempLastIndex;
}
+ public PositiveRange range() {
+ return new PositiveRange(firstIndex, lastIndex);
+ }
}
diff --git a/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/db/DbClause.java b/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/db/DbClause.java
index 1005375758..b956017c17 100644
--- a/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/db/DbClause.java
+++ b/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/db/DbClause.java
@@ -127,7 +127,7 @@ public static final class LikeClause extends DbClause {
public LikeClause(String columnName, String prefix) {
super(" " + columnName + " LIKE ? ");
- this.prefix = prefix.replace("%", "\\%").replace("_", "\\_") + '%';
+ this.prefix = DbUtils.escapeLikePattern(prefix);
}
@Override
diff --git a/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/db/DbUtils.java b/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/db/DbUtils.java
index c2bed678d7..16d0378121 100644
--- a/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/db/DbUtils.java
+++ b/apl-utils/src/main/java/com/apollocurrency/aplwallet/apl/util/db/DbUtils.java
@@ -19,6 +19,7 @@
package com.apollocurrency.aplwallet.apl.util.db;
+import com.apollocurrency.aplwallet.apl.util.api.PositiveRange;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
@@ -35,6 +36,7 @@
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
+import java.util.Objects;
import java.util.Optional;
import static org.slf4j.LoggerFactory.getLogger;
@@ -234,6 +236,10 @@ public static int calculateLimit(int from, int to) {
to - from + 1 : 0;
}
+ public static String limitsClause(PositiveRange paging) {
+ return limitsClause(paging.from(), paging.to());
+ }
+
public static String limitsClause(int from, int to) {
int limit = calculateLimit(from, to);
if (limit > 0 && from > 0) {
@@ -251,6 +257,10 @@ public static String limitsClause(int from, int to) {
}
}
+ public static int setLimits(int index, PreparedStatement pstmt, PositiveRange paging) throws SQLException {
+ return setLimits(index, pstmt, paging.from(), paging.to());
+ }
+
public static int setLimits(int index, PreparedStatement pstmt, int from, int to) throws SQLException {
int limit = calculateLimit(from, to);
if (limit > 0) {
@@ -262,4 +272,9 @@ public static int setLimits(int index, PreparedStatement pstmt, int from, int to
return index;
}
+ public static String escapeLikePattern(String pattern) {
+ Objects.requireNonNull(pattern, "pattern");
+ return pattern.replace("%", "\\%").replace("_", "\\_") + "%";
+ }
+
}