001/* 002 * Java GPX Library (jpx-2.0.0). 003 * Copyright (c) 2016-2020 Franz Wilhelmstötter 004 * 005 * Licensed under the Apache License, Version 2.0 (the "License"); 006 * you may not use this file except in compliance with the License. 007 * You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 * 017 * Author: 018 * Franz Wilhelmstötter ([email protected]) 019 */ 020package io.jenetics.jpx; 021 022import static java.util.Objects.requireNonNull; 023 024import java.util.Optional; 025 026/** 027 * Type of GPS fix. {@code none} means GPS had no fix. To signify "the fix info 028 * is unknown, leave out {@code Fix} entirely. {@code pps} = military signal 029 * used. 030 * 031 * @author <a href="mailto:[email protected]">Franz Wilhelmstötter</a> 032 * @version 1.2 033 * @since 1.0 034 */ 035public enum Fix { 036 037 NONE("none"), 038 DIM_2("2d"), 039 DIM_3("3d"), 040 DGPS("dgps"), 041 PPS("pps"); 042 043 private final String _value; 044 045 Fix(final String value) { 046 _value = requireNonNull(value); 047 } 048 049 /** 050 * Return the string representation of the GPS {@code Fix}. {@code none}, 051 * {@code 2d}. {@code 3d}, {@code dgps} or {@code pps}. 052 * 053 * @return the string representation of the GPS {@code Fix} 054 */ 055 public String getValue() { 056 return _value; 057 } 058 059 /** 060 * Return the {@code Fix} constant for the given fix {@code value}. 061 * 062 * @param name the GPS fix names 063 * @return the GPS fix for the given value, or {@code Optional.empty()} if 064 * the given {@code name} is invalid 065 */ 066 public static Optional<Fix> ofName(final String name) { 067 switch (name) { 068 case "none": return Optional.of(Fix.NONE); 069 case "2d": return Optional.of(Fix.DIM_2); 070 case "3d": return Optional.of(Fix.DIM_3); 071 case "dgps": return Optional.of(Fix.DGPS); 072 case "pps": return Optional.of(Fix.PPS); 073 default: return Optional.empty(); 074 } 075 } 076 077 static String format(final Fix fix) { 078 return fix != null ? fix._value : null; 079 } 080 081 static Fix parse(final String value) { 082 final String fix = Strings.trim(value); 083 084 return fix != null 085 ? Fix.ofName(fix).orElseThrow(() -> 086 new IllegalArgumentException(String.format( 087 "Invalid value for: '%s'.", fix))) 088 : null; 089 } 090}