From e7e56334349572f50dffd680bc434638608250a7 Mon Sep 17 00:00:00 2001 From: Mark Nellemann Date: Thu, 10 Aug 2023 14:33:09 +0200 Subject: [PATCH] Improve equals --- build.gradle | 2 +- .../nellemann/mdexpl/NetworkServiceCell.java | 2 ++ .../mdexpl/NetworkServiceListener.java | 18 ++++++++++------- .../mdexpl/model/NetworkService.java | 20 +++++++++++++++---- .../mdexpl/service/DiscoveryService.java | 3 +++ 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/build.gradle b/build.gradle index 67b8839..088bbbb 100644 --- a/build.gradle +++ b/build.gradle @@ -94,7 +94,7 @@ gluonfx { //providedKeyAlias = "" //providedKeyAliasPassword = "" // iOS - //bundleName = "" + //bundleName = "mDNS Explorer" //bundleVersion = "" //bundleShortVersion = "" //providedSigningIdentity = "" diff --git a/src/main/java/biz/nellemann/mdexpl/NetworkServiceCell.java b/src/main/java/biz/nellemann/mdexpl/NetworkServiceCell.java index 0276316..40118ed 100644 --- a/src/main/java/biz/nellemann/mdexpl/NetworkServiceCell.java +++ b/src/main/java/biz/nellemann/mdexpl/NetworkServiceCell.java @@ -18,6 +18,7 @@ public class NetworkServiceCell extends CharmListCell { private final Clipboard clipboard; private final ClipboardContent clipboardContent; + public NetworkServiceCell() { clipboard = Clipboard.getSystemClipboard(); clipboardContent = new ClipboardContent(); @@ -40,6 +41,7 @@ public class NetworkServiceCell extends CharmListCell { setText(null); } + @Override public void updateItem(NetworkService item, boolean empty) { super.updateItem(item, empty); diff --git a/src/main/java/biz/nellemann/mdexpl/NetworkServiceListener.java b/src/main/java/biz/nellemann/mdexpl/NetworkServiceListener.java index 1f73ac4..f817e5e 100644 --- a/src/main/java/biz/nellemann/mdexpl/NetworkServiceListener.java +++ b/src/main/java/biz/nellemann/mdexpl/NetworkServiceListener.java @@ -29,6 +29,8 @@ public class NetworkServiceListener implements ServiceListener { @Override public void serviceAdded(ServiceEvent event) { + ServiceInfo serviceInfo = event.getInfo(); + log.info("serviceRemoved() - Service: " + serviceInfo.getName()); } @Override @@ -37,10 +39,12 @@ public class NetworkServiceListener implements ServiceListener { if (serviceInfo != null) { String name = serviceInfo.getName(); log.info("serviceRemoved() - Service: " + name); - NetworkService oldNetworkService = new NetworkService(name, service, serviceInfo.getSubtype(), serviceInfo.getApplication(), serviceInfo.getURLs()[0], color); - Platform.runLater(() -> { - observableList.remove(oldNetworkService); - }); + NetworkService networkService = new NetworkService(name, service, serviceInfo.getSubtype(), serviceInfo.getApplication(), serviceInfo.getURLs()[0], color); + while (observableList.contains(networkService)) { + Platform.runLater(() -> { + observableList.remove(networkService); + }); + } } } @@ -52,10 +56,10 @@ public class NetworkServiceListener implements ServiceListener { String name = serviceInfo.getName(); String app = serviceInfo.getApplication(); log.info("serviceResolved() - Service: {} - {} with url {}", app, name, url); - NetworkService newNetworkService = new NetworkService(name, service, serviceInfo.getSubtype(), app, url, color); + NetworkService networkService = new NetworkService(name, service, serviceInfo.getSubtype(), app, url, color); Platform.runLater(() -> { - if(!observableList.contains(newNetworkService)) { - observableList.add(newNetworkService); + if(!observableList.contains(networkService)) { + observableList.add(networkService); } }); } diff --git a/src/main/java/biz/nellemann/mdexpl/model/NetworkService.java b/src/main/java/biz/nellemann/mdexpl/model/NetworkService.java index bdee01c..b148c0a 100644 --- a/src/main/java/biz/nellemann/mdexpl/model/NetworkService.java +++ b/src/main/java/biz/nellemann/mdexpl/model/NetworkService.java @@ -2,6 +2,8 @@ package biz.nellemann.mdexpl.model; import javafx.scene.paint.Color; +import java.util.Objects; + public class NetworkService { private String name; @@ -75,13 +77,23 @@ public class NetworkService { return name + " (" + type + "), app=" + app + ", url=" + url; } + @Override public boolean equals(Object o) { - if (o == this) return true; - if (!(o instanceof NetworkService networkService)) { + if (this == o) + return true; + if (o == null || getClass() != o.getClass()) return false; - } - return networkService.name.equals(name) && networkService.type.equals(type) && networkService.url.equals(url); + NetworkService networkService = (NetworkService) o; + return Objects.equals(type, networkService.type) && Objects.equals(name, networkService.name) && Objects.equals(url, networkService.url); + } + + @Override + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + ((url == null) ? 0 : url.hashCode()); + return result; } } diff --git a/src/main/java/biz/nellemann/mdexpl/service/DiscoveryService.java b/src/main/java/biz/nellemann/mdexpl/service/DiscoveryService.java index e17da8a..80023b5 100644 --- a/src/main/java/biz/nellemann/mdexpl/service/DiscoveryService.java +++ b/src/main/java/biz/nellemann/mdexpl/service/DiscoveryService.java @@ -51,6 +51,7 @@ public class DiscoveryService { put("ipp", Color.LIGHTGRAY); put("ipps", Color.LIGHTGRAY); put("printer", Color.LIGHTGRAY); + put("scanner", Color.LIGHTGRAY); put("nfs", Color.CORAL); put("smb", Color.CORAL); @@ -58,7 +59,9 @@ public class DiscoveryService { put("webdav", Color.CORAL); put("smartenergy", Color.LIGHTGREEN); + put("hap", Color.LIGHTGREEN); put("homekit", Color.LIGHTGREEN); + put("homebridge", Color.LIGHTGREEN); put("sip", Color.YELLOW); put("skype", Color.YELLOW);