From 3ae5d03f481ac2f77eed03d08d7e70a9db642acb Mon Sep 17 00:00:00 2001
From: Nikolaus Krismer <nikolaus.krismer@uibk.ac.at>
Date: Mon, 30 Jun 2014 21:08:32 +0200
Subject: [PATCH] fixed minor findbugs issues

---
 .../inf/isochrone/algorithm/Isochrone.java    |  3 +-
 .../inf/isochrone/config/ConfigIsochrone.java | 10 +--
 .../unibz/inf/isochrone/network/Location.java |  2 +-
 .../it/unibz/inf/isochrone/network/Node.java  | 21 +++---
 .../network/AlgorithmRuntimeTest.java         | 65 +++++++------------
 5 files changed, 39 insertions(+), 62 deletions(-)

diff --git a/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java b/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java
index bb5a8a00..a7876b6d 100644
--- a/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java
+++ b/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java
@@ -26,7 +26,6 @@ import java.util.Set;
  * algorithms which define the loading of the data from the database.
  */
 public abstract class Isochrone {
-	private static final double COMPARE_PRECISION = 0.0000001d;
 	private final Query query;
 	private final PriorityQueue<Node> queue;
 	private Set<Integer> codes;
@@ -135,7 +134,7 @@ public abstract class Isochrone {
 			for (final Link link : adjacents) {
 				if (link.isContinuous()) {
 					updateQueue(expandContinuousLink(node, link));
-					if (Math.abs(link.getStartOffset() - Double.MIN_VALUE) < COMPARE_PRECISION) {
+					if (new Double(Double.MIN_VALUE).equals(link.getStartOffset())) {
 						output.addLink(link);
 					}
 				} else {
diff --git a/src/main/java/it/unibz/inf/isochrone/config/ConfigIsochrone.java b/src/main/java/it/unibz/inf/isochrone/config/ConfigIsochrone.java
index 5b0721f8..7c8b0863 100644
--- a/src/main/java/it/unibz/inf/isochrone/config/ConfigIsochrone.java
+++ b/src/main/java/it/unibz/inf/isochrone/config/ConfigIsochrone.java
@@ -70,12 +70,14 @@ public final class ConfigIsochrone {
 	private String user;
 
 	public static ConfigIsochrone getInstance() {
-		if (instance == null) {
+		if (instance == null) { // avoid sync penalty if we can
 			synchronized (ConfigIsochrone.class) {
-				final ConfigIsochrone config = new ConfigBuilder<>(ConfigIsochrone.class).build();
-				config.initConnectionFactory();
+				if (instance == null) { // lock has to be inside of synchronized block
+					final ConfigIsochrone config = new ConfigBuilder<>(ConfigIsochrone.class).build();
+					config.initConnectionFactory();
 
-				instance = config;
+					instance = config;
+				}
 			}
 		}
 
diff --git a/src/main/java/it/unibz/inf/isochrone/network/Location.java b/src/main/java/it/unibz/inf/isochrone/network/Location.java
index db532085..3c1ef528 100644
--- a/src/main/java/it/unibz/inf/isochrone/network/Location.java
+++ b/src/main/java/it/unibz/inf/isochrone/network/Location.java
@@ -39,7 +39,7 @@ public class Location {
 		}
 
 		final Location other = (Location) obj;
-		return other.getLinkId() == getLinkId() && other.getOffset() == getOffset();
+		return other.getLinkId() == getLinkId() && new Double(other.getOffset()).equals(getOffset());
 	}
 
 	@Override
diff --git a/src/main/java/it/unibz/inf/isochrone/network/Node.java b/src/main/java/it/unibz/inf/isochrone/network/Node.java
index 5ac39c6b..b0cfb06b 100644
--- a/src/main/java/it/unibz/inf/isochrone/network/Node.java
+++ b/src/main/java/it/unibz/inf/isochrone/network/Node.java
@@ -13,11 +13,12 @@ import java.util.Map;
  */
 public class Node implements Comparable<Node> {
 	private final int id;
+	private final Map<Integer, Schedule> routes;
+
 	private double distance;
 	private short nrAdjacentLinks;
 	private boolean closed;
 	private Collection<Link> adjLinks = null;
-	private Map<Integer, Schedule> routes;
 	private int cheapestReachedRouteId = Integer.MIN_VALUE;
 	private Point coordinates = null; // for MrneX
 	private double radius = Double.NEGATIVE_INFINITY;
@@ -25,14 +26,11 @@ public class Node implements Comparable<Node> {
 	// Constructors
 
 	protected Node() {
-		id = Integer.MIN_VALUE;
+		this(Integer.MIN_VALUE, (short) -1);
 	}
 
 	public Node(final int nodeId) {
-		id = nodeId;
-		distance = Double.POSITIVE_INFINITY;
-		nrAdjacentLinks = -1;
-		closed = false;
+		this(nodeId, (short) -1);
 	}
 
 	public Node(final int nodeId, final short nrAdjacentLinks) {
@@ -40,6 +38,7 @@ public class Node implements Comparable<Node> {
 		distance = Double.POSITIVE_INFINITY;
 		this.nrAdjacentLinks = nrAdjacentLinks;
 		closed = false;
+		routes = new Hashtable<>();
 	}
 
 	// Public methods
@@ -53,10 +52,6 @@ public class Node implements Comparable<Node> {
 	}
 
 	public void addRoute(final int routeId) {
-		if (routes == null) {
-			routes = new Hashtable<>();
-		}
-
 		if (!routes.containsKey(routeId)) {
 			routes.put(routeId, new Schedule());
 		}
@@ -82,7 +77,7 @@ public class Node implements Comparable<Node> {
 	}
 
 	public boolean containsRoutes() {
-		return routes != null && !routes.isEmpty();
+		return !routes.isEmpty();
 	}
 
 	@Override
@@ -152,7 +147,7 @@ public class Node implements Comparable<Node> {
 		final int prime = 31;
 		final long temp = Double.doubleToLongBits(distance);
 		// CHECKSTYLE:OFF MagicNumber
-		result = prime * result + (int) (temp ^ (temp >>> 32));
+		result = prime * result + (int) (temp ^ temp >>> 32);
 		result = prime * result + id;
 		// CHECKSTYLE:ON MagicNumber
 
@@ -164,7 +159,7 @@ public class Node implements Comparable<Node> {
 	}
 
 	public boolean isExpired() {
-		return (getNrAdjacentLinks() == 0);
+		return getNrAdjacentLinks() == 0;
 	}
 
 	public void setAdjLinks(final Collection<Link> adjLinks) {
diff --git a/src/test/java/it/unibz/inf/isochrone/network/AlgorithmRuntimeTest.java b/src/test/java/it/unibz/inf/isochrone/network/AlgorithmRuntimeTest.java
index 805b0401..5abb7219 100644
--- a/src/test/java/it/unibz/inf/isochrone/network/AlgorithmRuntimeTest.java
+++ b/src/test/java/it/unibz/inf/isochrone/network/AlgorithmRuntimeTest.java
@@ -85,18 +85,18 @@ public final class AlgorithmRuntimeTest {
 		initDatasets();
 
 		System.out.println("Starting profiling:");
-		long startTimeOverall = Calendar.getInstance().getTimeInMillis();
+		final long startTimeOverall = Calendar.getInstance().getTimeInMillis();
 		System.out.println("-----------------------------");
 
 		for (int i = 0; i < REPEAT_COUNT; ++i) {
-			long startTime = Calendar.getInstance().getTimeInMillis();
+			final long startTime = Calendar.getInstance().getTimeInMillis();
 			o.testRuntimeMrneX();
-			long runTime = Calendar.getInstance().getTimeInMillis() - startTime;
+			final long runTime = Calendar.getInstance().getTimeInMillis() - startTime;
 			System.out.println(" - time for run #" + (i + 1) + " (in msec): " + runTime);
 		}
 
 		System.out.println("-----------------------------");
-		long runTimeOverall = Calendar.getInstance().getTimeInMillis() - startTimeOverall;
+		final long runTimeOverall = Calendar.getInstance().getTimeInMillis() - startTimeOverall;
 		System.out.println("Overall Time (in msec): " + runTimeOverall);
 	}
 
@@ -162,7 +162,7 @@ public final class AlgorithmRuntimeTest {
 		// CHECKSTYLE:ON MagicNumber
 
 		final Collection<String> datasetNames = datasetParams.keySet();
-		for (String dsName : datasetNames) {
+		for (final String dsName : datasetNames) {
 			addDataset(dsName);
 		}
 	}
@@ -174,10 +174,10 @@ public final class AlgorithmRuntimeTest {
 
 		times = new TreeSet<>();
 		for (int i = 0; i < RUNTIME_OFFSETS.length; ++i) {
-			int offset = RUNTIME_OFFSETS[i];
-			int stepWidth = RUNTIME_POINTS_PER_OFFSET[i];
+			final int offset = RUNTIME_OFFSETS[i];
+			final int stepWidth = RUNTIME_POINTS_PER_OFFSET[i];
 			for (int j = 0; j < offset; j += stepWidth) {
-				int runtime = Math.max(offset, offset * j);
+				final int runtime = Math.max(offset, offset * j);
 				if (runtime > MAX_RUNTIME) {
 					return;
 				}
@@ -189,17 +189,17 @@ public final class AlgorithmRuntimeTest {
 
 	private static void runForAllDatasets(final Class<? extends Isochrone> clazz) {
 		final Set<String> datasetNames = datasets.keySet();
-		for (String dsName : datasetNames) {
+		for (final String dsName : datasetNames) {
 			LOGGER.info("Executing " + clazz.getSimpleName() + " for dataset \"" + dsName + "\"");
 			long runTimeOverall = 0;
 
 			final Collection<TestParameters> dsParams = datasets.get(dsName);
 			for (final TestParameters params : dsParams) {
 				LOGGER.debug(" - executing for isochrone duration: " + params.getRuntime());
-				long startTime = Calendar.getInstance().getTimeInMillis();
+				final long startTime = Calendar.getInstance().getTimeInMillis();
 				AlgorithmHelper.run(clazz, params);
 
-				long runTime = Calendar.getInstance().getTimeInMillis() - startTime;
+				final long runTime = Calendar.getInstance().getTimeInMillis() - startTime;
 				LOGGER.debug(" - runtime \"" + params.getRuntime() + "\": " + runTime + "msec");
 				addRuntimeEntry(String.valueOf(params.getRuntime()), clazz.getSimpleName(), runTime);
 				runTimeOverall += runTime;
@@ -227,12 +227,10 @@ public final class AlgorithmRuntimeTest {
 
 	private static void writeMapToCSV(final File file, final Set<Entry<String, Map<String, Long>>> resultSet, final int repeatCount) {
 		final String separator = ";";
+		boolean isFirstLine = true;
 
-		BufferedWriter fw = null;
-		try {
-			fw = new BufferedWriter(new PrintWriter(file, RESULTFILE_ENCODING));
-			boolean isFirstLine = true;
-			for (Entry<String, Map<String, Long>> runtimeEntry : resultSet) {
+		try (final BufferedWriter fw = new BufferedWriter(new PrintWriter(file, RESULTFILE_ENCODING))) {
+			for (final Entry<String, Map<String, Long>> runtimeEntry : resultSet) {
 				final String time = runtimeEntry.getKey();
 				final Map<String, Long> results = runtimeEntry.getValue();
 				if (isFirstLine) {
@@ -260,38 +258,29 @@ public final class AlgorithmRuntimeTest {
 				}
 				fw.newLine();
 			}
-		} catch (IOException e) {
+
+			fw.flush();
+		} catch (final IOException e) {
 			e.printStackTrace();
-		} finally {
-			if (fw != null) {
-				try {
-					fw.flush();
-					fw.close();
-				} catch (IOException e) {
-					e.printStackTrace();
-				}
-			}
 		}
 	}
 
 	private static void writeMapToXML(final File file, final Set<Entry<String, Map<String, Long>>> resultSet, final int repeatCount) {
-		BufferedWriter fw = null;
-		try {
-			fw = new BufferedWriter(new PrintWriter(file, RESULTFILE_ENCODING));
+		try (final BufferedWriter fw = new BufferedWriter(new PrintWriter(file, RESULTFILE_ENCODING))) {
 			fw.write("<?xml version=\"1.0\" encoding=\"" + RESULTFILE_ENCODING + "\"?>");
 			fw.newLine();
 			fw.write("<results numberOfRuns=\"" + repeatCount + "\">");
 			fw.newLine();
 
-			for (Entry<String, Map<String, Long>> runtimeEntry : resultSet) {
+			for (final Entry<String, Map<String, Long>> runtimeEntry : resultSet) {
 				final String time = runtimeEntry.getKey();
 				final Map<String, Long> results = runtimeEntry.getValue();
 
 				fw.write("\t<time_" + time + ">");
 				fw.newLine();
 				final Set<Entry<String, Long>> timeSet = results.entrySet();
-				for (Entry<String, Long> entry : timeSet) {
-					fw.write("\t\t<" + entry.getKey() + ">" + (entry.getValue().longValue() / repeatCount) + "</" + entry.getKey() + ">");
+				for (final Entry<String, Long> entry : timeSet) {
+					fw.write("\t\t<" + entry.getKey() + ">" + entry.getValue().longValue() / repeatCount + "</" + entry.getKey() + ">");
 					fw.newLine();
 				}
 				fw.write("\t</time_" + time + ">");
@@ -299,17 +288,9 @@ public final class AlgorithmRuntimeTest {
 			}
 			fw.write("</results>");
 			fw.newLine();
-		} catch (IOException e) {
+			fw.flush();
+		} catch (final IOException e) {
 			e.printStackTrace();
-		} finally {
-			if (fw != null) {
-				try {
-					fw.flush();
-					fw.close();
-				} catch (IOException e) {
-					e.printStackTrace();
-				}
-			}
 		}
 	}
 
-- 
GitLab