From e2c4dd7b35fba48ca36457ac7a239cab347ddcfa Mon Sep 17 00:00:00 2001
From: Nikolaus Krismer <nikolaus.krismer@uibk.ac.at>
Date: Mon, 11 Aug 2014 16:15:02 +0200
Subject: [PATCH] extracted some functionality into methods

---
 .../inf/isochrone/algorithm/Isochrone.java    | 131 +++++++++---------
 1 file changed, 63 insertions(+), 68 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 f4df5218..20290977 100644
--- a/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java
+++ b/src/main/java/it/unibz/inf/isochrone/algorithm/Isochrone.java
@@ -133,14 +133,13 @@ public abstract class Isochrone {
 			final Collection<Link> adjacents = calcAdjLinks(node.getID());
 			output.addNode(node);
 			node.close();
+
 			for (final Link link : adjacents) {
 				if (link.isContinuous()) {
-					updateQueue(expandContinuousLink(node, link));
-					if (Math.abs(link.getStartOffset() - Double.MIN_VALUE) < COMPARE_PRECISION) {
-						output.addLink(link);
-					}
+					updateNodeQueue(expandContinuousLink(node, link));
+					addLinks(output, link);
 				} else {
-					updateQueue(expandDiscreteLink(node, link));
+					updateNodeQueue(expandDiscreteLink(node, link));
 				}
 			}
 
@@ -154,9 +153,44 @@ public abstract class Isochrone {
 		}
 
 		output.afterCalculation();
+
 		return output;
 	}
 
+	// Protected abstract methods
+
+	/**
+	 * Gets all links adjacent to a given node.
+	 *
+	 * @param nodeId the id of the node for which the adjacent links should be retrieved.
+	 * @return the adjacent link collection
+	 */
+	protected abstract Collection<Link> calcAdjLinks(int nodeId);
+
+	/**
+	 * Initialize a node and returns it.
+	 *
+	 * @param nodeId the id of the node that should be initialized and returned
+	 * @return the initialized node
+	 */
+	protected abstract Node initializeNode(int nodeId);
+
+	/**
+	 * Remove a node from the in-memory network, only used in the
+	 * MineX and MrneX algorithms.
+	 *
+	 * @param id The id of the node to be removed
+	 */
+	protected abstract void removeNode(int id);
+
+	// Private methods
+
+	private <T extends Output> void addLinks(final T output, final Link link) {
+		if (Math.abs(link.getStartOffset() - Double.MIN_VALUE) < COMPARE_PRECISION) {
+			output.addLink(link);
+		}
+	}
+
 	/**
 	 * Expand the given continuous link (that is connected to the given node)
 	 * and adjusts the link offsets based on the distance of the current Node
@@ -166,12 +200,14 @@ public abstract class Isochrone {
 	 * @param link continuous link, that is connected to the node
 	 * @return another node, if it can be reached in the available time (or null if time has run out)
 	 */
-	public Node expandContinuousLink(final Node node, final Link link) {
+	private Node expandContinuousLink(final Node node, final Link link) {
 		final int duration = query.getDuration().intValue();
 		final double walkingSpeed = query.getWalkingSpeed();
 		double destinationOffset;
 
 		final Node adjacentNode = getNode(link.getOppositeOf(node));
+		adjacentNode.visitNrAdjacentLinks((short) 1);
+
 		if (query.getDir() == Direction.INCOMING) {
 			destinationOffset = Math.max(0, link.getLength() - (query.getDuration() - node.getDistance()) * walkingSpeed);
 			link.setStartOffset(destinationOffset);
@@ -183,19 +219,14 @@ public abstract class Isochrone {
 			link.setEndOffset(destinationOffset);
 		}
 
-		adjacentNode.visitNrAdjacentLinks((short) 1);
 		if (!adjacentNode.isClosed()) {
-			final double newDistance = node.getDistance() + link.getLength()
-					/ walkingSpeed;
-			if (newDistance <= duration
-					&& newDistance < adjacentNode.getDistance()) {
+			final double newDistance = node.getDistance() + link.getLength() / walkingSpeed;
+			if (newDistance <= duration && newDistance < adjacentNode.getDistance()) {
 				adjacentNode.setDistance(newDistance);
 				return adjacentNode;
 			}
-		} else {
-			if (query.isExpireNodes() && adjacentNode.isExpired() && node.getID() != adjacentNode.getID()) {
-				removeNode(adjacentNode.getID());
-			}
+		} else if (query.isExpireNodes() && adjacentNode.isExpired() && node.getID() != adjacentNode.getID()) {
+			removeNode(adjacentNode.getID());
 		}
 
 		return null;
@@ -211,84 +242,48 @@ public abstract class Isochrone {
 	 * @param link discrete link connected to the node
 	 * @return another node, if it can be reached in the available time (or null if there is no time to reach another node)
 	 */
-	public Node expandDiscreteLink(final Node node, final Link link) {
+	private Node expandDiscreteLink(final Node node, final Link link) {
 		final Long duration = query.getDuration();
-		final Long fromTime = query.getFromTime();
-		final Long toTime = query.getToTime();
-
 		final Node adjacentNode = getNode(link.getOppositeOf(node));
 		adjacentNode.visitNrAdjacentLinks((short) 1);
+
 		if (!adjacentNode.isClosed()) {
 			final Set<Short> routes = new HashSet<>();
 			routes.add((short) link.getRoute());
 
-			final double newDistance = getAdjNodeCost(node, adjacentNode, routes, codes, fromTime, toTime);
+			final double newDistance = getAdjNodeCost(node, adjacentNode, routes);
 			if (newDistance <= duration && newDistance < adjacentNode.getDistance()) {
 				adjacentNode.setDistance(newDistance);
 				return adjacentNode;
 			}
-		} else {
-			if (query.isExpireNodes() && adjacentNode.isExpired()) {
-				removeNode(adjacentNode.getID());
-			}
+		} else if (query.isExpireNodes() && adjacentNode.isExpired()) {
+			removeNode(adjacentNode.getID());
 		}
 
 		return null;
 	}
 
-	/**
-	 * Get the cost to travel to an adjacent node.
-	 *
-	 * @param node the id of the node from which the calculation is started
-	 * @param adjNode the id of the node which is traveled to
-	 * @param routeIds the routes that should be considered
-	 * @param dateCodes the date on which is traveled
-	 * @param fromTime the earliest time consider for starting
-	 * @param toTime the latest time of arrival
-	 * @return the cost of traveling from the node to adjNode
-	 */
-	public double getAdjNodeCost(final Node node, final Node adjNode, final Set<Short> routeIds,
-			final Set<Integer> dateCodes, final long fromTime, final long toTime) {
-		return database.getAdjNodeCost(node, adjNode, routeIds, dateCodes, fromTime, toTime);
-	}
-
 	/**
 	 * Gets date codes for a given time.
 	 *
 	 * @param time the given
 	 * @return returns a set of date codes
 	 */
-	public Set<Integer> getDateCodes(final Calendar time) {
+	private Set<Integer> getDateCodes(final Calendar time) {
 		return database.getDateCodes(time);
 	}
 
-	// Protected abstract methods
-
-	/**
-	 * Gets all links adjacent to a given node.
-	 *
-	 * @param nodeId the id of the node for which the adjacent links should be retrieved.
-	 * @return the adjacent link collection
-	 */
-	protected abstract Collection<Link> calcAdjLinks(int nodeId);
-
 	/**
-	 * Initialize a node and returns it.
-	 *
-	 * @param nodeId the id of the node that should be initialized and returned
-	 * @return the initialized node
-	 */
-	protected abstract Node initializeNode(int nodeId);
-
-	/**
-	 * Remove a node from the in-memory network, only used in the
-	 * MineX and MrneX algorithms.
+	 * Get the cost to travel to an adjacent node.
 	 *
-	 * @param id The id of the node to be removed
+	 * @param node the id of the node from which the calculation is started
+	 * @param adjNode the id of the node which is traveled to
+	 * @param routeIds the routes that should be considered
+	 * @return the cost of traveling from the node to adjNode
 	 */
-	protected abstract void removeNode(int id);
-
-	// Private methods
+	private double getAdjNodeCost(final Node node, final Node adjNode, final Set<Short> routeIds) {
+		return database.getAdjNodeCost(node, adjNode, routeIds, codes, query.getFromTime(), query.getToTime());
+	}
 
 	/**
 	 * Initialize the datecodes for the isochrone.
@@ -338,7 +333,7 @@ public abstract class Isochrone {
 
 			if (distance <= query.getDuration() && distance < node.getDistance()) {
 				node.setDistance(distance);
-				updateQueue(node);
+				updateNodeQueue(node);
 			}
 		}
 	}
@@ -350,7 +345,7 @@ public abstract class Isochrone {
 	 */
 	private void initializeStartNodes(final Collection<Integer> nodeIds) {
 		for (final Integer id : nodeIds) {
-			updateQueue(initializeNode(id));
+			updateNodeQueue(initializeNode(id));
 		}
 	}
 
@@ -360,7 +355,7 @@ public abstract class Isochrone {
 	 *
 	 * @param node The node that was changed
 	 */
-	private void updateQueue(final Node node) {
+	private void updateNodeQueue(final Node node) {
 		if (node != null) {
 			queue.remove(node);
 			queue.offer(node);
-- 
GitLab