aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/traccar/storage
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/java/org/traccar/storage')
-rw-r--r--src/main/java/org/traccar/storage/DatabaseStorage.java30
-rw-r--r--src/main/java/org/traccar/storage/MemoryStorage.java44
-rw-r--r--src/main/java/org/traccar/storage/QueryBuilder.java24
3 files changed, 39 insertions, 59 deletions
diff --git a/src/main/java/org/traccar/storage/DatabaseStorage.java b/src/main/java/org/traccar/storage/DatabaseStorage.java
index d20429319..1ff043e77 100644
--- a/src/main/java/org/traccar/storage/DatabaseStorage.java
+++ b/src/main/java/org/traccar/storage/DatabaseStorage.java
@@ -206,28 +206,23 @@ public class DatabaseStorage extends Storage {
private Map<String, Object> getConditionVariables(Condition genericCondition) {
Map<String, Object> results = new HashMap<>();
- if (genericCondition instanceof Condition.Compare) {
- var condition = (Condition.Compare) genericCondition;
+ if (genericCondition instanceof Condition.Compare condition) {
if (condition.getValue() != null) {
results.put(condition.getVariable(), condition.getValue());
}
- } else if (genericCondition instanceof Condition.Between) {
- var condition = (Condition.Between) genericCondition;
+ } else if (genericCondition instanceof Condition.Between condition) {
results.put(condition.getFromVariable(), condition.getFromValue());
results.put(condition.getToVariable(), condition.getToValue());
- } else if (genericCondition instanceof Condition.Binary) {
- var condition = (Condition.Binary) genericCondition;
+ } else if (genericCondition instanceof Condition.Binary condition) {
results.putAll(getConditionVariables(condition.getFirst()));
results.putAll(getConditionVariables(condition.getSecond()));
- } else if (genericCondition instanceof Condition.Permission) {
- var condition = (Condition.Permission) genericCondition;
+ } else if (genericCondition instanceof Condition.Permission condition) {
if (condition.getOwnerId() > 0) {
results.put(Permission.getKey(condition.getOwnerClass()), condition.getOwnerId());
} else {
results.put(Permission.getKey(condition.getPropertyClass()), condition.getPropertyId());
}
- } else if (genericCondition instanceof Condition.LatestPositions) {
- var condition = (Condition.LatestPositions) genericCondition;
+ } else if (genericCondition instanceof Condition.LatestPositions condition) {
if (condition.getDeviceId() > 0) {
results.put("deviceId", condition.getDeviceId());
}
@@ -249,43 +244,38 @@ public class DatabaseStorage extends Storage {
if (appendWhere) {
result.append(" WHERE ");
}
- if (genericCondition instanceof Condition.Compare) {
+ if (genericCondition instanceof Condition.Compare condition) {
- var condition = (Condition.Compare) genericCondition;
result.append(condition.getColumn());
result.append(" ");
result.append(condition.getOperator());
result.append(" :");
result.append(condition.getVariable());
- } else if (genericCondition instanceof Condition.Between) {
+ } else if (genericCondition instanceof Condition.Between condition) {
- var condition = (Condition.Between) genericCondition;
result.append(condition.getColumn());
result.append(" BETWEEN :");
result.append(condition.getFromVariable());
result.append(" AND :");
result.append(condition.getToVariable());
- } else if (genericCondition instanceof Condition.Binary) {
+ } else if (genericCondition instanceof Condition.Binary condition) {
- var condition = (Condition.Binary) genericCondition;
result.append(formatCondition(condition.getFirst(), false));
result.append(" ");
result.append(condition.getOperator());
result.append(" ");
result.append(formatCondition(condition.getSecond(), false));
- } else if (genericCondition instanceof Condition.Permission) {
+ } else if (genericCondition instanceof Condition.Permission condition) {
- var condition = (Condition.Permission) genericCondition;
result.append("id IN (");
result.append(formatPermissionQuery(condition));
result.append(")");
- } else if (genericCondition instanceof Condition.LatestPositions) {
+ } else if (genericCondition instanceof Condition.LatestPositions condition) {
- var condition = (Condition.LatestPositions) genericCondition;
result.append("id IN (");
result.append("SELECT positionId FROM ");
result.append(getStorageName(Device.class));
diff --git a/src/main/java/org/traccar/storage/MemoryStorage.java b/src/main/java/org/traccar/storage/MemoryStorage.java
index 9b5db1209..1ec8bfe57 100644
--- a/src/main/java/org/traccar/storage/MemoryStorage.java
+++ b/src/main/java/org/traccar/storage/MemoryStorage.java
@@ -60,54 +60,44 @@ public class MemoryStorage extends Storage {
return true;
}
- if (genericCondition instanceof Condition.Compare) {
+ if (genericCondition instanceof Condition.Compare condition) {
- var condition = (Condition.Compare) genericCondition;
Object value = retrieveValue(object, condition.getVariable());
int result = ((Comparable) value).compareTo(condition.getValue());
- switch (condition.getOperator()) {
- case "<":
- return result < 0;
- case "<=":
- return result <= 0;
- case ">":
- return result > 0;
- case ">=":
- return result >= 0;
- case "=":
- return result == 0;
- default:
- throw new RuntimeException("Unsupported comparison condition");
- }
+ return switch (condition.getOperator()) {
+ case "<" -> result < 0;
+ case "<=" -> result <= 0;
+ case ">" -> result > 0;
+ case ">=" -> result >= 0;
+ case "=" -> result == 0;
+ default -> throw new RuntimeException("Unsupported comparison condition");
+ };
- } else if (genericCondition instanceof Condition.Between) {
+ } else if (genericCondition instanceof Condition.Between condition) {
- var condition = (Condition.Between) genericCondition;
Object fromValue = retrieveValue(object, condition.getFromVariable());
int fromResult = ((Comparable) fromValue).compareTo(condition.getFromValue());
Object toValue = retrieveValue(object, condition.getToVariable());
int toResult = ((Comparable) toValue).compareTo(condition.getToValue());
return fromResult >= 0 && toResult <= 0;
- } else if (genericCondition instanceof Condition.Binary) {
+ } else if (genericCondition instanceof Condition.Binary condition) {
- var condition = (Condition.Binary) genericCondition;
if (condition.getOperator().equals("AND")) {
return checkCondition(condition.getFirst(), object) && checkCondition(condition.getSecond(), object);
} else if (condition.getOperator().equals("OR")) {
return checkCondition(condition.getFirst(), object) || checkCondition(condition.getSecond(), object);
}
- } else if (genericCondition instanceof Condition.Permission) {
+ } else if (genericCondition instanceof Condition.Permission condition) {
- var condition = (Condition.Permission) genericCondition;
long id = (Long) retrieveValue(object, "id");
return getPermissionsSet(condition.getOwnerClass(), condition.getPropertyClass()).stream()
.anyMatch(pair -> {
if (condition.getOwnerId() > 0) {
- return pair.getFirst() == condition.getOwnerId() && pair.getSecond() == id;
+ return pair.first() == condition.getOwnerId() && pair.second() == id;
} else {
- return pair.getFirst() == id && pair.getSecond() == condition.getPropertyId();
+ return pair.first() == id && pair.second() == condition.getPropertyId();
}
});
@@ -178,9 +168,9 @@ public class MemoryStorage extends Storage {
Class<? extends BaseModel> ownerClass, long ownerId,
Class<? extends BaseModel> propertyClass, long propertyId) {
return getPermissionsSet(ownerClass, propertyClass).stream()
- .filter(pair -> ownerId == 0 || pair.getFirst().equals(ownerId))
- .filter(pair -> propertyId == 0 || pair.getSecond().equals(propertyId))
- .map(pair -> new Permission(ownerClass, pair.getFirst(), propertyClass, pair.getSecond()))
+ .filter(pair -> ownerId == 0 || pair.first().equals(ownerId))
+ .filter(pair -> propertyId == 0 || pair.second().equals(propertyId))
+ .map(pair -> new Permission(ownerClass, pair.first(), propertyClass, pair.second()))
.collect(Collectors.toList());
}
diff --git a/src/main/java/org/traccar/storage/QueryBuilder.java b/src/main/java/org/traccar/storage/QueryBuilder.java
index 2f4c07406..6a9c46807 100644
--- a/src/main/java/org/traccar/storage/QueryBuilder.java
+++ b/src/main/java/org/traccar/storage/QueryBuilder.java
@@ -267,18 +267,18 @@ public final class QueryBuilder {
}
public QueryBuilder setValue(String name, Object value) throws SQLException {
- if (value instanceof Boolean) {
- setBoolean(name, (Boolean) value);
- } else if (value instanceof Integer) {
- setInteger(name, (Integer) value);
- } else if (value instanceof Long) {
- setLong(name, (Long) value);
- } else if (value instanceof Double) {
- setDouble(name, (Double) value);
- } else if (value instanceof String) {
- setString(name, (String) value);
- } else if (value instanceof Date) {
- setDate(name, (Date) value);
+ if (value instanceof Boolean booleanValue) {
+ setBoolean(name, booleanValue);
+ } else if (value instanceof Integer integerValue) {
+ setInteger(name, integerValue);
+ } else if (value instanceof Long longValue) {
+ setLong(name, longValue);
+ } else if (value instanceof Double doubleValue) {
+ setDouble(name, doubleValue);
+ } else if (value instanceof String stringValue) {
+ setString(name, stringValue);
+ } else if (value instanceof Date dateValue) {
+ setDate(name, dateValue);
}
return this;
}