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  import static org.bremersee.geojson.GeoJsonConstants.MULTI_LINESTRING;
22  
23  import java.io.Serial;
24  import java.util.ArrayList;
25  import java.util.List;
26  import org.locationtech.jts.geom.LineString;
27  import org.locationtech.jts.geom.MultiLineString;
28  
29  /**
30   * The type Multi line string to json converter.
31   *
32   * @author Christian Bremer
33   */
34  class MultiLineStringToJsonConverter extends AbstractGeometryToJsonConverter<MultiLineString> {
35  
36    @Serial
37    private static final long serialVersionUID = 1L;
38  
39    private final LineStringToJsonConverter lineStringConverter;
40  
41    /**
42     * Instantiates a new Multi line string to json converter.
43     *
44     * @param lineStringConverter the line string converter
45     * @param withBoundingBox the with bounding box
46     */
47    MultiLineStringToJsonConverter(
48        LineStringToJsonConverter lineStringConverter,
49        boolean withBoundingBox) {
50  
51      super(withBoundingBox);
52      if (isNull(lineStringConverter)) {
53        throw new IllegalArgumentException("Line string converter must be present.");
54      }
55      this.lineStringConverter = lineStringConverter;
56    }
57  
58    @Override
59    String getGeometryType() {
60      return MULTI_LINESTRING;
61    }
62  
63    @Override
64    Object getGeometryJsonValue(MultiLineString source) {
65      List<Object> list = new ArrayList<>(source.getNumGeometries());
66      for (int i = 0; i < source.getNumGeometries(); i++) {
67        LineString lineString = (LineString) source.getGeometryN(i);
68        list.add(lineStringConverter.getGeometryJsonValue(lineString));
69      }
70      return unmodifiableList(list);
71    }
72  }