aboutsummaryrefslogtreecommitdiffstats
path: root/src/cgeo/geocaching/sorting/AbstractCacheComparator.java
blob: 03db8df7294a5d1926bd5a76c1c76ba5041e8058 (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
package cgeo.geocaching.sorting;

import cgeo.geocaching.cgCache;
import cgeo.geocaching.cgSettings;

import android.util.Log;

/**
 * abstract super implementation for all cache comparators
 *
 */
public abstract class AbstractCacheComparator implements CacheComparator {

    @Override
    public final int compare(cgCache cache1, cgCache 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(cgSettings.tag, "AbstractCacheComparator.compare: " + e.toString());
        }
        return 0;
    }

    /**
     * check necessary preconditions (like missing fields) before running the comparison itself
     *
     * @param cache1
     * @param cache2
     * @return
     */
    protected abstract boolean canCompare(final cgCache cache1, final cgCache cache2);

    /**
     * compares two caches. Logging and exception handling is implemented outside this method already.
     *
     * @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 cgCache cache1, final cgCache cache2);
}