diff options
Diffstat (limited to 'src/cgeo/geocaching/sorting')
| -rw-r--r-- | src/cgeo/geocaching/sorting/AbstractCacheComparator.java | 42 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/CacheComparator.java | 9 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/DifficultyComparator.java | 25 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/DistanceComparator.java | 65 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/GeocodeComparator.java | 26 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/InventoryComparator.java | 28 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/NameComparator.java | 20 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/PopularityComparator.java | 25 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/RatingComparator.java | 36 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/SizeComparator.java | 48 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/TerrainComparator.java | 25 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/VisitComparator.java | 26 | ||||
| -rw-r--r-- | src/cgeo/geocaching/sorting/VoteComparator.java | 39 |
13 files changed, 414 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); +} diff --git a/src/cgeo/geocaching/sorting/CacheComparator.java b/src/cgeo/geocaching/sorting/CacheComparator.java new file mode 100644 index 0000000..c9b5150 --- /dev/null +++ b/src/cgeo/geocaching/sorting/CacheComparator.java @@ -0,0 +1,9 @@ +package cgeo.geocaching.sorting; + +import java.util.Comparator; + +import cgeo.geocaching.cgCache; + +public interface CacheComparator extends Comparator<cgCache> { + +} diff --git a/src/cgeo/geocaching/sorting/DifficultyComparator.java b/src/cgeo/geocaching/sorting/DifficultyComparator.java new file mode 100644 index 0000000..cb16b7c --- /dev/null +++ b/src/cgeo/geocaching/sorting/DifficultyComparator.java @@ -0,0 +1,25 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by difficulty + * + */ +public class DifficultyComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.difficulty != null && cache2.difficulty != null; + } + + @Override + protected int compareCaches(final cgCache cache1, final cgCache cache2) { + if (cache1.difficulty > cache2.difficulty) { + return 1; + } else if (cache2.difficulty > cache1.difficulty) { + return -1; + } + return 0; + } +}
\ No newline at end of file diff --git a/src/cgeo/geocaching/sorting/DistanceComparator.java b/src/cgeo/geocaching/sorting/DistanceComparator.java new file mode 100644 index 0000000..fc7b712 --- /dev/null +++ b/src/cgeo/geocaching/sorting/DistanceComparator.java @@ -0,0 +1,65 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgBase; +import cgeo.geocaching.cgCache; + +/** + * sorts caches by distance to current position + * + */ +public class DistanceComparator extends AbstractCacheComparator { + private Double latitude = null; + private Double longitude = null; + + public DistanceComparator() { + // nothing + } + + public DistanceComparator(Double latitudeIn, Double longitudeIn) { + setCoords(latitudeIn, longitudeIn); + } + + public void setCoords(Double latitudeIn, Double longitudeIn) { + latitude = latitudeIn; + longitude = longitudeIn; + } + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(final cgCache cache1, final cgCache cache2) { + 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) { + return -1; + } else if (distance1 > distance2) { + return 1; + } + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/GeocodeComparator.java b/src/cgeo/geocaching/sorting/GeocodeComparator.java new file mode 100644 index 0000000..dd16f08 --- /dev/null +++ b/src/cgeo/geocaching/sorting/GeocodeComparator.java @@ -0,0 +1,26 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by GC code, therefore effectively sorting by cache age + * + */ +public class GeocodeComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.geocode != null && cache1.geocode.length() > 0 + && cache2.geocode != null && cache2.geocode.length() > 0; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + if (cache1.geocode.length() > cache2.geocode.length()) { + return 1; + } else if (cache2.geocode.length() > cache1.geocode.length()) { + return -1; + } + return cache1.geocode.compareToIgnoreCase(cache2.geocode); + } +} diff --git a/src/cgeo/geocaching/sorting/InventoryComparator.java b/src/cgeo/geocaching/sorting/InventoryComparator.java new file mode 100644 index 0000000..c126e41 --- /dev/null +++ b/src/cgeo/geocaching/sorting/InventoryComparator.java @@ -0,0 +1,28 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by number of items in inventory + * @author bananeweizen + * + */ +public class InventoryComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + int itemCount1 = cache1.inventoryItems; + int itemCount2 = cache2.inventoryItems; + if (itemCount1 < itemCount2) { + return 1; + } else if (itemCount2 < itemCount1) { + return -1; + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/NameComparator.java b/src/cgeo/geocaching/sorting/NameComparator.java new file mode 100644 index 0000000..f1c5ae3 --- /dev/null +++ b/src/cgeo/geocaching/sorting/NameComparator.java @@ -0,0 +1,20 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by name + * + */ +public class NameComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.name != null && cache2.name != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + return cache1.name.compareToIgnoreCase(cache2.name); + } +} diff --git a/src/cgeo/geocaching/sorting/PopularityComparator.java b/src/cgeo/geocaching/sorting/PopularityComparator.java new file mode 100644 index 0000000..cc1a6ea --- /dev/null +++ b/src/cgeo/geocaching/sorting/PopularityComparator.java @@ -0,0 +1,25 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by popularity (favorite count) + * + */ +public class PopularityComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.favouriteCnt != null && cache2.favouriteCnt != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + if (cache1.favouriteCnt < cache2.favouriteCnt) { + return 1; + } else if (cache2.favouriteCnt < cache1.favouriteCnt) { + return -1; + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/RatingComparator.java b/src/cgeo/geocaching/sorting/RatingComparator.java new file mode 100644 index 0000000..3cdf474 --- /dev/null +++ b/src/cgeo/geocaching/sorting/RatingComparator.java @@ -0,0 +1,36 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by gcvote.com rating + * + */ +public class RatingComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.rating != null && cache2.rating != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + Float rating1 = cache1.rating; + Float rating2 = cache2.rating; + + // voting can be disabled for caches, then assume an average rating instead + if (rating1 == 0.0) { + rating1 = 2.5f; + } + if (rating2 == 0.0) { + rating2 = 2.5f; + } + + if (rating1 < rating2) { + return 1; + } else if (rating2 < rating1) { + return -1; + } + return 0; + } +}
\ No newline at end of file diff --git a/src/cgeo/geocaching/sorting/SizeComparator.java b/src/cgeo/geocaching/sorting/SizeComparator.java new file mode 100644 index 0000000..f5bd643 --- /dev/null +++ b/src/cgeo/geocaching/sorting/SizeComparator.java @@ -0,0 +1,48 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by size + * + */ +public class SizeComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.size != null && cache1.size.length() > 0 && cache2.size != null && cache2.size.length() > 0; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + int size1 = getSize(cache1); + int size2 = getSize(cache2); + if (size1 < size2) { + return 1; + } else if (size2 < size1) { + return -1; + } + return 0; + } + + /** + * speed optimized comparison of size string + * @param cache + * @return + */ + private int getSize(final cgCache cache) { + char c = cache.size.charAt(0); + switch (c) { + case 'm': // micro + return 1; + case 's': // small + return 2; + case 'r': // regular + return 3; + case 'l': // large + return 4; + default: + return 0; + } + } +}
\ No newline at end of file diff --git a/src/cgeo/geocaching/sorting/TerrainComparator.java b/src/cgeo/geocaching/sorting/TerrainComparator.java new file mode 100644 index 0000000..bf66af9 --- /dev/null +++ b/src/cgeo/geocaching/sorting/TerrainComparator.java @@ -0,0 +1,25 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by terrain rating + * + */ +public class TerrainComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.terrain != null && cache2.terrain != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + if (cache1.terrain > cache2.terrain) { + return 1; + } else if (cache2.terrain > cache1.terrain) { + return -1; + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/VisitComparator.java b/src/cgeo/geocaching/sorting/VisitComparator.java new file mode 100644 index 0000000..79a8ac5 --- /dev/null +++ b/src/cgeo/geocaching/sorting/VisitComparator.java @@ -0,0 +1,26 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by last visited date + * + */ +public class VisitComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.visitedDate != null && cache1.visitedDate > 0 + && cache2.visitedDate != null && cache2.visitedDate > 0; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + if (cache1.visitedDate > cache2.visitedDate) { + return -1; + } else if (cache1.visitedDate < cache2.visitedDate) { + return 1; + } + return 0; + } +} diff --git a/src/cgeo/geocaching/sorting/VoteComparator.java b/src/cgeo/geocaching/sorting/VoteComparator.java new file mode 100644 index 0000000..0039c7c --- /dev/null +++ b/src/cgeo/geocaching/sorting/VoteComparator.java @@ -0,0 +1,39 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sorts caches by the users own voting (if available at all) + * + * @author bananeweizen + * + */ +public class VoteComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + // if there is no vote available, put that cache at the end of the list + float vote1 = 0; + if (cache1.myVote != null) { + vote1 = cache1.myVote; + } + + float vote2 = 0; + if (cache2.myVote != null) { + vote2 = cache2.myVote; + } + + // compare + if (vote1 < vote2) { + return 1; + } else if (vote2 < vote1) { + return -1; + } + return 0; + } +} |
