aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/geo/ParsedDegree.java
blob: 0c1ad1cb7c60e40e35c636d59e24a3856932fa29 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.geo;

/**
 * Utility for holding one geographical coordinate
 *
 * @author arnej27959
 */
public class ParsedDegree {

    /**
     * The parsed latitude or longitude
     * Degrees north or east if positive
     * Degrees south or west if negative
     */
    public final double degrees;

    // one of these two flag will be true:
    public final boolean isLatitude;
    public final boolean isLongitude;

    public ParsedDegree(double value, boolean isLat, boolean isLon) {
        this.degrees = value;
        this.isLatitude = isLat;
        this.isLongitude = isLon;
        if (isLat && isLon) {
            throw new IllegalArgumentException("value cannot be both latitude and longitude at once");
        }
        if (isLat || isLon) {
            return;
        }
        throw new IllegalArgumentException("value must be either latitude or longitude");
    }

    static public ParsedDegree fromString(String toParse, boolean assumeLatitude, boolean assumeLongitude) {
        if (assumeLatitude && assumeLongitude) {
            throw new IllegalArgumentException("value cannot be both latitude and longitude at once");
        }
        var parser = new OneDegreeParser(assumeLatitude, toParse);
        if (parser.foundLatitude) {
            return new ParsedDegree(parser.latitude, true, false);
        }
        if (parser.foundLongitude) {
            return new ParsedDegree(parser.longitude, false, true);
        }
        throw new IllegalArgumentException("could not parse: "+toParse);
    }

    @Override
    public String toString() {
        if (isLatitude) {
            return "Latitude: "+degrees+" degrees";
        } else {
            return "Longitude: "+degrees+" degrees";
        }
    }

}