aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/sorting/DistanceComparator.java
blob: 619875313ffb3b5b9f013bada81b9194e4ec4596 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package cgeo.geocaching.sorting;

import cgeo.geocaching.cgBase;
import cgeo.geocaching.cgCache;
import cgeo.geocaching.geopoint.Geopoint;

/**
 * sorts caches by distance to current position
 *
 */
public class DistanceComparator extends AbstractCacheComparator {
	private Geopoint coords = null;

	public DistanceComparator() {
		// nothing
	}

	public DistanceComparator(final Geopoint coords) {
		setCoords(coords);
	}

	public void setCoords(final Geopoint coords) {
		this.coords = coords;
	}

	@Override
	protected boolean canCompare(cgCache cache1, cgCache cache2) {
		return true;
	}

	@Override
	protected int compareCaches(final cgCache cache1, final cgCache cache2) {
		if ((cache1.coords == null || cache2.coords == null)
				&& cache1.distance != null && cache2.distance != null) {
			if (cache1.distance < cache2.distance) {
				return -1;
			} else if (cache1.distance > cache2.distance) {
				return 1;
			} else {
				return 0;
			}
		} else {
			if (cache1.coords == null) {
				return 1;
			}
			if (cache2.coords == null) {
				return -1;
			}

			Double distance1 = cgBase.getDistance(coords, cache1.coords);
			Double distance2 = cgBase.getDistance(coords, cache2.coords);

			if (distance1 < distance2) {
				return -1;
			} else if (distance1 > distance2) {
				return 1;
			}
		}
		return 0;
	}
}