From e4401fa87fcec09880a747cc2b1b35e0837860ac Mon Sep 17 00:00:00 2001
From: Nikolaus Krismer <niko@krismer.de>
Date: Tue, 8 Apr 2014 17:45:40 +0200
Subject: [PATCH] fixed some problems with empty lists changed list type of
 adjList variable to collection

---
 .../inf/isochrone/algorithm/Isochrone.java    |  9 ++--
 .../unibz/inf/isochrone/algorithm/MineX.java  |  2 +-
 .../unibz/inf/isochrone/algorithm/MrneX.java  | 42 ++++++++++---------
 .../it/unibz/inf/isochrone/db/Database.java   | 13 +++---
 .../it/unibz/inf/isochrone/network/Node.java  |  3 +-
 5 files changed, 39 insertions(+), 30 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 bc323754..deb58bcb 100644
--- a/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java
+++ b/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java
@@ -149,6 +149,10 @@ public abstract class Isochrone {
 				node.close();
 
 				final Collection<Link> adjacents = mapEntry.getValue();
+				if (adjacents == null) {
+					throw new NullPointerException("Unexpected null value!");
+				}
+
 				for (final Link link : adjacents) {
 					if (link.isContinuous()) {
 						updateQueue(expandContinuousLink(node, link));
@@ -315,9 +319,8 @@ public abstract class Isochrone {
 	 */
 	protected Map<Integer, Collection<Link>> calcAdjLinks(final Collection<Integer> nodes) {
 		final Map<Integer, Collection<Link>> resultMap = new HashMap<>();
-		for (Integer id : nodes) {
-			final Collection<Link> adjLinks = getNode(id).getAdjLinks();
-			resultMap.put(id, adjLinks);
+		for (final Integer id : nodes) {
+			resultMap.put(id, getNode(id).getAdjLinks());
 		}
 
 		return resultMap;
diff --git a/src/main/java/it/unibz/inf/isochrone/algorithm/MineX.java b/src/main/java/it/unibz/inf/isochrone/algorithm/MineX.java
index da4cf182..27866058 100644
--- a/src/main/java/it/unibz/inf/isochrone/algorithm/MineX.java
+++ b/src/main/java/it/unibz/inf/isochrone/algorithm/MineX.java
@@ -71,7 +71,7 @@ public class MineX extends Isochrone {
 		final Collection<Integer> c = new ArrayList<>(1);
 		c.add(nodeId);
 
-		return database.getAdjLinks(c, nodes).get(nodeId);
+		return calcAdjLinks(c).get(nodeId);
 	}
 
 	@Override
diff --git a/src/main/java/it/unibz/inf/isochrone/algorithm/MrneX.java b/src/main/java/it/unibz/inf/isochrone/algorithm/MrneX.java
index 5055cdd6..a6d06353 100644
--- a/src/main/java/it/unibz/inf/isochrone/algorithm/MrneX.java
+++ b/src/main/java/it/unibz/inf/isochrone/algorithm/MrneX.java
@@ -13,7 +13,6 @@ import java.util.ArrayList;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
@@ -23,7 +22,7 @@ import java.util.Set;
  * Loads the data in circles surrounding the currently explored node.
  */
 public class MrneX extends Isochrone {
-	private final Map<Integer, List<Link>> adjList;
+	private final Map<Integer, Collection<Link>> adjList;
 	private final Map<Integer, Node> loadedIERNodes;
 	private final Map<Integer, Node> nodes;
 	private final int maxDuration;
@@ -43,9 +42,9 @@ public class MrneX extends Isochrone {
 	public MrneX(final ConfigDataset config, final Database db, final Query query, final int maxMemorySize) throws AlgorithmException {
 		super(config, db, query);
 
-		adjList = new HashMap<Integer, List<Link>>();
-		loadedIERNodes = new HashMap<Integer, Node>();
-		nodes = new HashMap<Integer, Node>();
+		adjList = new HashMap<>();
+		loadedIERNodes = new HashMap<>();
+		nodes = new HashMap<>();
 		maxDuration = query.getDuration().intValue();
 		maxMemory = maxMemorySize;
 		walkingSpeed = query.getWalkingSpeed();
@@ -93,16 +92,19 @@ public class MrneX extends Isochrone {
 			}
 		}
 
-		final Map<Integer, Node> nullNodeMap = database.getNode(nullNodes);
-		final Set<Entry<Integer, Node>> nullNodeCheckList = nullNodeMap.entrySet();
-		for (Entry<Integer, Node> e : nullNodeCheckList) {
-			final Integer nodeId = e.getKey();
-			final Node node = e.getValue();
-			if (node == null) {
-				throw new RuntimeException("NodeId not present: " + nodeId);
-			}
+		if (!nullNodes.isEmpty()) {
+			final Map<Integer, Node> nullNodeMap = database.getNode(nullNodes);
+			final Set<Entry<Integer, Node>> nullNodeCheckList = nullNodeMap.entrySet();
+			for (Entry<Integer, Node> e : nullNodeCheckList) {
+				final Integer nodeId = e.getKey();
+				final Node node = e.getValue();
+				if (node == null) {
+					throw new RuntimeException("NodeId not present: " + nodeId);
+				}
 
-			result.put(nodeId, node);
+				nodes.put(nodeId, node);
+				result.put(nodeId, node);
+			}
 		}
 
 		return result;
@@ -140,7 +142,7 @@ public class MrneX extends Isochrone {
 
 		final Collection<Integer> nonCoordinateNodeIds = new ArrayList<>(maxNodeCount);
 		for (Integer nodeId : nodeIds) {
-			final List<Link> adjNodeLinks = adjList.get(nodeId);
+			final Collection<Link> adjNodeLinks = adjList.get(nodeId);
 			if (adjNodeLinks != null) {
 				/*
 				 * Check if adjacent continuous links are empty
@@ -170,10 +172,12 @@ public class MrneX extends Isochrone {
 		}
 
 		// fill coordinates of remaining nodes
-		final Map<Integer, Point> coordinates = database.getCoordinates(nonCoordinateNodeIds);
-		final Set<Entry<Integer, Point>> coordinatEntrySet = coordinates.entrySet();
-		for (Entry<Integer, Point> e : coordinatEntrySet) {
-			nodeMap.get(e.getKey()).setCoordinates(e.getValue());
+		if (!nonCoordinateNodeIds.isEmpty()) {
+			final Map<Integer, Point> coordinates = database.getCoordinates(nonCoordinateNodeIds);
+			final Set<Entry<Integer, Point>> coordinatEntrySet = coordinates.entrySet();
+			for (Entry<Integer, Point> e : coordinatEntrySet) {
+				nodeMap.get(e.getKey()).setCoordinates(e.getValue());
+			}
 		}
 
 		// prepare radius of remaining nodes
diff --git a/src/main/java/it/unibz/inf/isochrone/db/Database.java b/src/main/java/it/unibz/inf/isochrone/db/Database.java
index 6711e5cb..2cb10bd4 100644
--- a/src/main/java/it/unibz/inf/isochrone/db/Database.java
+++ b/src/main/java/it/unibz/inf/isochrone/db/Database.java
@@ -18,7 +18,6 @@ import java.util.Calendar;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.HashSet;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
@@ -157,7 +156,7 @@ public class Database {
 
 	// Public methods
 
-	public void addLink(final Link link, final short degree, final Map<Integer, Node> nodes, final Map<Integer, List<Link>> adjList) {
+	public void addLink(final Link link, final short degree, final Map<Integer, Node> nodes, final Map<Integer, Collection<Link>> adjList) {
 		int n1, n2;
 		if (isIncoming) {
 			n1 = link.getStartNode();
@@ -518,10 +517,12 @@ public class Database {
 	/**
 	 * Loads the links in a circle from the database (for better explanation see the MrneX algorithm).
 	 */
-	public void loadLinksFromIER(final Node node, final Collection<Node> intersections, final Map<Integer, Node> nodes, final Map<Integer, List<Link>> adjList) {
+	public void loadLinksFromIER(final Node node, final Collection<Node> intersections, final Map<Integer, Node> nodes, final Map<Integer, Collection<Link>> adjList) {
 		PreparedStatement stmt = null;
 		ResultSet rs = null;
 
+		final int serverSRID = config.getServerSRID();
+		final double r = node.getRadius();
 		final Point g = node.getCoordinates();
 		final String pointString = "POINT( " + g.getX() + " " + g.getY() + ")";
 		try {
@@ -529,13 +530,13 @@ public class Database {
 				stmt = getPstmt(queryLoadLinksFromIER);
 				// CHECKSTYLE:OFF MagicNumber
 				stmt.setString(1, pointString);
-				stmt.setInt(2, config.getServerSRID());
-				stmt.setDouble(3, node.getRadius());
+				stmt.setInt(2, serverSRID);
+				stmt.setDouble(3, r);
 				// CHECKSTYLE:ON MagicNumber
 			} else {
 				final Node[] loadedAreas = intersections.toArray(new Node[0]);
 				final String disjunctAreaString = "st_difference(st_buffer(st_pointfromtext('" + pointString + "',"
-						+ config.getServerSRID() + ")," + node.getRadius() + ")," + subQuery(loadedAreas, loadedAreas.length - 1, config.getServerSRID()) + ")";
+						+ config.getServerSRID() + ")," + r + ")," + subQuery(loadedAreas, loadedAreas.length - 1, serverSRID) + ")";
 				stmt = getPstmt(String.format(queryGetLinksInRange, disjunctAreaString));
 			}
 			rs = stmt.executeQuery();
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 f1d30d43..4483565d 100644
--- a/src/main/java/it/unibz/inf/isochrone/network/Node.java
+++ b/src/main/java/it/unibz/inf/isochrone/network/Node.java
@@ -3,6 +3,7 @@ package it.unibz.inf.isochrone.network;
 import it.unibz.inf.isochrone.util.Point;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Hashtable;
 import java.util.List;
 
@@ -101,7 +102,7 @@ public class Node implements Comparable<Node> {
 		return true;
 	}
 
-	public List<Link> getAdjLinks() {
+	public Collection<Link> getAdjLinks() {
 		return adjLinks;
 	}
 
-- 
GitLab