From b8390005722dd1cdb24d762797ef3f98ebc37755 Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sat, 6 Jul 2024 17:46:17 -0700 Subject: Update instanceof expressions --- .../java/org/traccar/storage/DatabaseStorage.java | 30 +++++++------------ .../java/org/traccar/storage/MemoryStorage.java | 34 ++++++++-------------- .../java/org/traccar/storage/QueryBuilder.java | 24 +++++++-------- 3 files changed, 34 insertions(+), 54 deletions(-) (limited to 'src/main/java/org/traccar/storage') 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 getConditionVariables(Condition genericCondition) { Map 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..18cb6a294 100644 --- a/src/main/java/org/traccar/storage/MemoryStorage.java +++ b/src/main/java/org/traccar/storage/MemoryStorage.java @@ -60,47 +60,37 @@ 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 -> { 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; } -- cgit v1.2.3 From cf6aa038b7514201ffb89fa26a4ba7d921b8f16f Mon Sep 17 00:00:00 2001 From: Anton Tananaev Date: Sun, 7 Jul 2024 12:48:35 -0700 Subject: Migrate to records --- .../traccar/api/resource/NotificationResource.java | 4 +- src/main/java/org/traccar/helper/Log.java | 4 +- src/main/java/org/traccar/model/Pair.java | 56 +++++++--------------- src/main/java/org/traccar/model/Typed.java | 36 +------------- .../java/org/traccar/session/ConnectionKey.java | 30 +----------- .../java/org/traccar/session/cache/CacheGraph.java | 2 +- .../java/org/traccar/session/cache/CacheKey.java | 41 +--------------- .../java/org/traccar/storage/MemoryStorage.java | 10 ++-- 8 files changed, 31 insertions(+), 152 deletions(-) (limited to 'src/main/java/org/traccar/storage') diff --git a/src/main/java/org/traccar/api/resource/NotificationResource.java b/src/main/java/org/traccar/api/resource/NotificationResource.java index a41d00cf3..8bc81a3a4 100644 --- a/src/main/java/org/traccar/api/resource/NotificationResource.java +++ b/src/main/java/org/traccar/api/resource/NotificationResource.java @@ -85,7 +85,7 @@ public class NotificationResource extends ExtendedObjectResource { public Collection getNotificators(@QueryParam("announcement") boolean announcement) { Set announcementsUnsupported = Set.of("command", "web"); return notificatorManager.getAllNotificatorTypes().stream() - .filter(typed -> !announcement || !announcementsUnsupported.contains(typed.getType())) + .filter(typed -> !announcement || !announcementsUnsupported.contains(typed.type())) .collect(Collectors.toUnmodifiableSet()); } @@ -94,7 +94,7 @@ public class NotificationResource extends ExtendedObjectResource { public Response testMessage() throws MessageException, StorageException { User user = permissionsService.getUser(getUserId()); for (Typed method : notificatorManager.getAllNotificatorTypes()) { - notificatorManager.getNotificator(method.getType()).send(null, user, new Event("test", 0), null); + notificatorManager.getNotificator(method.type()).send(null, user, new Event("test", 0), null); } return Response.noContent().build(); } diff --git a/src/main/java/org/traccar/helper/Log.java b/src/main/java/org/traccar/helper/Log.java index c68198542..d01d70559 100644 --- a/src/main/java/org/traccar/helper/Log.java +++ b/src/main/java/org/traccar/helper/Log.java @@ -284,8 +284,8 @@ public final class Log { } } return stores.stream() - .sorted(Comparator.comparingDouble(p -> p.getFirst() / (double) p.getSecond())) - .flatMap(p -> Stream.of(p.getFirst(), p.getSecond())) + .sorted(Comparator.comparingDouble(p -> p.first() / (double) p.second())) + .flatMap(p -> Stream.of(p.first(), p.second())) .mapToLong(Long::longValue) .toArray(); } diff --git a/src/main/java/org/traccar/model/Pair.java b/src/main/java/org/traccar/model/Pair.java index b679de57b..90b82d577 100644 --- a/src/main/java/org/traccar/model/Pair.java +++ b/src/main/java/org/traccar/model/Pair.java @@ -1,43 +1,19 @@ +/* + * Copyright 2024 Anton Tananaev (anton@traccar.org) + * + * 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 org.traccar.model; -import java.util.Objects; - -public class Pair { - - private final K first; - private final V second; - - public Pair(K first, V second) { - this.first = first; - this.second = second; - } - - public K getFirst() { - return first; - } - - public V getSecond() { - return second; - } - - @SuppressWarnings("rawtypes") - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - - Pair pair = (Pair) o; - - return Objects.equals(first, pair.first) && Objects.equals(second, pair.second); - } - - @Override - public int hashCode() { - return Objects.hash(first, second); - } - +public record Pair(K first, V second) { } diff --git a/src/main/java/org/traccar/model/Typed.java b/src/main/java/org/traccar/model/Typed.java index fc671ac70..0476fdc4b 100644 --- a/src/main/java/org/traccar/model/Typed.java +++ b/src/main/java/org/traccar/model/Typed.java @@ -1,5 +1,5 @@ /* - * Copyright 2020 Anton Tananaev (anton@traccar.org) + * Copyright 2020 - 2024 Anton Tananaev (anton@traccar.org) * Copyright 2016 Gabor Somogyi (gabor.g.somogyi@gmail.com) * * Licensed under the Apache License, Version 2.0 (the "License"); @@ -16,37 +16,5 @@ */ package org.traccar.model; -import java.util.Objects; - -public class Typed { - - private String type; - - public Typed(String type) { - this.type = type; - } - - public String getType() { - return type; - } - - public void setType(String type) { - this.type = type; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - return Objects.equals(type, ((Typed) o).type); - } - - @Override - public int hashCode() { - return Objects.hash(type); - } +public record Typed(String type) { } diff --git a/src/main/java/org/traccar/session/ConnectionKey.java b/src/main/java/org/traccar/session/ConnectionKey.java index 3b7e2ebf8..807e68776 100644 --- a/src/main/java/org/traccar/session/ConnectionKey.java +++ b/src/main/java/org/traccar/session/ConnectionKey.java @@ -18,37 +18,9 @@ package org.traccar.session; import io.netty.channel.Channel; import java.net.SocketAddress; -import java.util.Objects; - -public class ConnectionKey { - - private final SocketAddress localAddress; - private final SocketAddress remoteAddress; +public record ConnectionKey(SocketAddress localAddress, SocketAddress remoteAddress) { public ConnectionKey(Channel channel, SocketAddress remoteAddress) { this(channel.localAddress(), remoteAddress); } - - public ConnectionKey(SocketAddress localAddress, SocketAddress remoteAddress) { - this.localAddress = localAddress; - this.remoteAddress = remoteAddress; - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - ConnectionKey that = (ConnectionKey) o; - return Objects.equals(localAddress, that.localAddress) && Objects.equals(remoteAddress, that.remoteAddress); - } - - @Override - public int hashCode() { - return Objects.hash(localAddress, remoteAddress); - } - } diff --git a/src/main/java/org/traccar/session/cache/CacheGraph.java b/src/main/java/org/traccar/session/cache/CacheGraph.java index c99997288..a4c2ce61b 100644 --- a/src/main/java/org/traccar/session/cache/CacheGraph.java +++ b/src/main/java/org/traccar/session/cache/CacheGraph.java @@ -38,7 +38,7 @@ public class CacheGraph { CacheKey key = new CacheKey(clazz, id); CacheNode node = nodes.remove(key); if (node != null) { - node.getAllLinks(false).forEach(child -> child.getLinks(key.getClazz(), true).remove(node)); + node.getAllLinks(false).forEach(child -> child.getLinks(key.clazz(), true).remove(node)); } roots.remove(key); } diff --git a/src/main/java/org/traccar/session/cache/CacheKey.java b/src/main/java/org/traccar/session/cache/CacheKey.java index f27d5fbf5..001058e4c 100644 --- a/src/main/java/org/traccar/session/cache/CacheKey.java +++ b/src/main/java/org/traccar/session/cache/CacheKey.java @@ -1,5 +1,5 @@ /* - * Copyright 2022 Anton Tananaev (anton@traccar.org) + * Copyright 2022 - 2024 Anton Tananaev (anton@traccar.org) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,45 +17,8 @@ package org.traccar.session.cache; import org.traccar.model.BaseModel; -import java.util.Objects; - -class CacheKey { - - private final Class clazz; - private final long id; - +record CacheKey(Class clazz, long id) { CacheKey(BaseModel object) { this(object.getClass(), object.getId()); } - - CacheKey(Class clazz, long id) { - this.clazz = clazz; - this.id = id; - } - - public Class getClazz() { - return clazz; - } - - public boolean classIs(Class clazz) { - return clazz.equals(this.clazz); - } - - @Override - public boolean equals(Object o) { - if (this == o) { - return true; - } - if (o == null || getClass() != o.getClass()) { - return false; - } - CacheKey cacheKey = (CacheKey) o; - return id == cacheKey.id && Objects.equals(clazz, cacheKey.clazz); - } - - @Override - public int hashCode() { - return Objects.hash(clazz, id); - } - } diff --git a/src/main/java/org/traccar/storage/MemoryStorage.java b/src/main/java/org/traccar/storage/MemoryStorage.java index 18cb6a294..1ec8bfe57 100644 --- a/src/main/java/org/traccar/storage/MemoryStorage.java +++ b/src/main/java/org/traccar/storage/MemoryStorage.java @@ -95,9 +95,9 @@ public class MemoryStorage extends Storage { 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(); } }); @@ -168,9 +168,9 @@ public class MemoryStorage extends Storage { Class ownerClass, long ownerId, Class 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()); } -- cgit v1.2.3