LongitudeDegree.java
01 /*
02  * Java GPX Library (jpx-3.1.0).
03  *
04  * Licensed under the Apache License, Version 2.0 (the "License");
05  * you may not use this file except in compliance with the License.
06  * You may obtain a copy of the License at
07  *
08  *      http://www.apache.org/licenses/LICENSE-2.0
09  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package io.jenetics.jpx.format;
17 
18 import static java.lang.Math.abs;
19 
20 import java.text.ParsePosition;
21 import java.util.Optional;
22 
23 import io.jenetics.jpx.Longitude;
24 
25 /**
26  * This field allows to access the longitude degrees of a given location. If the
27  * pattern has a fractional part, the longitude is rounded to match the pattern.
28  * If the pattern has no fractional part, the longitude is truncated rather than
29  * rounded, on the assumption that the fractional part will be represented by
30  * minutes and seconds.
31  *
32  @version 2.2
33  @since 2.2
34  */
35 class LongitudeDegree extends Field {
36 
37     LongitudeDegree(final String pattern) {
38         super(pattern, 'd');
39     }
40 
41     @Override
42     public void parse(
43         final CharSequence in,
44         final ParsePosition pos,
45         final LocationBuilder builder
46     ) {
47         double d = parse(in, pos);
48         builder.addLongitude(d);
49     }
50 
51     @Override
52     public Optional<String> format(final Location loc) {
53         return loc.longitude()
54             .map(Longitude::toDegrees)
55             .map(d -> isAbsolute() ? abs(d: d)
56             .map(this::format);
57     }
58 
59     @Override
60     public String toPattern() {
61         return isPrefixSign() "+" super.toPattern() super.toPattern();
62     }
63 
64 }