Fix.java
01 /*
02  * Java GPX Library (jpx-3.1.0).
03  * Copyright (c) 2016-2023 Franz Wilhelmstötter
04  *
05  * Licensed under the Apache License, Version 2.0 (the "License");
06  * you may not use this file except in compliance with the License.
07  * You may obtain a copy of the License at
08  *
09  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * Author:
18  *    Franz Wilhelmstötter (franz.wilhelmstoetter@gmail.com)
19  */
20 package io.jenetics.jpx;
21 
22 import static java.util.Objects.requireNonNull;
23 
24 import java.util.Optional;
25 
26 /**
27  * Type of GPS fix. {@code none} means GPS had no fix. To signify "the fix info
28  * is unknown, leave out {@code Fix} entirely. {@code pps} = military signal
29  * used.
30  *
31  @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
32  @version 1.2
33  @since 1.0
34  */
35 public enum  Fix {
36 
37     NONE("none"),
38     DIM_2("2d"),
39     DIM_3("3d"),
40     DGPS("dgps"),
41     PPS("pps");
42 
43     private final String _value;
44 
45     Fix(final String value) {
46         _value = requireNonNull(value);
47     }
48 
49     /**
50      * Return the string representation of the GPS {@code Fix}. {@code none},
51      * {@code 2d}. {@code 3d}, {@code dgps} or {@code pps}.
52      *
53      @return the string representation of the GPS {@code Fix}
54      */
55     public String getValue() {
56         return _value;
57     }
58 
59     /**
60      * Return the {@code Fix} constant for the given fix {@code value}.
61      *
62      @param name the GPS fix names
63      @return the GPS fix for the given value, or {@code Optional.empty()} if
64      *         the given {@code name} is invalid
65      */
66     public static Optional<Fix> ofName(final String name) {
67         return switch (name) {
68             case "none" -> Optional.of(Fix.NONE);
69             case "2d" -> Optional.of(Fix.DIM_2);
70             case "3d" -> Optional.of(Fix.DIM_3);
71             case "dgps" -> Optional.of(Fix.DGPS);
72             case "pps" -> Optional.of(Fix.PPS);
73             default -> Optional.empty();
74         };
75     }
76 
77     static String format(final Fix fix) {
78         return fix != null ? fix._value : null;
79     }
80 
81     static Fix parse(final String value) {
82         final String fix = Strings.trim(value);
83 
84         return fix != null
85             ? Fix.ofName(fix).orElseThrow(() ->
86                 new IllegalArgumentException(String.format(
87                     "Invalid value for: '%s'.", fix)))
88             null;
89     }
90 }