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') 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