blob: 0548345666a09acd85271daf8abc0fac8a162735 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package cgeo.geocaching.filter;
import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.DataStore;
import cgeo.geocaching.Geocache;
import cgeo.geocaching.R;
import cgeo.geocaching.enumerations.LogType;
import org.eclipse.jdt.annotation.NonNull;
import java.util.ArrayList;
import java.util.List;
/**
* filters caches by popularity ratio (favorites per find in %).
*/
class PopularityRatioFilter extends AbstractFilter {
private final int minRatio;
private final int maxRatio;
public PopularityRatioFilter(@NonNull final String name, final int minRatio, final int maxRatio) {
super(name);
this.minRatio = minRatio;
this.maxRatio = maxRatio;
}
@Override
public boolean accepts(@NonNull final Geocache cache) {
final int finds = getFindsCount(cache);
if (finds == 0) { // Prevent division by zero
return false;
}
final int favorites = cache.getFavoritePoints();
final float ratio = 100.0f * favorites / finds;
return ratio > minRatio && ratio <= maxRatio;
}
private static int getFindsCount(final Geocache cache) {
if (cache.getLogCounts().isEmpty()) {
cache.setLogCounts(DataStore.loadLogCounts(cache.getGeocode()));
}
final Integer logged = cache.getLogCounts().get(LogType.FOUND_IT);
if (logged != null) {
return logged;
}
return 0;
}
public static class Factory implements IFilterFactory {
private static final int[] RATIOS = { 10, 20, 30, 40, 50, 75 };
@Override
@NonNull
public List<IFilter> getFilters() {
final List<IFilter> filters = new ArrayList<>(RATIOS.length);
for (final int minRange : RATIOS) {
final int maxRange = Integer.MAX_VALUE;
final String name = CgeoApplication.getInstance().getResources().getString(R.string.more_than_percent_favorite_points, minRange);
filters.add(new PopularityRatioFilter(name, minRange, maxRange));
}
return filters;
}
}
}
|