1 package cz.cuni.amis.utils.astar; 2 3 import java.util.Comparator; 4 import java.util.Map; 5 6 /** 7 * This comparator is a tricky object - it serves for the {@link AStarHeap} to compare nodes inside the heap. 8 * <p><p> 9 * The trick is, that it is initialized {@link AStarHeapComparator#AStarHeapComparator(Map)} with a map 10 * that contains node's costs that are used during the comparation inside {@link AStarHeapComparator#compare(Object, Object)}. 11 * No magic yet, ha? Well, the magic is that this map is not cloned... simply a pointer to this very instance passed 12 * inside the constructor is saved to you may alter the cost as you wish to! Which is truly needed by the {@link AStar} class 13 * that is obtaining nodes' costs from the {@link AStarMap} implementor. 14 * 15 * @author Jimmy 16 * 17 * @param <NODE> 18 */ 19 public class AStarHeapComparator<NODE> implements Comparator { 20 21 private Map<NODE, Integer> values; 22 23 public AStarHeapComparator(Map<NODE, Integer> estimatedCosts){ 24 values = estimatedCosts; 25 } 26 27 public int compare(Object arg0, Object arg1) { 28 return values.get(arg0).intValue() - values.get(arg1).intValue(); 29 } 30 31 }