View Javadoc
1   /*
2    * Copyright 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.converter.serialization;
18  
19  import static java.util.Collections.unmodifiableList;
20  import static java.util.Objects.isNull;
21  
22  import java.io.Serial;
23  import java.io.Serializable;
24  import java.util.ArrayList;
25  import java.util.List;
26  import lombok.AccessLevel;
27  import lombok.RequiredArgsConstructor;
28  import org.locationtech.jts.geom.CoordinateSequence;
29  
30  /**
31   * The type Coordinate sequence to list converter.
32   *
33   * @author Christian Bremer
34   */
35  @RequiredArgsConstructor(access = AccessLevel.PROTECTED)
36  class CoordinateSequenceToListConverter implements Serializable {
37  
38    @Serial
39    private static final long serialVersionUID = 1L;
40  
41    private final CoordinateToListConverter coordinateConverter;
42  
43    /**
44     * Convert list.
45     *
46     * @param source the source
47     * @return the list
48     */
49    List<List<Number>> convert(CoordinateSequence source) {
50      int size = isNull(source) ? 0 : source.size();
51      List<List<Number>> list = new ArrayList<>(size);
52      if (size > 0) {
53        for (int n = 0; n < source.size(); n++) {
54          list.add(coordinateConverter.convert(source.getCoordinate(n)));
55        }
56      }
57      return unmodifiableList(list);
58    }
59  }