aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/cgCacheDistanceComparator.java
blob: 2fbdd6494cd7ca064837a6774536a8e77c9ac8d1 (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
package cgeo.geocaching;

import java.util.Comparator;
import android.util.Log;

public class cgCacheDistanceComparator implements Comparator<cgCache> {
	private Double latitude = null;
	private Double longitude = null;
	
	public cgCacheDistanceComparator() {
		// nothing
	}

	public cgCacheDistanceComparator(Double latitudeIn, Double longitudeIn) {
		latitude = latitudeIn;
		longitude = longitudeIn;
	}
	
	public void setCoords(Double latitudeIn, Double longitudeIn) {
		latitude = latitudeIn;
		longitude = longitudeIn;
	}
	
	public int compare(cgCache cache1, cgCache cache2) {
		int result = 0;
		try {
			if (
				(cache1.latitude == null || cache1.longitude == null || cache2.latitude == null || cache2.longitude == 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.latitude == null || cache1.longitude == null) return 1;
				if (cache2.latitude == null || cache2.longitude == null) return -1;

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

				if (distance1 < distance2) result =  -1;
				else if (distance1 > distance2) result = 1;
				else result = 0;
			}
		} catch (Exception e) {
			Log.e(cgSettings.tag, "cgCacheDistanceComparator.compare: " + e.toString());
		}

		return result;
	}
}