diff options
| author | Bananeweizen <bananeweizen@gmx.de> | 2011-07-23 21:52:13 +0200 |
|---|---|---|
| committer | Bananeweizen <bananeweizen@gmx.de> | 2011-07-23 21:52:13 +0200 |
| commit | 0454ba218fd2d9add5c663c9799b80b59a259eff (patch) | |
| tree | ccabf3e3be73210fdfa4cb39633731e84122f16d /src/cgeo/geocaching/sorting/AbstractCacheComparator.java | |
| parent | 6d33eb1f7022544c145400845a4f084f1610ffa8 (diff) | |
| download | cgeo-0454ba218fd2d9add5c663c9799b80b59a259eff.zip cgeo-0454ba218fd2d9add5c663c9799b80b59a259eff.tar.gz cgeo-0454ba218fd2d9add5c663c9799b80b59a259eff.tar.bz2 | |
move all comparators to a new package
extract interface and common implementation details of sorting
Diffstat (limited to 'src/cgeo/geocaching/sorting/AbstractCacheComparator.java')
| -rw-r--r-- | src/cgeo/geocaching/sorting/AbstractCacheComparator.java | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/src/cgeo/geocaching/sorting/AbstractCacheComparator.java b/src/cgeo/geocaching/sorting/AbstractCacheComparator.java new file mode 100644 index 0000000..4630888 --- /dev/null +++ b/src/cgeo/geocaching/sorting/AbstractCacheComparator.java @@ -0,0 +1,42 @@ +package cgeo.geocaching.sorting; + +import android.util.Log; +import cgeo.geocaching.cgCache; +import cgeo.geocaching.cgSettings; + +/** + * 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); +} |
