package org.psjava.algo.graph.shortestpath;

import java.util.LinkedList;

import org.psjava.ds.graph.DirectedEdge;
import org.psjava.ds.map.Map;
import org.psjava.ds.numbersystrem.InfinitableNumber;
import org.psjava.util.AssertStatus;

public class SingleSourceShortestPathResultFactory {

	public static <V, E extends DirectedEdge<V>, W> SingleSourceShortestPathResult<V, W, E> create(final V start, final Map<V, InfinitableNumber<W>> distance, final Map<V, E> previous) {
		return new SingleSourceShortestPathResult<V, W, E>() {
			@Override
			public W getDistance(V to) {
				assertReachable(to);
				return distance.get(to).getValue();
			}

			@Override
			public Iterable<E> getPath(V to) {
				assertReachable(to);
				LinkedList<E> r = new LinkedList<E>();
				for (V v = to; !v.equals(start); v = previous.get(v).from())
					r.addFirst(previous.get(v));
				return r;
			}

			@Override
			public boolean isReachable(V to) {
				return !distance.get(to).isInfinity();
			}

			private void assertReachable(V to) {
				AssertStatus.assertTrue(isReachable(to), "Not reachable");
			}
		};
	}

	private SingleSourceShortestPathResultFactory() {
	}

}

@@ -237,16 +237,57 @@
 Status;%0D
+%0Aimport org.psjava.util.VarargsIterable;%0D
 %0A%0D%0Apubli
@@ -337,17 +337,20 @@
 ry %7B%0D%0A%0D%0A
-%09
+    
 public s
@@ -525,18 +525,24 @@
 ous) %7B%0D%0A
-%09%09
+        
 return n
@@ -589,33 +589,51 @@
 E%3E() %7B%0D%0A
-%09%09%09@Override%0D%0A%09%09%09
+            @Override%0D%0A            
 public W
@@ -646,36 +646,48 @@
 stance(V to) %7B%0D%0A
-%09%09%09%09
+                
 assertReachable(
@@ -688,28 +688,40 @@
 hable(to);%0D%0A
-%09%09%09%09
+                
 return dista
@@ -749,33 +749,60 @@
 );%0D%0A
-%09%09%09%7D%0D%0A%0D%0A%09%09%09@Override%0D%0A%09%09%09
+            %7D%0D%0A%0D%0A            @Override%0D%0A            
 publ
@@ -833,20 +833,32 @@
  to) %7B%0D%0A
-%09%09%09%09
+                
 assertRe
@@ -871,20 +871,32 @@
 e(to);%0D%0A
-%09%09%09%09
+                
 LinkedLi
@@ -927,20 +927,32 @@
 %3CE%3E();%0D%0A
-%09%09%09%09
+                
 for (V v
@@ -1009,13 +1009,28 @@
 ))%0D%0A
-%09%09%09%09%09
+                    
 r.ad
@@ -1059,48 +1059,87 @@
 );%0D%0A
-%09%09%09%09return r;%0D%0A%09%09%09%7D%0D%0A%0D%0A%09%09%09@Override%0D%0A%09%09%09
+                return r;%0D%0A            %7D%0D%0A%0D%0A            @Override%0D%0A            
 publ
@@ -1170,20 +1170,32 @@
  to) %7B%0D%0A
-%09%09%09%09
+                
 return !
@@ -1230,19 +1230,37 @@
 );%0D%0A
-%09%09%09%7D%0D%0A%0D%0A%09%09%09
+            %7D%0D%0A%0D%0A            
 priv
@@ -1297,12 +1297,24 @@
  %7B%0D%0A
-%09%09%09%09
+                
 Asse
@@ -1373,27 +1373,207 @@
 );%0D%0A
-%09%09%09%7D%0D%0A%09%09%7D;%0D%0A%09
+            %7D%0D%0A%0D%0A            @Override%0D%0A            public String toString() %7B%0D%0A                return VarargsIterable.create(distance, previous).toString();%0D%0A            %7D%0D%0A        %7D;%0D%0A    
 %7D%0D%0A%0D%0A
-%09
+    
 priv
@@ -1623,9 +1623,12 @@
  %7B%0D%0A
-%09
+    
 %7D%0D%0A%0D

improve debug log