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.java80
1 files changed, 34 insertions, 46 deletions
diff --git a/main/src/cgeo/geocaching/gcvote/GCVote.java b/main/src/cgeo/geocaching/gcvote/GCVote.java
index 868be2b..c3c7b8d 100644
--- a/main/src/cgeo/geocaching/gcvote/GCVote.java
+++ b/main/src/cgeo/geocaching/gcvote/GCVote.java
@@ -20,16 +20,16 @@ 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);
+ 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);
/**
* Get user rating for a given guid or geocode. For a guid first the ratings cache is checked
@@ -40,8 +40,8 @@ public final class GCVote {
* @return
*/
public static GCVoteRating getRating(String guid, String geocode) {
- if (StringUtils.isNotBlank(guid) && ratingsCache.containsKey(guid)) {
- return ratingsCache.get(guid);
+ if (StringUtils.isNotBlank(guid) && RATINGS_CACHE.containsKey(guid)) {
+ return RATINGS_CACHE.get(guid);
}
final Map<String, GCVoteRating> ratings = getRating(singletonOrNull(guid), singletonOrNull(geocode));
@@ -88,7 +88,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 +97,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,26 +119,22 @@ 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;
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) {
@@ -155,11 +143,11 @@ public final class GCVote {
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) {
@@ -169,11 +157,11 @@ public final class GCVote {
float myVote = 0;
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 +169,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);
}