View Javadoc

1   /*
2    * @(#) LatLonDistanceComparator.java Jan 29, 2005
3    * 
4    * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
5    * Delft, the Netherlands. All rights reserved.
6    * 
7    * See for project information <a href="http://www.simulation.tudelft.nl/">
8    * www.simulation.tudelft.nl </a>.
9    * 
10   * The source code and binary code of this software is proprietary information
11   * of Delft University of Technology.
12   * 
13   */
14  package org.gscg.common.geo;
15  
16  import java.util.Comparator;
17  
18  import javax.vecmath.Point2d;
19  
20  import nl.tudelft.simulation.logger.Logger;
21  
22  
23  /***
24   * <p>
25   * 
26   * Copyright (c) 2003-2005 Delft University of Technology, Jaffalaan 5, 2628 BX
27   * Delft, the Netherlands. All rights reserved.
28   * 
29   * See for project information <a href="http://www.simulation.tudelft.nl/">
30   * www.simulation.tudelft.nl </a>.
31   * 
32   * The source code and binary code of this software is proprietary information
33   * of Delft University of Technology.
34   * 
35   * 
36   * @author <a
37   *         href="http://www.tbm.tudelft.nl/webstaf/stijnh/index.htm">Stijn-Pieter
38   *         van Houten </a>
39   * @version $Revision: 1.1 $ $Date: 2005/06/16 12:34:08 $
40   * @since 1.1.2
41   */
42  public class LatLonDistanceComparator implements Comparator
43  {
44  	/*** the own location */
45  	private Point2d ownLocation = null;
46  
47  	/*** the calculator */
48  	private CalculateLatLonDistance calculateLatLonDistance = null;
49  
50  	/***
51  	 * constructs a new LatLonDistanceComparator
52  	 * 
53  	 * @param ownLocation the own location to compare two other locations
54  	 *        against and eventually the difference in distance
55  	 */
56  	public LatLonDistanceComparator(final Point2d ownLocation)
57  	{
58  		super();
59  		this.ownLocation = ownLocation;
60  		this.calculateLatLonDistance = new CalculateLatLonDistance();
61  	}
62  
63  	/***
64  	 * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object)
65  	 */
66  	public int compare(final Object o1, final Object o2)
67  	{
68  		try
69  		{
70  			LatLonInterface location1 = (LatLonInterface) o1;
71  			LatLonInterface location2 = (LatLonInterface) o2;
72  
73  			double distanceTo1 = this.calculateLatLonDistance.getDistance(
74  					this.ownLocation, location1.getLatLonLocation());
75  			double distanceTo2 = this.calculateLatLonDistance.getDistance(
76  					this.ownLocation, location2.getLatLonLocation());
77  
78  			if (distanceTo1 < distanceTo2)
79  			{
80  				return -1;
81  			}
82  			return 1;
83  		} catch (Exception exception)
84  		{
85  			Logger.warning(this, "compare", exception);
86  		}
87  		return new Integer(o1.hashCode()).compareTo(new Integer(o2.hashCode()));
88  	}
89  }