From f01a56794c32d0bef4ecce447f93afad407bd700 Mon Sep 17 00:00:00 2001 From: Britta Heymann <britta_hey@web.de> Date: Wed, 8 Feb 2017 17:58:11 +0100 Subject: [PATCH] Fix some SonarLint issues refs #40 --- src/core/OneToOneMessage.java | 8 ++-- src/report/DistanceDelayReport.java | 11 +++--- src/routing/ActiveRouter.java | 4 +- src/routing/EpidemicOracleRouter.java | 8 ++-- src/routing/MaxPropRouter.java | 24 ++++++------ src/routing/MaxPropRouterWithEstimation.java | 24 ++++++------ src/routing/ProphetRouter.java | 10 ++--- src/routing/ProphetRouterWithEstimation.java | 14 +++---- src/routing/ProphetV2Router.java | 11 +++--- .../util/MessageTransferAcceptPolicy.java | 38 +++++++++---------- 10 files changed, 74 insertions(+), 78 deletions(-) diff --git a/src/core/OneToOneMessage.java b/src/core/OneToOneMessage.java index 0a891d96..34e282ca 100644 --- a/src/core/OneToOneMessage.java +++ b/src/core/OneToOneMessage.java @@ -14,9 +14,9 @@ public class OneToOneMessage extends Message implements ISpecificRecipientsMessa /** * Creates a new OneToOneMessage. * @param from Who the message is (originally) from - * @param to Who the message is (originally) to - * @param id Message identifier (must be unique for message but - * will be the same for all replicates of the message) + * @param to Who the message is (originally) to + * @param id Message identifier (must be unique for message but + * will be the same for all replicates of the message) * @param size Size of the message (in bytes) */ public OneToOneMessage(DTNHost from, DTNHost to, String id, int size) { @@ -31,7 +31,7 @@ public class OneToOneMessage extends Message implements ISpecificRecipientsMessa */ @Override public boolean isFinalRecipient(DTNHost host) { - return this.to == host; + return this.to.equals(host); } /** diff --git a/src/report/DistanceDelayReport.java b/src/report/DistanceDelayReport.java index f3b49269..37c980ee 100644 --- a/src/report/DistanceDelayReport.java +++ b/src/report/DistanceDelayReport.java @@ -5,7 +5,6 @@ package report; import java.util.HashMap; -import java.util.List; import javafx.util.Pair; @@ -69,12 +68,12 @@ public class DistanceDelayReport extends Report implements MessageListener { } if (!(m instanceof ISpecificRecipientsMessage)) { - return; + return; } ISpecificRecipientsMessage msg = (ISpecificRecipientsMessage)m; for(DTNHost node : msg.getFinalRecipients()) { - this.creationInfos.put(new Pair<String, Integer>(m.getId(), node.getAddress()), + this.creationInfos.put(new Pair(m.getId(), node.getAddress()), new InfoTuple(getSimTime(), m.getFrom().getLocation().clone(), node.getLocation().clone()) ); @@ -111,9 +110,9 @@ public class DistanceDelayReport extends Report implements MessageListener { public void done() { // report rest of the messages as 'not delivered' (time == -1) - for (Pair<String, Integer> messageIdAndDestination : creationInfos.keySet()) { - InfoTuple info = creationInfos.get(messageIdAndDestination); - report( + for (Pair<String, Integer> messageIdAndDestination : creationInfos.keySet()) { + InfoTuple info = creationInfos.get(messageIdAndDestination); + report( messageIdAndDestination.getKey(), info.getLoc1().distance(info.getLoc2()), -1, diff --git a/src/routing/ActiveRouter.java b/src/routing/ActiveRouter.java index 54817c15..98de683f 100644 --- a/src/routing/ActiveRouter.java +++ b/src/routing/ActiveRouter.java @@ -112,7 +112,7 @@ public abstract class ActiveRouter extends MessageRouter { ArrayList<Message> temp = new ArrayList<Message>(this.getMessageCollection()); for (Message m : temp) { - if (m.isFinalRecipient(other)) { + if (m.isFinalRecipient(other)) { if (startTransfer(m, con) == RCV_OK) { return true; } @@ -150,7 +150,7 @@ public abstract class ActiveRouter extends MessageRouter { // check if msg was for this host and a response was requested if (m.isFinalRecipient(getHost()) && m.getResponseSize() > 0) { // generate a response message - Message res = new OneToOneMessage(this.getHost(),m.getFrom(), + Message res = new OneToOneMessage(this.getHost(),m.getFrom(), RESPONSE_PREFIX+m.getId(), m.getResponseSize()); this.createNewMessage(res); this.getMessage(RESPONSE_PREFIX+m.getId()).setRequest(m); diff --git a/src/routing/EpidemicOracleRouter.java b/src/routing/EpidemicOracleRouter.java index a6272a5d..c00cb5e8 100644 --- a/src/routing/EpidemicOracleRouter.java +++ b/src/routing/EpidemicOracleRouter.java @@ -116,7 +116,7 @@ public class EpidemicOracleRouter extends ActiveRouter { if (m.isFinalRecipient(this.getHost())) { for (EpidemicOracleRouter r : allRouters) { if (r != this && r != from.getRouter()) { - this.ensureMessageHasSingleRecipient(m); + EpidemicOracleRouter.ensureMessageHasSingleRecipient(m); r.removeDeliveredMessage(id); } } @@ -157,7 +157,7 @@ public class EpidemicOracleRouter extends ActiveRouter { /* was the message delivered to the final recipient? */ if (m.isFinalRecipient(con.getOtherNode(getHost()))) { - this.ensureMessageHasSingleRecipient(m); + EpidemicOracleRouter.ensureMessageHasSingleRecipient(m); this.deleteMessage(m.getId(), false); } } @@ -166,8 +166,8 @@ public class EpidemicOracleRouter extends ActiveRouter { * Ensures that the given message only has a single recipient. * @param msg Message to check. */ - private void ensureMessageHasSingleRecipient(Message msg) { - assert msg instanceof OneToOneMessage : + private static void ensureMessageHasSingleRecipient(Message msg) { + assert msg instanceof OneToOneMessage : "EpidemicOracleRouter only works with one to one messages"; } diff --git a/src/routing/MaxPropRouter.java b/src/routing/MaxPropRouter.java index 93f2014f..6d2a5dbd 100644 --- a/src/routing/MaxPropRouter.java +++ b/src/routing/MaxPropRouter.java @@ -217,7 +217,7 @@ public class MaxPropRouter extends ActiveRouter { /* was the message delivered to the final recipient? */ if (m.isFinalRecipient(recipient)) { - this.ensureMessageHasSingleRecipient(m); + MaxPropRouter.ensureMessageHasSingleRecipient(m); this.ackedMessageIds.add(m.getId()); // yes, add to ACKed messages this.deleteMessage(m.getId(), false); // delete from buffer } @@ -278,7 +278,7 @@ public class MaxPropRouter extends ActiveRouter { if (excludeMsgBeingSent && isSending(m.getId())) { continue; // skip the message(s) that router is sending } - validMessages.add(this.castMessageToOneToOne(m)); + validMessages.add(MaxPropRouter.castMessageToOneToOne(m)); } Collections.sort(validMessages, @@ -323,8 +323,8 @@ public class MaxPropRouter extends ActiveRouter { * (optimization) */ Set<Integer> toSet = new HashSet<Integer>(); for (Message m : getMessageCollection()) { - OneToOneMessage singleRecipientMessage = this.castMessageToOneToOne(m); - toSet.add(singleRecipientMessage.getTo().getAddress()); + OneToOneMessage singleRecipientMessage = MaxPropRouter.castMessageToOneToOne(m); + toSet.add(singleRecipientMessage.getTo().getAddress()); } this.costsForMessages = dijkstra.getCosts(fromIndex, toSet); @@ -346,7 +346,7 @@ public class MaxPropRouter extends ActiveRouter { * @return The return value of {@link #tryMessagesForConnected(List)} */ private Tuple<? extends Message, Connection> tryOtherMessages() { - List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); + List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); Collection<Message> msgCollection = getMessageCollection(); @@ -374,7 +374,7 @@ public class MaxPropRouter extends ActiveRouter { continue; } /* message was a good candidate for sending */ - messages.add(new Tuple(this.castMessageToOneToOne(m),con)); + messages.add(new Tuple(MaxPropRouter.castMessageToOneToOne(m),con)); } } @@ -454,18 +454,18 @@ public class MaxPropRouter extends ActiveRouter { * @return Message as OneToOneMessage. * @throws AssertionError if message is not a OneToOneMessage. */ - private OneToOneMessage castMessageToOneToOne(Message msg) { - assert msg instanceof OneToOneMessage : + private static OneToOneMessage castMessageToOneToOne(Message msg) { + assert msg instanceof OneToOneMessage : "MaxPropRouter only works with one to one messages"; - return (OneToOneMessage) msg; + return (OneToOneMessage) msg; } /** * Ensures that the given message only has a single recipient. * @param msg Message to check. */ - private void ensureMessageHasSingleRecipient(Message msg) { - assert msg instanceof OneToOneMessage : + private static void ensureMessageHasSingleRecipient(Message msg) { + assert msg instanceof OneToOneMessage : "MaxPropRouter only works with one to one messages"; } @@ -475,7 +475,7 @@ public class MaxPropRouter extends ActiveRouter { * threshold are given priority and they are ordered by their hop count. * Other messages are ordered by their delivery cost. */ - private class MaxPropComparator implements Comparator<OneToOneMessage> { + private class MaxPropComparator implements Comparator<OneToOneMessage> { private int threshold; private DTNHost from1; private DTNHost from2; diff --git a/src/routing/MaxPropRouterWithEstimation.java b/src/routing/MaxPropRouterWithEstimation.java index 1f36f0ec..dee49a8a 100644 --- a/src/routing/MaxPropRouterWithEstimation.java +++ b/src/routing/MaxPropRouterWithEstimation.java @@ -353,7 +353,7 @@ public class MaxPropRouterWithEstimation extends ActiveRouter { Message m = con.getMessage(); /* was the message delivered to the final recipient? */ if (m.isFinalRecipient(con.getOtherNode(getHost()))) { - this.ensureMessageHasSingleRecipient(m); + MaxPropRouterWithEstimation.ensureMessageHasSingleRecipient(m); this.ackedMessageIds.add(m.getId()); // yes, add to ACKed messages this.deleteMessage(m.getId(), false); // delete from buffer } @@ -406,7 +406,7 @@ public class MaxPropRouterWithEstimation extends ActiveRouter { if (excludeMsgBeingSent && isSending(m.getId())) { continue; // skip the message(s) that router is sending } - validMessages.add(this.castMessageToOneToOne(m)); + validMessages.add(MaxPropRouterWithEstimation.castMessageToOneToOne(m)); } Collections.sort(validMessages, @@ -451,8 +451,8 @@ public class MaxPropRouterWithEstimation extends ActiveRouter { * (optimization) */ Set<Integer> toSet = new HashSet<Integer>(); for (Message m : getMessageCollection()) { - OneToOneMessage singleRecipientMessage = this.castMessageToOneToOne(m); - toSet.add(singleRecipientMessage.getTo().getAddress()); + OneToOneMessage singleRecipientMessage = MaxPropRouterWithEstimation.castMessageToOneToOne(m); + toSet.add(singleRecipientMessage.getTo().getAddress()); } this.costsForMessages = dijkstra.getCosts(fromIndex, toSet); @@ -474,7 +474,7 @@ public class MaxPropRouterWithEstimation extends ActiveRouter { * @return The return value of {@link #tryMessagesForConnected(List)} */ private Tuple<? extends Message, Connection> tryOtherMessages() { - List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); + List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); Collection<Message> msgCollection = getMessageCollection(); @@ -495,7 +495,7 @@ public class MaxPropRouterWithEstimation extends ActiveRouter { m.getHops().contains(other)) { continue; } - messages.add(new Tuple<OneToOneMessage, Connection>(this.castMessageToOneToOne(m),con)); + messages.add(new Tuple<>(MaxPropRouterWithEstimation.castMessageToOneToOne(m),con)); } } @@ -572,18 +572,18 @@ public class MaxPropRouterWithEstimation extends ActiveRouter { * @return Message as OneToOneMessage. * @throws AssertionError if message is not a OneToOneMessage. */ - private OneToOneMessage castMessageToOneToOne(Message msg) { - assert msg instanceof OneToOneMessage : + private static OneToOneMessage castMessageToOneToOne(Message msg) { + assert msg instanceof OneToOneMessage : "MaxPropRouterWithEstimation only works with one to one messages"; - return (OneToOneMessage) msg; + return (OneToOneMessage) msg; } /** * Ensures that the given message only has a single recipient. * @param msg Message to check. */ - private void ensureMessageHasSingleRecipient(Message msg) { - assert msg instanceof OneToOneMessage : + private static void ensureMessageHasSingleRecipient(Message msg) { + assert msg instanceof OneToOneMessage : "MaxPropRouterWithEstimation only works with one to one messages"; } @@ -593,7 +593,7 @@ public class MaxPropRouterWithEstimation extends ActiveRouter { * threshold are given priority and they are ordered by their hop count. * Other messages are ordered by their delivery cost. */ - private class MaxPropComparator implements Comparator<OneToOneMessage> { + private class MaxPropComparator implements Comparator<OneToOneMessage> { private int threshold; private DTNHost from1; private DTNHost from2; diff --git a/src/routing/ProphetRouter.java b/src/routing/ProphetRouter.java index bf74617f..9dc326fe 100644 --- a/src/routing/ProphetRouter.java +++ b/src/routing/ProphetRouter.java @@ -213,7 +213,7 @@ public class ProphetRouter extends ActiveRouter { * @return The return value of {@link #tryMessagesForConnected(List)} */ private Tuple<? extends Message, Connection> tryOtherMessages() { - List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); + List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); Collection<Message> msgCollection = getMessageCollection(); @@ -228,13 +228,13 @@ public class ProphetRouter extends ActiveRouter { } for (Message m : msgCollection) { - OneToOneMessage msg = this.castMessageToOneToOne(m); + OneToOneMessage msg = ProphetRouter.castMessageToOneToOne(m); if (othRouter.hasMessage(msg.getId())) { continue; // skip messages that the other one has } if (othRouter.getPredFor(msg.getTo()) > getPredFor(msg.getTo())) { // the other node has higher probability of delivery - messages.add(new Tuple<OneToOneMessage, Connection>(msg,con)); + messages.add(new Tuple<>(msg,con)); } } } @@ -254,7 +254,7 @@ public class ProphetRouter extends ActiveRouter { * @return Message as OneToOneMessage. * @throws AssertionError if message is not a OneToOneMessage. */ - private OneToOneMessage castMessageToOneToOne(Message msg) { + private static OneToOneMessage castMessageToOneToOne(Message msg) { assert msg instanceof OneToOneMessage : "ProphetRouter only works with one to one messages"; return (OneToOneMessage) msg; @@ -268,7 +268,7 @@ public class ProphetRouter extends ActiveRouter { private class TupleComparator implements Comparator <Tuple<OneToOneMessage, Connection>> { - public int compare(Tuple<OneToOneMessage, Connection> tuple1, + public int compare(Tuple<OneToOneMessage, Connection> tuple1, Tuple<OneToOneMessage, Connection> tuple2) { // delivery probability of tuple1's message with tuple1's connection double p1 = ((ProphetRouter)tuple1.getValue(). diff --git a/src/routing/ProphetRouterWithEstimation.java b/src/routing/ProphetRouterWithEstimation.java index 37e5b65e..b9f5adce 100644 --- a/src/routing/ProphetRouterWithEstimation.java +++ b/src/routing/ProphetRouterWithEstimation.java @@ -423,7 +423,7 @@ public class ProphetRouterWithEstimation extends ActiveRouter { * @return The return value of {@link #tryMessagesForConnected(List)} */ private Tuple<? extends Message, Connection> tryOtherMessages() { - List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); + List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); Collection<Message> msgCollection = getMessageCollection(); @@ -438,13 +438,13 @@ public class ProphetRouterWithEstimation extends ActiveRouter { } for (Message m : msgCollection) { - OneToOneMessage msg = this.castMessageToOneToOne(m); + OneToOneMessage msg = ProphetRouterWithEstimation.castMessageToOneToOne(m); if (othRouter.hasMessage(msg.getId())) { continue; // skip messages that the other one has } if (othRouter.getPredFor(msg.getTo()) > getPredFor(msg.getTo())) { // the other node has higher probability of delivery - messages.add(new Tuple<OneToOneMessage, Connection>(msg,con)); + messages.add(new Tuple<>(msg,con)); } } } @@ -466,7 +466,7 @@ public class ProphetRouterWithEstimation extends ActiveRouter { private class TupleComparator implements Comparator <Tuple<OneToOneMessage, Connection>> { - public int compare(Tuple<OneToOneMessage, Connection> tuple1, + public int compare(Tuple<OneToOneMessage, Connection> tuple1, Tuple<OneToOneMessage, Connection> tuple2) { // delivery probability of tuple1's message with tuple1's connection double p1 = ((ProphetRouterWithEstimation)tuple1.getValue(). @@ -497,10 +497,10 @@ public class ProphetRouterWithEstimation extends ActiveRouter { * @return Message as OneToOneMessage. * @throws AssertionError if message is not a OneToOneMessage. */ - private OneToOneMessage castMessageToOneToOne(Message msg) { - assert msg instanceof OneToOneMessage : + private static OneToOneMessage castMessageToOneToOne(Message msg) { + assert msg instanceof OneToOneMessage : "ProphetRouterWithEstimation only works with one to one messages"; - return (OneToOneMessage) msg; + return (OneToOneMessage) msg; } @Override diff --git a/src/routing/ProphetV2Router.java b/src/routing/ProphetV2Router.java index 3cd6a2b2..8f80754d 100644 --- a/src/routing/ProphetV2Router.java +++ b/src/routing/ProphetV2Router.java @@ -266,7 +266,7 @@ public class ProphetV2Router extends ActiveRouter { * @return The return value of {@link #tryMessagesForConnected(List)} */ private Tuple<? extends Message, Connection> tryOtherMessages() { - List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); + List<Tuple<OneToOneMessage, Connection>> messages = new ArrayList<>(); Collection<Message> msgCollection = getMessageCollection(); @@ -281,14 +281,13 @@ public class ProphetV2Router extends ActiveRouter { } for (Message m : msgCollection) { - OneToOneMessage msg = this.castMessageToOneToOne(m); + OneToOneMessage msg = ProphetV2Router.castMessageToOneToOne(m); if (othRouter.hasMessage(msg.getId())) { continue; // skip messages that the other one has } if((othRouter.getPredFor(msg.getTo()) >= getPredFor(msg.getTo()))) { - - messages.add(new Tuple<OneToOneMessage, Connection>(msg,con)); + messages.add(new Tuple<>(msg,con)); } } } @@ -308,7 +307,7 @@ public class ProphetV2Router extends ActiveRouter { * @return Message as OneToOneMessage. * @throws AssertionError if message is not a OneToOneMessage. */ - private OneToOneMessage castMessageToOneToOne(Message msg) { + private static OneToOneMessage castMessageToOneToOne(Message msg) { assert msg instanceof OneToOneMessage : "ProphetV2Router only works with one to one messages"; return (OneToOneMessage) msg; @@ -322,7 +321,7 @@ public class ProphetV2Router extends ActiveRouter { private class TupleComparator implements Comparator <Tuple<OneToOneMessage, Connection>> { - public int compare(Tuple<OneToOneMessage, Connection> tuple1, + public int compare(Tuple<OneToOneMessage, Connection> tuple1, Tuple<OneToOneMessage, Connection> tuple2) { // delivery probability of tuple1's message with tuple1's connection double p1 = ((ProphetV2Router)tuple1.getValue(). diff --git a/src/routing/util/MessageTransferAcceptPolicy.java b/src/routing/util/MessageTransferAcceptPolicy.java index a0aa6491..8f756856 100644 --- a/src/routing/util/MessageTransferAcceptPolicy.java +++ b/src/routing/util/MessageTransferAcceptPolicy.java @@ -229,13 +229,13 @@ public class MessageTransferAcceptPolicy { * @return true if both conditions evaluated to true */ private boolean checkSimplePolicy(Message m, int ownAddress) { - boolean checkRecipients = true; - if (m instanceof ISpecificRecipientsMessage) { - List<DTNHost> recipients = ((ISpecificRecipientsMessage)m).getFinalRecipients(); - checkRecipients = this.checkSimplePolicy(recipients, this.toSendPolicy, ownAddress); + boolean checkRecipients = true; + if (m instanceof ISpecificRecipientsMessage) { + List<DTNHost> recipients = ((ISpecificRecipientsMessage)m).getFinalRecipients(); + checkRecipients = MessageTransferAcceptPolicy.checkSimplePolicy(recipients, this.toSendPolicy, ownAddress); } - return checkRecipients && checkSimplePolicy(m.getFrom(), this.fromSendPolicy, ownAddress); + return checkRecipients && checkSimplePolicy(m.getFrom(), this.fromSendPolicy, ownAddress); } /** @@ -248,10 +248,10 @@ public class MessageTransferAcceptPolicy { * @return True if the address was in the policy list, or the policy list * was null */ - private boolean checkSimplePolicy(DTNHost host, Range [] policy, int thisHost) { - List<DTNHost> hosts = new ArrayList<>(1); - hosts.add(host); - return this.checkSimplePolicy(hosts, policy, thisHost); + private static boolean checkSimplePolicy(DTNHost host, Range [] policy, int thisHost) { + List<DTNHost> hosts = new ArrayList<>(1); + hosts.add(host); + return MessageTransferAcceptPolicy.checkSimplePolicy(hosts, policy, thisHost); } /** @@ -264,22 +264,20 @@ public class MessageTransferAcceptPolicy { * @return True if one of the addresses was in the policy list, or the policy list * was null */ - private boolean checkSimplePolicy(List<DTNHost> hosts, Range [] policy, int thisHost) { - if (policy == null) { + private static boolean checkSimplePolicy(List<DTNHost> hosts, Range [] policy, int thisHost) { + if (policy == null) { return true; } - for (Range r : policy) { - for (DTNHost host : hosts) { - if (r.isInRange(TO_ME_VALUE) && host.getAddress() == thisHost) { - return true; - } - else if (r.isInRange(host.getAddress())) { - return true; - } + for (Range r : policy) { + for (DTNHost host : hosts) { + boolean checkIsTrueBecauseOfOwnAddress = r.isInRange(TO_ME_VALUE) && host.getAddress() == thisHost; + if (checkIsTrueBecauseOfOwnAddress || r.isInRange(host.getAddress())) { + return true; + } } } - return false; + return false; } /** -- GitLab