View Javadoc
1   /*
2    * Copyright 2018-2022 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
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  
17  package org.bremersee.geojson.model;
18  
19  import java.math.BigDecimal;
20  
21  /**
22   * The interface lat lon aware.
23   *
24   * @author Christian Bremer
25   */
26  public interface LatLonAware {
27  
28    /**
29     * Gets latitude.
30     *
31     * @return the latitude
32     */
33    BigDecimal getLatitude();
34  
35    /**
36     * Gets longitude.
37     *
38     * @return the longitude
39     */
40    BigDecimal getLongitude();
41  
42    /**
43     * Determines whether latitude and longitude have values.
44     *
45     * @return {@code true} if latitude and longitude is not {@code null}, otherwise {@code false}
46     */
47    default boolean hasValues() {
48      return getLatitude() != null && getLongitude() != null;
49    }
50  
51    /**
52     * To comma separated latitude longitude string.
53     *
54     * @return the string
55     */
56    default String toLatLonString() {
57      return hasValues()
58          ? getLatitude().toPlainString() + "," + getLongitude().toPlainString()
59          : "";
60    }
61  
62    /**
63     * To comma separated longitude latitude string.
64     *
65     * @return the string
66     */
67    default String toLonLatString() {
68      return hasValues()
69          ? getLongitude().toPlainString() + "," + getLatitude().toPlainString()
70          : "";
71    }
72  
73    /**
74     * Returns a new builder.
75     *
76     * @return the builder
77     */
78    static Builder builder() {
79      return new BuilderImpl();
80    }
81  
82    /**
83     * The builder interface.
84     */
85    interface Builder {
86  
87      /**
88       * Sets latitude and longitude from the given object.
89       *
90       * @param latLonAware the latitude and longitude aware object
91       * @return the builder
92       */
93      Builder from(LatLonAware latLonAware);
94  
95      /**
96       * Sets latitude.
97       *
98       * @param latitude the latitude
99       * @return the builder
100      */
101     Builder latitude(BigDecimal latitude);
102 
103     /**
104      * Sets latitude.
105      *
106      * @param latitude the latitude
107      * @return the builder
108      */
109     Builder latitude(double latitude);
110 
111     /**
112      * Sets longitude.
113      *
114      * @param longitude the longitude
115      * @return the builder
116      */
117     Builder longitude(BigDecimal longitude);
118 
119     /**
120      * Sets longitude.
121      *
122      * @param longitude the longitude
123      * @return the builder
124      */
125     Builder longitude(double longitude);
126 
127     /**
128      * Build latitude and longitude aware.
129      *
130      * @return the lat lon aware
131      */
132     LatLonAware build();
133   }
134 
135   /**
136    * The builder implementation.
137    */
138   class BuilderImpl implements Builder {
139 
140     private BigDecimal latitude;
141 
142     private BigDecimal longitude;
143 
144     @Override
145     public Builder from(LatLonAware latLonAware) {
146       if (latLonAware != null) {
147         latitude = latLonAware.getLatitude();
148         longitude = latLonAware.getLongitude();
149       }
150       return this;
151     }
152 
153     @Override
154     public Builder latitude(BigDecimal latitude) {
155       this.latitude = latitude;
156       return this;
157     }
158 
159     @Override
160     public Builder latitude(double latitude) {
161       this.latitude = BigDecimal.valueOf(latitude);
162       return this;
163     }
164 
165     @Override
166     public Builder longitude(BigDecimal longitude) {
167       this.longitude = longitude;
168       return this;
169     }
170 
171     @Override
172     public Builder longitude(double longitude) {
173       this.longitude = BigDecimal.valueOf(longitude);
174       return this;
175     }
176 
177     @Override
178     public LatLonAware build() {
179       return new LatLon(latitude, longitude);
180     }
181   }
182 
183 }