diff options
author | Samuel Tardieu <sam@rfc1149.net> | 2011-09-16 14:36:28 +0200 |
---|---|---|
committer | Samuel Tardieu <sam@rfc1149.net> | 2011-09-16 14:36:28 +0200 |
commit | 579ef7a535489d4aa632db11667a3b01deb6cafd (patch) | |
tree | 55810021c02ac7d80d3a9702ef0b59e4af154b9c /main/src/cgeo/geocaching/sorting | |
parent | 96ea21fd50334479c262da692038965d0e4d596a (diff) | |
download | cgeo-579ef7a535489d4aa632db11667a3b01deb6cafd.zip cgeo-579ef7a535489d4aa632db11667a3b01deb6cafd.tar.gz cgeo-579ef7a535489d4aa632db11667a3b01deb6cafd.tar.bz2 |
Move sources into the main directory
This prepares the inclusion of tests into the same repository.
Diffstat (limited to 'main/src/cgeo/geocaching/sorting')
16 files changed, 477 insertions, 0 deletions
diff --git a/main/src/cgeo/geocaching/sorting/AbstractCacheComparator.java b/main/src/cgeo/geocaching/sorting/AbstractCacheComparator.java new file mode 100644 index 0000000..03db8df --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/AbstractCacheComparator.java @@ -0,0 +1,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); +} diff --git a/main/src/cgeo/geocaching/sorting/CacheComparator.java b/main/src/cgeo/geocaching/sorting/CacheComparator.java new file mode 100644 index 0000000..c9b5150 --- /dev/null +++ b/main/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/main/src/cgeo/geocaching/sorting/DateComparator.java b/main/src/cgeo/geocaching/sorting/DateComparator.java new file mode 100644 index 0000000..b59e84e --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/DateComparator.java @@ -0,0 +1,35 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +import java.util.Date; + +/** + * compares caches by date + * + * @author bananeweizen + * + */ +public class DateComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + Date date1 = cache1.hidden; + Date date2 = cache2.hidden; + if (date1 != null && date2 != null) { + return date1.compareTo(date2); + } + if (date1 != null) { + return -1; + } + if (date2 != null) { + return 1; + } + return 0; + } +} diff --git a/main/src/cgeo/geocaching/sorting/DifficultyComparator.java b/main/src/cgeo/geocaching/sorting/DifficultyComparator.java new file mode 100644 index 0000000..846417c --- /dev/null +++ b/main/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/main/src/cgeo/geocaching/sorting/DistanceComparator.java b/main/src/cgeo/geocaching/sorting/DistanceComparator.java new file mode 100644 index 0000000..7051718 --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/DistanceComparator.java @@ -0,0 +1,40 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; +import cgeo.geocaching.geopoint.Geopoint; + +/** + * sorts caches by distance to current position + * + */ +public class DistanceComparator extends AbstractCacheComparator { + private final Geopoint coords; + + public DistanceComparator(final Geopoint coords) { + this.coords = coords; + } + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(final cgCache cache1, final cgCache cache2) { + if ((cache1.coords == null || cache2.coords == null) + && cache1.distance != null && cache2.distance != null) { + return Double.compare(cache1.distance, cache2.distance); + } else { + if (cache1.coords == null) { + return 1; + } + if (cache2.coords == null) { + return -1; + } + + return Float.compare(coords.distanceTo(cache1.coords), + coords.distanceTo(cache2.coords)); + } + } + +} diff --git a/main/src/cgeo/geocaching/sorting/FindsComparator.java b/main/src/cgeo/geocaching/sorting/FindsComparator.java new file mode 100644 index 0000000..efde22d --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/FindsComparator.java @@ -0,0 +1,40 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgBase; +import cgeo.geocaching.cgCache; +import cgeo.geocaching.cgeoapplication; + +public class FindsComparator extends AbstractCacheComparator implements + CacheComparator { + + private cgeoapplication app; + + public FindsComparator(cgeoapplication app) { + this.app = app; + } + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return cache1.logCounts != null && cache2.logCounts != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + int finds1 = getFindsCount(cache1); + int finds2 = getFindsCount(cache2); + return finds2 - finds1; + } + + private int getFindsCount(cgCache cache) { + int finds = 0; + if (cache.logCounts.isEmpty()) { + cache.logCounts = app.loadLogCounts(cache.geocode); + } + Integer logged = cache.logCounts.get(cgBase.LOG_FOUND_IT); + if (logged != null) { + finds = logged; + } + return finds; + } + +} diff --git a/main/src/cgeo/geocaching/sorting/GeocodeComparator.java b/main/src/cgeo/geocaching/sorting/GeocodeComparator.java new file mode 100644 index 0000000..136d7f5 --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/GeocodeComparator.java @@ -0,0 +1,28 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +import org.apache.commons.lang3.StringUtils; + +/** + * 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 StringUtils.isNotBlank(cache1.geocode) + && StringUtils.isNotBlank(cache2.geocode); + } + + @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/main/src/cgeo/geocaching/sorting/InventoryComparator.java b/main/src/cgeo/geocaching/sorting/InventoryComparator.java new file mode 100644 index 0000000..0833094 --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/InventoryComparator.java @@ -0,0 +1,29 @@ +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/main/src/cgeo/geocaching/sorting/NameComparator.java b/main/src/cgeo/geocaching/sorting/NameComparator.java new file mode 100644 index 0000000..d301180 --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/NameComparator.java @@ -0,0 +1,22 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +import org.apache.commons.lang3.StringUtils; + +/** + * sorts caches by name + * + */ +public class NameComparator extends AbstractCacheComparator { + + @Override + protected boolean canCompare(cgCache cache1, cgCache cache2) { + return StringUtils.isNotBlank(cache1.name) && StringUtils.isNotBlank(cache2.name); + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + return cache1.name.compareToIgnoreCase(cache2.name); + } +} diff --git a/main/src/cgeo/geocaching/sorting/PopularityComparator.java b/main/src/cgeo/geocaching/sorting/PopularityComparator.java new file mode 100644 index 0000000..dbc418a --- /dev/null +++ b/main/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/main/src/cgeo/geocaching/sorting/RatingComparator.java b/main/src/cgeo/geocaching/sorting/RatingComparator.java new file mode 100644 index 0000000..b7140c6 --- /dev/null +++ b/main/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/main/src/cgeo/geocaching/sorting/SizeComparator.java b/main/src/cgeo/geocaching/sorting/SizeComparator.java new file mode 100644 index 0000000..a633a06 --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/SizeComparator.java @@ -0,0 +1,20 @@ +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 && cache2.size != null; + } + + @Override + protected int compareCaches(cgCache cache1, cgCache cache2) { + return cache2.size.comparable - cache1.size.comparable; + } +}
\ No newline at end of file diff --git a/main/src/cgeo/geocaching/sorting/StateComparator.java b/main/src/cgeo/geocaching/sorting/StateComparator.java new file mode 100644 index 0000000..f56d3fe --- /dev/null +++ b/main/src/cgeo/geocaching/sorting/StateComparator.java @@ -0,0 +1,32 @@ +package cgeo.geocaching.sorting; + +import cgeo.geocaching.cgCache; + +/** + * sort caches by state (normal, disabled, archived) + * + */ +public class StateComparator extends AbstractCacheComparator implements + CacheComparator { + + @Override + protected boolean canCompare(final cgCache cache1, final cgCache cache2) { + return true; + } + + @Override + protected int compareCaches(final cgCache cache1, final cgCache cache2) { + return getState(cache1) - getState(cache2); + } + + private static int getState(final cgCache cache) { + if (cache.disabled) { + return 1; + } + if (cache.archived) { + return 2; + } + return 0; + } + +} diff --git a/main/src/cgeo/geocaching/sorting/TerrainComparator.java b/main/src/cgeo/geocaching/sorting/TerrainComparator.java new file mode 100644 index 0000000..88c75e4 --- /dev/null +++ b/main/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/main/src/cgeo/geocaching/sorting/VisitComparator.java b/main/src/cgeo/geocaching/sorting/VisitComparator.java new file mode 100644 index 0000000..597efb5 --- /dev/null +++ b/main/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/main/src/cgeo/geocaching/sorting/VoteComparator.java b/main/src/cgeo/geocaching/sorting/VoteComparator.java new file mode 100644 index 0000000..2a4b4ac --- /dev/null +++ b/main/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; + } +} |