aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/enumerations/CacheSize.java
blob: 10c8c9f1adcb1d5ab32c090286bad1d18078c0b4 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package cgeo.geocaching.enumerations;

import cgeo.geocaching.CgeoApplication;
import cgeo.geocaching.R;

import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
 * Enum listing cache sizes
 */
public enum CacheSize {
    NANO("Nano", 0, R.string.cache_size_nano, "nano"), // used by OC only
    MICRO("Micro", 1, R.string.cache_size_micro, "micro"),
    SMALL("Small", 2, R.string.cache_size_small, "small"),
    REGULAR("Regular", 3, R.string.cache_size_regular, "regular"),
    LARGE("Large", 4, R.string.cache_size_large, "large"),
    VERY_LARGE("Very large", 5, R.string.cache_size_very_large, "xlarge"), // used by OC only
    NOT_CHOSEN("Not chosen", 6, R.string.cache_size_notchosen, ""),
    VIRTUAL("Virtual", 7, R.string.cache_size_virtual, "none"),
    OTHER("Other", 8, R.string.cache_size_other, "other"),
    UNKNOWN("Unknown", -1, R.string.cache_size_unknown, ""); // CacheSize not init. yet

    @NonNull
    public final String id;
    public final int comparable;
    private final int stringId;
    /**
     * lookup for OC JSON requests (the numeric size is deprecated for OC)
     */
    private final String ocSize2;

    CacheSize(@NonNull final String id, final int comparable, final int stringId, final String ocSize2) {
        this.id = id;
        this.comparable = comparable;
        this.stringId = stringId;
        this.ocSize2 = ocSize2;
    }

    @NonNull
    final private static Map<String, CacheSize> FIND_BY_ID = new HashMap<>();
    static {
        for (final CacheSize cs : values()) {
            FIND_BY_ID.put(cs.id.toLowerCase(Locale.US), cs);
            FIND_BY_ID.put(cs.ocSize2.toLowerCase(Locale.US), cs);
        }
        // add medium as additional string for Regular
        FIND_BY_ID.put("medium", CacheSize.REGULAR);
    }

    @NonNull
    public static CacheSize getById(@Nullable final String id) {
        if (id == null) {
            return UNKNOWN;
        }
        // avoid String operations for performance reasons
        final CacheSize result = CacheSize.FIND_BY_ID.get(id);
        if (result != null) {
            return result;
        }
        // only if String was not found, normalize it
        final CacheSize resultNormalized = CacheSize.FIND_BY_ID.get(id.toLowerCase(Locale.US).trim());
        if (resultNormalized != null) {
            return resultNormalized;
        }
        return getByNumber(id);
    }

    /**
     * Bad GPX files can contain the container size encoded as number.
     */
    @NonNull
    private static CacheSize getByNumber(final String id) {
        try {
            final int numerical = Integer.parseInt(id);
            if (numerical != 0) {
                for (final CacheSize size : CacheSize.values()) {
                    if (size.comparable == numerical) {
                        return size;
                    }
                }
            }
        } catch (final NumberFormatException ignored) {
            // ignore, as this might be a number or not
        }
        return UNKNOWN;
    }

    @NonNull
    public final String getL10n() {
        return CgeoApplication.getInstance().getBaseContext().getResources().getString(stringId);
    }
}