blob: a23d135a7d693740fcf06841311a0b5dc1ccfd56 (
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
62
63
64
65
66
67
68
|
package cgeo.geocaching.sorting;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.enumerations.LogType;
import cgeo.geocaching.utils.Log;
/**
* abstract super implementation for all cache comparators
*
*/
public abstract class AbstractCacheComparator implements CacheComparator {
@Override
public final int compare(final Geocache cache1, final Geocache cache2) {
try {
// first check that we have all necessary data for the comparison
if (!canCompare(cache1, cache2)) {
return 0;
}
return compareCaches(cache1, cache2);
} catch (Exception e) {
Log.e("AbstractCacheComparator.compare", e);
}
return 0;
}
/**
* Check necessary preconditions (like missing fields) before running the comparison itself
*
* @param cache1
* @param cache2
* @return
*/
protected abstract boolean canCompare(final Geocache cache1, final Geocache cache2);
/**
* Compares two caches. Logging and exception handling is implemented outside this method already.
* <p/>
* A cache is smaller than another cache if it is desirable to show it first when presented to the user.
* For example, a highly rated cache must be considered smaller than a poorly rated one.
*
* @param cache1
* @param cache2
* @return an integer < 0 if cache1 is less than cache2, 0 if they are equal, and > 0 if cache1 is greater than
* cache2.
*/
protected abstract int compareCaches(final Geocache cache1, final Geocache cache2);
/**
* Get number of overall finds for a cache.
*
* @param cache
* @return
*/
protected static int getFindsCount(Geocache cache) {
if (cache.getLogCounts().isEmpty()) {
cache.setLogCounts(DataStore.loadLogCounts(cache.getGeocode()));
}
Integer logged = cache.getLogCounts().get(LogType.FOUND_IT);
if (logged != null) {
return logged;
}
return 0;
}
}
|