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 static java.util.Objects.isNull;
20  
21  import io.swagger.v3.oas.annotations.media.Schema;
22  import java.io.Serial;
23  import java.io.Serializable;
24  import java.math.BigDecimal;
25  import java.util.ArrayList;
26  import lombok.EqualsAndHashCode;
27  import lombok.ToString;
28  
29  /**
30   * The first two elements are longitude and latitude.
31   *
32   * @author Christian Bremer
33   */
34  @Schema(description = "The first two elements are longitude and latitude.")
35  @EqualsAndHashCode(callSuper = true)
36  @ToString(callSuper = true)
37  public class Position extends ArrayList<BigDecimal> implements Serializable {
38  
39    @Serial
40    private static final long serialVersionUID = 1L;
41  
42    /**
43     * Instantiates a new empty (illegal) position.
44     */
45    public Position() {
46      super(3);
47    }
48  
49    /**
50     * Instantiates a new position.
51     *
52     * @param x the x (aka longitude)
53     * @param y the y (aka latitude)
54     */
55    public Position(BigDecimal x, BigDecimal y) {
56      this(x, y, null);
57    }
58  
59    /**
60     * Instantiates a new position.
61     *
62     * @param x the x (aka longitude)
63     * @param y the y (aka latitude)
64     * @param z the z (the z coordinate)
65     */
66    public Position(BigDecimal x, BigDecimal y, BigDecimal z) {
67      super(isNull(z) ? 2 : 3);
68      if (isNull(x)) {
69        throw new IllegalArgumentException("X (longitude) must not be null.");
70      }
71      if (isNull(y)) {
72        throw new IllegalArgumentException("Y (latitude) must not be null.");
73      }
74      add(x);
75      add(y);
76      if (!isNull(z)) {
77        add(z);
78      }
79    }
80  
81  }
82