From d9d5e771057e4ac5f488144107c9870bce6d3d8c Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Sun, 16 Oct 2016 19:22:44 +0200 Subject: [PATCH] Corrections de bugs multiples - Correction de quelques NPE - Ajout d'une classe BiMap - Correctif mineure dans le code --- src/fr/pandacube/java/util/BiMap.java | 47 +++++++++++++++++++ .../java/util/ServerPropertyFile.java | 1 - .../java/util/commands/AbstractCommand.java | 5 +- .../java/util/network/server/TCPServer.java | 2 +- 4 files changed, 51 insertions(+), 4 deletions(-) create mode 100644 src/fr/pandacube/java/util/BiMap.java diff --git a/src/fr/pandacube/java/util/BiMap.java b/src/fr/pandacube/java/util/BiMap.java new file mode 100644 index 0000000..b78684f --- /dev/null +++ b/src/fr/pandacube/java/util/BiMap.java @@ -0,0 +1,47 @@ +package fr.pandacube.java.util; + +import java.util.HashMap; + +public class BiMap { + + HashMap map = new HashMap<>(); + HashMap inversedMap = new HashMap<>(); + + public synchronized void put(K k, V v) { + if (map.containsKey(k) || inversedMap.containsKey(v)) { + map.remove(k); + inversedMap.remove(v); + } + map.put(k, v); + inversedMap.put(v, k); + } + + public synchronized V get(K k) { + return map.get(k); + } + + public synchronized K getKey(V v) { + return inversedMap.get(v); + } + + public synchronized boolean containsKey(K k) { + return map.containsKey(k); + } + + public synchronized boolean containsValue(V v) { + return inversedMap.containsKey(v); + } + + public synchronized V remove(K k) { + V v = map.remove(k); + inversedMap.remove(v); + return v; + } + + public synchronized K removeValue(V v) { + K k = inversedMap.remove(v); + map.remove(k); + return k; + } + +} diff --git a/src/fr/pandacube/java/util/ServerPropertyFile.java b/src/fr/pandacube/java/util/ServerPropertyFile.java index 4a02f51..ad80c76 100644 --- a/src/fr/pandacube/java/util/ServerPropertyFile.java +++ b/src/fr/pandacube/java/util/ServerPropertyFile.java @@ -38,7 +38,6 @@ public class ServerPropertyFile { public boolean loadFromFile() { try (BufferedReader in = new BufferedReader(new FileReader(file))) { - @SuppressWarnings("unchecked") Map dataFile = new Gson().fromJson(in, Map.class); if (!dataFile.containsKey("name") || !(dataFile.get("name") instanceof String)) return false; diff --git a/src/fr/pandacube/java/util/commands/AbstractCommand.java b/src/fr/pandacube/java/util/commands/AbstractCommand.java index 1db6eaa..0e78024 100644 --- a/src/fr/pandacube/java/util/commands/AbstractCommand.java +++ b/src/fr/pandacube/java/util/commands/AbstractCommand.java @@ -50,13 +50,14 @@ public class AbstractCommand { * @return */ public static List getTabProposalFromToken(String token, Collection allProposal) { - List ret = new ArrayList(); + List ret = new ArrayList<>(); for (String s : allProposal) - if (s.toLowerCase().startsWith(token.toLowerCase())) ret.add(s); + if (s != null && s.toLowerCase().startsWith(token.toLowerCase())) ret.add(s); if (ret.isEmpty()) ret.addAll(allProposal); + ret.removeIf(s -> s == null); ret.sort(null); // String implents Comparable return ret; diff --git a/src/fr/pandacube/java/util/network/server/TCPServer.java b/src/fr/pandacube/java/util/network/server/TCPServer.java index b0178c3..3076712 100644 --- a/src/fr/pandacube/java/util/network/server/TCPServer.java +++ b/src/fr/pandacube/java/util/network/server/TCPServer.java @@ -294,7 +294,7 @@ public class TCPServer extends Thread implements Closeable { } } - } catch (InterruptedException e) {} catch (IOException e) {} + } catch (InterruptedException|IOException e) {} close(); }