From 5832e5270d5470b4b6a43a020a74e8a308d32529 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Wed, 5 Jul 2017 00:17:41 +0200 Subject: [PATCH] Some code improvement --- pom.xml | 2 +- src/main/java/fr/pandacube/java/util/BiMap.java | 4 ++++ .../packets/global/PacketD0ServerException.java | 4 ++-- .../pandacube/java/util/network/server/TCPServer.java | 5 +++-- .../util/network_api/server/NetworkAPIListener.java | 1 - .../java/fr/pandacube/java/util/orm/SQLElement.java | 2 +- .../fr/pandacube/java/util/orm/SQLElementList.java | 11 ++++------- 7 files changed, 15 insertions(+), 14 deletions(-) diff --git a/pom.xml b/pom.xml index d213002..85e2ca6 100644 --- a/pom.xml +++ b/pom.xml @@ -24,7 +24,7 @@ fr.pandacube.bungeecord bungeecord-chat - 1.11-SNAPSHOT + 1.12-SNAPSHOT compile diff --git a/src/main/java/fr/pandacube/java/util/BiMap.java b/src/main/java/fr/pandacube/java/util/BiMap.java index 18d5e0e..c4c0c30 100644 --- a/src/main/java/fr/pandacube/java/util/BiMap.java +++ b/src/main/java/fr/pandacube/java/util/BiMap.java @@ -57,5 +57,9 @@ public class BiMap implements Iterable> { c.accept(entry.getKey(), entry.getValue()); } } + + public int size() { + return map.size(); + } } diff --git a/src/main/java/fr/pandacube/java/util/network/packet/packets/global/PacketD0ServerException.java b/src/main/java/fr/pandacube/java/util/network/packet/packets/global/PacketD0ServerException.java index 5d9c4a3..af7cd29 100644 --- a/src/main/java/fr/pandacube/java/util/network/packet/packets/global/PacketD0ServerException.java +++ b/src/main/java/fr/pandacube/java/util/network/packet/packets/global/PacketD0ServerException.java @@ -25,9 +25,9 @@ public class PacketD0ServerException extends PacketServer { } - public void setException(Exception e) { + public void setException(Throwable t) { StringWriter sw = new StringWriter(); - e.printStackTrace(new PrintWriter(sw)); + t.printStackTrace(new PrintWriter(sw)); exception = sw.toString(); } diff --git a/src/main/java/fr/pandacube/java/util/network/server/TCPServer.java b/src/main/java/fr/pandacube/java/util/network/server/TCPServer.java index bbce74b..8aeb8ab 100644 --- a/src/main/java/fr/pandacube/java/util/network/server/TCPServer.java +++ b/src/main/java/fr/pandacube/java/util/network/server/TCPServer.java @@ -75,7 +75,6 @@ public class TCPServer extends Thread implements Closeable { try { while (true) { - @SuppressWarnings("resource") Socket socketClient = socket.accept(); socketClient.setSendBufferSize(Pandacube.NETWORK_TCP_BUFFER_SIZE); socketClient.setSoTimeout(Pandacube.NETWORK_TIMEOUT); @@ -156,11 +155,13 @@ public class TCPServer extends Thread implements Closeable { executeCallbacks(pc, callbacks); - } catch (Exception e) { + } catch (Throwable e) { Log.severe("Exception while handling packet. This exception will be sent to the client with PacketServerException packet.", e); PacketD0ServerException packet = new PacketD0ServerException(); packet.setException(e); send(packet); + if (e instanceof InterruptedException || e instanceof Error) + throw e; } } diff --git a/src/main/java/fr/pandacube/java/util/network_api/server/NetworkAPIListener.java b/src/main/java/fr/pandacube/java/util/network_api/server/NetworkAPIListener.java index a6b7ee6..25ce582 100644 --- a/src/main/java/fr/pandacube/java/util/network_api/server/NetworkAPIListener.java +++ b/src/main/java/fr/pandacube/java/util/network_api/server/NetworkAPIListener.java @@ -47,7 +47,6 @@ public class NetworkAPIListener implements Runnable { try { // réception des connexion client while (!serverSocket.isClosed()) { - @SuppressWarnings("resource") Socket socketClient = serverSocket.accept(); nAPIExecutionHandler.handleRun(new PacketExecutor(socketClient, this)); } diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLElement.java b/src/main/java/fr/pandacube/java/util/orm/SQLElement.java index 4653ad0..7ef42fb 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLElement.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLElement.java @@ -188,7 +188,7 @@ public abstract class SQLElement> { return modifiedSinceLastSave.contains(field.name); } - @SuppressWarnings({ "unchecked", "resource" }) + @SuppressWarnings("unchecked") public void save() throws ORMException { if (!isValidForSave()) throw new IllegalStateException(toString() + " has at least one undefined value and can't be saved."); diff --git a/src/main/java/fr/pandacube/java/util/orm/SQLElementList.java b/src/main/java/fr/pandacube/java/util/orm/SQLElementList.java index 36001c9..2ad97d6 100644 --- a/src/main/java/fr/pandacube/java/util/orm/SQLElementList.java +++ b/src/main/java/fr/pandacube/java/util/orm/SQLElementList.java @@ -9,6 +9,7 @@ import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import java.util.Set; +import java.util.stream.Collectors; import com.google.gson.JsonArray; @@ -45,10 +46,10 @@ public class SQLElementList> extends ArrayList { * @param value la valeur à lui appliquer */ public synchronized void setCommon(SQLField field, T value) { - if (field != null && field.name == "id") - throw new IllegalArgumentException("Can't modify id field in a SQLElementList"); if (field == null) throw new IllegalArgumentException("field can't be null"); + if (field.name == "id") + throw new IllegalArgumentException("Can't modify id field in a SQLElementList"); Class elemClass = field.getSQLElementType(); try { @@ -132,11 +133,7 @@ public class SQLElementList> extends ArrayList { } private List getStoredEl() { - List listStored = new ArrayList<>(); - forEach(el -> { - if (el.isStored()) listStored.add(el); - }); - return listStored; + return stream().filter(SQLElement::isStored).collect(Collectors.toCollection(() -> new ArrayList<>())); } public synchronized void removeFromDB() {