aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/cgeo/geocaching/connector/ConnectorFactory.java
blob: 39e6dee077ec254264835e8ad22d32dd9de938bf (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
package cgeo.geocaching.connector;

import cgeo.geocaching.ICache;
import cgeo.geocaching.connector.opencaching.ApiOpenCachingConnector;
import cgeo.geocaching.connector.opencaching.OpenCachingConnector;

import org.apache.commons.lang3.StringUtils;

public final class ConnectorFactory {
    private static final UnknownConnector UNKNOWN_CONNECTOR = new UnknownConnector();
    private static final IConnector[] connectors = new IConnector[] {
            GCConnector.getInstance(),
            new OpenCachingConnector("OpenCaching.DE", "www.opencaching.de", "OC"),
            new OpenCachingConnector("OpenCaching.CZ", "www.opencaching.cz", "OZ"),
            new ApiOpenCachingConnector("OpenCaching.CO.UK", "www.opencaching.org.uk", "OK", "arU4okouc4GEjMniE2fq"),
            new OpenCachingConnector("OpenCaching.ES", "www.opencachingspain.es", "OC"),
            new OpenCachingConnector("OpenCaching.IT", "www.opencaching.it", "OC"),
            new OpenCachingConnector("OpenCaching.JP", "www.opencaching.jp", "OJ"),
            new OpenCachingConnector("OpenCaching.NO/SE", "www.opencaching.no", "OS"),
            new OpenCachingConnector("OpenCaching.NL", "www.opencaching.nl", "OB"),
            new ApiOpenCachingConnector("OpenCaching.PL", "www.opencaching.pl", "OP", "GkxM47WkUkLQXXsZ9qSh"),
            new ApiOpenCachingConnector("OpenCaching.US", "www.opencaching.us", "OU", "pTsYAYSXFcfcRQnYE6uA"),
            new OXConnector(),
            new GeocachingAustraliaConnector(),
            new GeopeitusConnector(),
            UNKNOWN_CONNECTOR // the unknown connector MUST be the last one
    };

    public static IConnector[] getConnectors() {
        return connectors;
    }

    public static boolean canHandle(final String geocode) {
        if (isInvalidGeocode(geocode)) {
            return false;
        }
        for (IConnector connector : connectors) {
            if (connector.canHandle(geocode)) {
                return true;
            }
        }
        return false;
    }

    public static IConnector getConnector(ICache cache) {
        return getConnector(cache.getGeocode());
    }

    public static IConnector getConnector(String geocode) {
        if (isInvalidGeocode(geocode)) {
            return UNKNOWN_CONNECTOR;
        }
        for (IConnector connector : connectors) {
            if (connector.canHandle(geocode)) {
                return connector;
            }
        }
        // in case of errors, take UNKNOWN
        return UNKNOWN_CONNECTOR;
    }

    private static boolean isInvalidGeocode(final String geocode) {
        return StringUtils.isBlank(geocode) || !Character.isLetterOrDigit(geocode.charAt(0));
    }
}