001 /*
002 * Java GPX Library (jpx-3.1.0).
003 * Copyright (c) 2016-2023 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 (franz.wilhelmstoetter@gmail.com)
019 */
020 package io.jenetics.jpx;
021
022 import static java.lang.String.format;
023
024 import java.net.URI;
025 import java.net.URISyntaxException;
026 import java.time.Duration;
027 import java.time.Year;
028
029 /**
030 * Some helper methods for parsing GPS values.
031 *
032 * @author <a href="mailto:franz.wilhelmstoetter@gmail.com">Franz Wilhelmstötter</a>
033 * @version 1.2
034 * @since 1.0
035 */
036 final class Format {
037
038 private Format() {
039 }
040
041 static Double parseDouble(final String value) {
042 final String d = Strings.trim(value);
043 return d != null ? Double.parseDouble(d) : null;
044 }
045
046 /**
047 * Convert the given {@code object} into a duration value. If the
048 * {@code object} is {@code null}, {@code null} is returned.
049 *
050 * @param value the object to convert
051 * @return the converted object
052 */
053 static Duration parseDuration(final String value) {
054 final String duration = Strings.trim(value);
055 return duration != null
056 ? Duration.ofSeconds(Long.parseLong(duration))
057 : null;
058 }
059
060 static String toDurationString(final Duration duration) {
061 return duration != null ? Long.toString(duration.getSeconds()) : null;
062 }
063
064 /**
065 * Convert the given {@code object} into a year value. If the
066 * {@code object} is {@code null}, {@code null} is returned.
067 *
068 * @param value the string value to parse
069 * @return the converted object
070 */
071 static Year parseYear(final String value) {
072 final String year = Strings.trim(value);
073 return year != null ? Year.of(Integer.parseInt(year)) : null;
074 }
075
076 static String toYearString(final Year year) {
077 return year != null ? Integer.toString(year.getValue()) : null;
078 }
079
080 /**
081 * Convert the given {@code object} into a URI value. If the
082 * {@code object} is {@code null}, {@code null} is returned.
083 *
084 * @param value the string to convert
085 * @return the converted object
086 */
087 static URI parseURI(final String value) {
088 try {
089 final String uri = Strings.trim(value);
090 return uri != null ? new URI(uri) : null;
091 } catch (URISyntaxException e) {
092 throw new IllegalArgumentException(format(
093 "Invalid URI value: '%s'.", value
094 ));
095 }
096 }
097
098 static String toUriString(final URI uri) {
099 return uri != null ? uri.toString() : null;
100 }
101
102 static String toIntString(final Number number) {
103 return number != null ? Integer.toString(number.intValue()) : null;
104 }
105
106 }
|