LocationBuilder.java
001 /*
002  * Java GPX Library (jpx-3.1.0).
003  *
004  * Licensed under the Apache License, Version 2.0 (the "License");
005  * you may not use this file except in compliance with the License.
006  * You may obtain a copy of the License at
007  *
008  *      http://www.apache.org/licenses/LICENSE-2.0
009  *
010  * Unless required by applicable law or agreed to in writing, software
011  * distributed under the License is distributed on an "AS IS" BASIS,
012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013  * See the License for the specific language governing permissions and
014  * limitations under the License.
015  */
016 package io.jenetics.jpx.format;
017 
018 import io.jenetics.jpx.Latitude;
019 import io.jenetics.jpx.Length;
020 import io.jenetics.jpx.Longitude;
021 
022 /**
023  @version 2.2
024  @since 2.2
025  */
026 final class LocationBuilder {
027 
028     private Double _latitude = null;
029     private int _latitudeSign = +1;
030 
031     private Double _longitude = null;
032     private int _longitudeSign = +1;
033 
034     private Double elevation = null;
035 
036     LocationBuilder copy() {
037         LocationBuilder c = new LocationBuilder();
038         c._latitudeSign = _latitudeSign;
039         c._latitude = _latitude;
040         c._longitudeSign = _longitudeSign;
041         c._longitude = _longitude;
042         c.elevation = elevation;
043         return c;
044     }
045 
046     void copy(final LocationBuilder from) {
047         _latitudeSign = from._latitudeSign;
048         _latitude = from._latitude;
049         _longitudeSign = from._longitudeSign;
050         _longitude = from._longitude;
051         elevation = from.elevation;
052     }
053 
054     void setLatitudeSign(final int sign) {
055         _latitudeSign = sign;
056     }
057 
058     void addLatitude(final double degrees) {
059         if (degrees < 0.0) {
060             _latitudeSign = -1;
061         }
062         if (_latitude == null) {
063             _latitude = 0.0;
064         }
065         _latitude += Math.abs(degrees);
066     }
067 
068     void addLatitudeMinute(final double minutes) {
069         addLatitude(minutes/60.0);
070     }
071 
072     void addLatitudeSecond(final double seconds) {
073         addLatitudeseconds/3600.0);
074     }
075 
076     void setLongitudeSign(final int sign) {
077         _longitudeSign = sign;
078     }
079 
080     void addLongitude(final double degrees) {
081         if (degrees < 0.0) {
082             _longitudeSign = -1;
083         }
084         if (_longitude == null) {
085             _longitude = 0.0;
086         }
087         _longitude += Math.abs(degrees);
088     }
089 
090     void addLongitudeMinute(final double minutes) {
091         addLongitude(minutes/60.0);
092     }
093 
094     void addLongitudeSecond(final double seconds) {
095         addLongitude(seconds/3600.0);
096     }
097 
098     void setElevation(final double meters) {
099         elevation = meters;
100     }
101 
102     Location build() {
103         final var lat = _latitude == null
104             null
105             : Latitude.ofDegrees(_latitudeSign*_latitude);
106         final var lon = _longitude == null
107             null
108             : Longitude.ofDegrees(_longitudeSign*_longitude);
109         final var ele = elevation == null
110             null
111             : Length.of(elevation, Length.Unit.METER);
112 
113         return Location.of(lat, lon, ele);
114     }
115 
116 }