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.java61
1 files changed, 23 insertions, 38 deletions
diff --git a/main/src/cgeo/geocaching/gcvote/GCVote.java b/main/src/cgeo/geocaching/gcvote/GCVote.java
index 5a00009..a053f31 100644
--- a/main/src/cgeo/geocaching/gcvote/GCVote.java
+++ b/main/src/cgeo/geocaching/gcvote/GCVote.java
@@ -1,21 +1,22 @@
package cgeo.geocaching.gcvote;
+import cgeo.geocaching.Geocache;
import cgeo.geocaching.Settings;
-import cgeo.geocaching.cgCache;
import cgeo.geocaching.network.Network;
import cgeo.geocaching.network.Parameters;
import cgeo.geocaching.utils.LeastRecentlyUsedMap;
import cgeo.geocaching.utils.Log;
+import cgeo.geocaching.utils.MatcherWrapper;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import java.util.ArrayList;
+import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
public final class GCVote {
@@ -38,31 +39,16 @@ public final class GCVote {
* @return
*/
public static GCVoteRating getRating(String guid, String geocode) {
- List<String> guids = null;
- List<String> geocodes = null;
-
- if (StringUtils.isNotBlank(guid)) {
-
- GCVoteRating rating = ratingsCache.get(guid);
- if (rating != null) {
- return rating;
- }
- guids = new ArrayList<String>();
- guids.add(guid);
- } else if (StringUtils.isNotBlank(geocode)) {
- geocodes = new ArrayList<String>();
- geocodes.add(geocode);
- } else {
- return null;
+ if (StringUtils.isNotBlank(guid) && ratingsCache.containsKey(guid)) {
+ return ratingsCache.get(guid);
}
- final Map<String, GCVoteRating> ratings = getRating(guids, geocodes);
-
- if (MapUtils.isEmpty(ratings)) {
- return null;
- }
+ final Map<String, GCVoteRating> ratings = getRating(singletonOrNull(guid), singletonOrNull(geocode));
+ return MapUtils.isNotEmpty(ratings) ? ratings.values().iterator().next() : null;
+ }
- return ratings.values().iterator().next();
+ private static List<String> singletonOrNull(final String item) {
+ return StringUtils.isNotBlank(item) ? Collections.singletonList(item) : null;
}
/**
@@ -98,17 +84,16 @@ public final class GCVote {
return null;
}
- String voteData;
- final Matcher matcherVoteElement = patternVoteElement.matcher(page);
+ final MatcherWrapper matcherVoteElement = new MatcherWrapper(patternVoteElement, page);
while (matcherVoteElement.find()) {
- voteData = matcherVoteElement.group(1);
+ String voteData = matcherVoteElement.group(1);
if (voteData == null) {
continue;
}
String guid = null;
try {
- final Matcher matcherGuid = patternGuid.matcher(voteData);
+ final MatcherWrapper matcherGuid = new MatcherWrapper(patternGuid, voteData);
if (matcherGuid.find()) {
if (matcherGuid.groupCount() > 0) {
guid = matcherGuid.group(1);
@@ -123,7 +108,7 @@ public final class GCVote {
boolean loggedIn = false;
try {
- final Matcher matcherLoggedIn = patternLogIn.matcher(page);
+ final MatcherWrapper matcherLoggedIn = new MatcherWrapper(patternLogIn, page);
if (matcherLoggedIn.find()) {
if (matcherLoggedIn.groupCount() > 0) {
if (matcherLoggedIn.group(1).equalsIgnoreCase("true")) {
@@ -137,7 +122,7 @@ public final class GCVote {
float rating = 0;
try {
- final Matcher matcherRating = patternRating.matcher(voteData);
+ final MatcherWrapper matcherRating = new MatcherWrapper(patternRating, voteData);
if (matcherRating.find()) {
rating = Float.parseFloat(matcherRating.group(1));
}
@@ -150,7 +135,7 @@ public final class GCVote {
int votes = -1;
try {
- final Matcher matcherVotes = patternVotes.matcher(voteData);
+ final MatcherWrapper matcherVotes = new MatcherWrapper(patternVotes, voteData);
if (matcherVotes.find()) {
votes = Integer.parseInt(matcherVotes.group(1));
}
@@ -164,7 +149,7 @@ public final class GCVote {
float myVote = 0;
if (loggedIn) {
try {
- final Matcher matcherVote = patternVote.matcher(voteData);
+ final MatcherWrapper matcherVote = new MatcherWrapper(patternVote, voteData);
if (matcherVote.find()) {
myVote = Float.parseFloat(matcherVote.group(1));
}
@@ -180,7 +165,7 @@ public final class GCVote {
}
}
} catch (Exception e) {
- Log.e("GCVote.getRating: " + e.toString());
+ Log.e("GCVote.getRating", e);
}
return ratings;
@@ -193,7 +178,7 @@ public final class GCVote {
* @param vote
* @return
*/
- public static boolean setRating(cgCache cache, double vote) {
+ public static boolean setRating(Geocache cache, double vote) {
if (!cache.supportsGCVote()) {
return false;
}
@@ -222,13 +207,13 @@ public final class GCVote {
return result.trim().equalsIgnoreCase("ok");
}
- public static void loadRatings(ArrayList<cgCache> caches) {
+ public static void loadRatings(ArrayList<Geocache> caches) {
if (!Settings.isRatingWanted()) {
return;
}
final ArrayList<String> guids = new ArrayList<String>(caches.size());
- for (final cgCache cache : caches) {
+ for (final Geocache cache : caches) {
String guid = cache.getGuid();
if (StringUtils.isNotBlank(guid)) {
guids.add(guid);
@@ -244,7 +229,7 @@ public final class GCVote {
if (MapUtils.isNotEmpty(ratings)) {
// save found cache coordinates
- for (cgCache cache : caches) {
+ for (Geocache cache : caches) {
if (ratings.containsKey(cache.getGuid())) {
GCVoteRating rating = ratings.get(cache.getGuid());
@@ -255,7 +240,7 @@ public final class GCVote {
}
}
} catch (Exception e) {
- Log.e("GCvote.loadRatings: " + e.toString());
+ Log.e("GCvote.loadRatings", e);
}
}
}