aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/gcvote/GCVote.java
diff options
context:
space:
mode:
Diffstat (limited to 'main/src/cgeo/geocaching/gcvote/GCVote.java')
-rw-r--r--main/src/cgeo/geocaching/gcvote/GCVote.java130
1 files changed, 66 insertions, 64 deletions
diff --git a/main/src/cgeo/geocaching/gcvote/GCVote.java b/main/src/cgeo/geocaching/gcvote/GCVote.java
index 868be2b..f20a6d4 100644
--- a/main/src/cgeo/geocaching/gcvote/GCVote.java
+++ b/main/src/cgeo/geocaching/gcvote/GCVote.java
@@ -16,20 +16,28 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
+import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;
public final class GCVote {
- private static final Pattern patternLogIn = Pattern.compile("loggedIn='([^']+)'", Pattern.CASE_INSENSITIVE);
- private static final Pattern patternGuid = Pattern.compile("cacheId='([^']+)'", Pattern.CASE_INSENSITIVE);
- private static final Pattern patternWaypoint = Pattern.compile("waypoint='([^']+)'", Pattern.CASE_INSENSITIVE);
- private static final Pattern patternRating = Pattern.compile("voteAvg='([0-9.]+)'", Pattern.CASE_INSENSITIVE);
- private static final Pattern patternVotes = Pattern.compile("voteCnt='([0-9]+)'", Pattern.CASE_INSENSITIVE);
- private static final Pattern patternVote = Pattern.compile("voteUser='([0-9.]+)'", Pattern.CASE_INSENSITIVE);
- private static final Pattern patternVoteElement = Pattern.compile("<vote ([^>]+)>", Pattern.CASE_INSENSITIVE);
+ public static final float NO_RATING = 0;
+ private static final Pattern PATTERN_LOG_IN = Pattern.compile("loggedIn='([^']+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern PATTERN_GUID = Pattern.compile("cacheId='([^']+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern PATTERN_WAYPOINT = Pattern.compile("waypoint='([^']+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern PATTERN_RATING = Pattern.compile("voteAvg='([0-9.]+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern PATTERN_VOTES = Pattern.compile("voteCnt='([0-9]+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern PATTERN_VOTE = Pattern.compile("voteUser='([0-9.]+)'", Pattern.CASE_INSENSITIVE);
+ private static final Pattern PATTERN_VOTE_ELEMENT = Pattern.compile("<vote ([^>]+)>", Pattern.CASE_INSENSITIVE);
private static final int MAX_CACHED_RATINGS = 1000;
- private static LeastRecentlyUsedMap<String, GCVoteRating> ratingsCache = new LeastRecentlyUsedMap.LruCache<String, GCVoteRating>(MAX_CACHED_RATINGS);
+ private static final LeastRecentlyUsedMap<String, GCVoteRating> RATINGS_CACHE = new LeastRecentlyUsedMap.LruCache<String, GCVoteRating>(MAX_CACHED_RATINGS);
+ private static final float MIN_RATING = 1;
+ private static final float MAX_RATING = 5;
+
+ private GCVote() {
+ // utility class
+ }
/**
* Get user rating for a given guid or geocode. For a guid first the ratings cache is checked
@@ -39,9 +47,9 @@ public final class GCVote {
* @param geocode
* @return
*/
- public static GCVoteRating getRating(String guid, String geocode) {
- if (StringUtils.isNotBlank(guid) && ratingsCache.containsKey(guid)) {
- return ratingsCache.get(guid);
+ public static GCVoteRating getRating(final String guid, final String geocode) {
+ if (StringUtils.isNotBlank(guid) && RATINGS_CACHE.containsKey(guid)) {
+ return RATINGS_CACHE.get(guid);
}
final Map<String, GCVoteRating> ratings = getRating(singletonOrNull(guid), singletonOrNull(geocode));
@@ -59,7 +67,7 @@ public final class GCVote {
* @param geocodes
* @return
*/
- public static Map<String, GCVoteRating> getRating(List<String> guids, List<String> geocodes) {
+ public static Map<String, GCVoteRating> getRating(final List<String> guids, final List<String> geocodes) {
if (guids == null && geocodes == null) {
return null;
}
@@ -88,7 +96,7 @@ public final class GCVote {
return null;
}
- final MatcherWrapper matcherVoteElement = new MatcherWrapper(patternVoteElement, page);
+ final MatcherWrapper matcherVoteElement = new MatcherWrapper(PATTERN_VOTE_ELEMENT, page);
while (matcherVoteElement.find()) {
String voteData = matcherVoteElement.group(1);
if (voteData == null) {
@@ -97,29 +105,21 @@ public final class GCVote {
String id = null;
String guid = null;
- try {
- final MatcherWrapper matcherGuid = new MatcherWrapper(patternGuid, voteData);
- if (matcherGuid.find()) {
- if (matcherGuid.groupCount() > 0) {
- guid = matcherGuid.group(1);
- if (requestByGuids) {
- id = guid;
- }
+ final MatcherWrapper matcherGuid = new MatcherWrapper(PATTERN_GUID, voteData);
+ if (matcherGuid.find()) {
+ if (matcherGuid.groupCount() > 0) {
+ guid = matcherGuid.group(1);
+ if (requestByGuids) {
+ id = guid;
}
}
- } catch (Exception e) {
- Log.w("GCVote.getRating: Failed to parse guid");
}
if (!requestByGuids) {
- try {
- final MatcherWrapper matcherWp = new MatcherWrapper(patternWaypoint, voteData);
- if (matcherWp.find()) {
- if (matcherWp.groupCount() > 0) {
- id = matcherWp.group(1);
- }
+ final MatcherWrapper matcherWp = new MatcherWrapper(PATTERN_WAYPOINT, voteData);
+ if (matcherWp.find()) {
+ if (matcherWp.groupCount() > 0) {
+ id = matcherWp.group(1);
}
- } catch (Exception e) {
- Log.w("GCVote.getRating: Failed to parse waypoint");
}
}
if (id == null) {
@@ -127,53 +127,49 @@ public final class GCVote {
}
boolean loggedIn = false;
- try {
- final MatcherWrapper matcherLoggedIn = new MatcherWrapper(patternLogIn, page);
- if (matcherLoggedIn.find()) {
- if (matcherLoggedIn.groupCount() > 0) {
- if (matcherLoggedIn.group(1).equalsIgnoreCase("true")) {
- loggedIn = true;
- }
+ final MatcherWrapper matcherLoggedIn = new MatcherWrapper(PATTERN_LOG_IN, page);
+ if (matcherLoggedIn.find()) {
+ if (matcherLoggedIn.groupCount() > 0) {
+ if (matcherLoggedIn.group(1).equalsIgnoreCase("true")) {
+ loggedIn = true;
}
}
- } catch (Exception e) {
- Log.w("GCVote.getRating: Failed to parse loggedIn");
}
- float rating = 0;
+ float rating = NO_RATING;
try {
- final MatcherWrapper matcherRating = new MatcherWrapper(patternRating, voteData);
+ final MatcherWrapper matcherRating = new MatcherWrapper(PATTERN_RATING, voteData);
if (matcherRating.find()) {
rating = Float.parseFloat(matcherRating.group(1));
}
- } catch (Exception e) {
+ } catch (NumberFormatException e) {
Log.w("GCVote.getRating: Failed to parse rating");
}
- if (rating <= 0) {
+ if (!isValidRating(rating)) {
continue;
}
int votes = -1;
try {
- final MatcherWrapper matcherVotes = new MatcherWrapper(patternVotes, voteData);
+ final MatcherWrapper matcherVotes = new MatcherWrapper(PATTERN_VOTES, voteData);
if (matcherVotes.find()) {
votes = Integer.parseInt(matcherVotes.group(1));
}
- } catch (Exception e) {
+ } catch (NumberFormatException e) {
Log.w("GCVote.getRating: Failed to parse vote count");
}
if (votes < 0) {
continue;
}
- float myVote = 0;
+ float myVote = NO_RATING;
if (loggedIn) {
try {
- final MatcherWrapper matcherVote = new MatcherWrapper(patternVote, voteData);
+ final MatcherWrapper matcherVote = new MatcherWrapper(PATTERN_VOTE, voteData);
if (matcherVote.find()) {
myVote = Float.parseFloat(matcherVote.group(1));
}
- } catch (Exception e) {
+ } catch (NumberFormatException e) {
Log.w("GCVote.getRating: Failed to parse user's vote");
}
}
@@ -181,10 +177,10 @@ public final class GCVote {
if (StringUtils.isNotBlank(id)) {
GCVoteRating gcvoteRating = new GCVoteRating(rating, votes, myVote);
ratings.put(id, gcvoteRating);
- ratingsCache.put(guid, gcvoteRating);
+ RATINGS_CACHE.put(guid, gcvoteRating);
}
}
- } catch (Exception e) {
+ } catch (RuntimeException e) {
Log.e("GCVote.getRating", e);
}
@@ -198,18 +194,11 @@ public final class GCVote {
* @param vote
* @return {@code true} if the rating was submitted successfully
*/
- public static boolean setRating(Geocache cache, double vote) {
- if (!Settings.isGCvoteLogin()) {
- return false;
- }
- if (!cache.supportsGCVote()) {
+ public static boolean setRating(final Geocache cache, final float vote) {
+ if (!isVotingPossible(cache)) {
return false;
}
- String guid = cache.getGuid();
- if (StringUtils.isBlank(guid)) {
- return false;
- }
- if (vote <= 0.0 || vote > 5.0) {
+ if (!isValidRating(vote)) {
return false;
}
@@ -221,16 +210,16 @@ public final class GCVote {
final Parameters params = new Parameters(
"userName", login.left,
"password", login.right,
- "cacheId", guid,
+ "cacheId", cache.getGuid(),
"voteUser", String.format("%.1f", vote).replace(',', '.'),
"version", "cgeo");
final String result = Network.getResponseData(Network.getRequest("http://gcvote.com/setVote.php", params));
- return result.trim().equalsIgnoreCase("ok");
+ return result != null && result.trim().equalsIgnoreCase("ok");
}
- public static void loadRatings(ArrayList<Geocache> caches) {
+ public static void loadRatings(final ArrayList<Geocache> caches) {
if (!Settings.isRatingWanted()) {
return;
}
@@ -266,4 +255,17 @@ public final class GCVote {
Log.e("GCvote.loadRatings", e);
}
}
+
+ public static boolean isValidRating(final float rating) {
+ return rating >= MIN_RATING && rating <= MAX_RATING;
+ }
+
+ public static String getRatingText(final float rating) {
+ return String.format(Locale.getDefault(), "%.1f", rating);
+ }
+
+ public static boolean isVotingPossible(final Geocache cache) {
+ return Settings.isGCvoteLogin() && StringUtils.isNotBlank(cache.getGuid()) && cache.supportsGCVote();
+ }
+
}