View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.geometry.io.euclidean.threed.txt;
18  
19  import java.nio.charset.Charset;
20  import java.nio.charset.StandardCharsets;
21  import java.util.Iterator;
22  import java.util.function.DoubleFunction;
23  import java.util.stream.Stream;
24  
25  import org.apache.commons.geometry.euclidean.threed.PlaneConvexSubset;
26  import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
27  import org.apache.commons.geometry.io.core.output.GeometryOutput;
28  import org.apache.commons.geometry.io.euclidean.threed.AbstractBoundaryWriteHandler3D;
29  import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;
30  
31  /** Abstract based class for write handlers that output text formats produced
32   * by {@link TextFacetDefinitionWriter}.
33   * @see TextFacetDefinitionWriter
34   */
35  public abstract class AbstractTextBoundaryWriteHandler3D extends AbstractBoundaryWriteHandler3D {
36  
37      /** The default line separator value. */
38      private static final String DEFAULT_LINE_SEPARATOR = "\n";
39  
40      /** Default charset used for text output. */
41      private Charset defaultCharset = StandardCharsets.UTF_8;
42  
43      /** Line separator string. */
44      private String lineSeparator = DEFAULT_LINE_SEPARATOR;
45  
46      /** Double format function. */
47      private DoubleFunction<String> doubleFormat = Double::toString;
48  
49      /** Get the text output default charset, used if the output does not
50       * specify a charset.
51       * @return text output default charset
52       */
53      public Charset getDefaultCharset() {
54          return defaultCharset;
55      }
56  
57      /** Set the text output default charset, used if the output does not
58       * specify a charset.
59       * @param defaultCharset text output default charset
60       */
61      public void setDefaultCharset(final Charset defaultCharset) {
62          this.defaultCharset = defaultCharset;
63      }
64  
65      /** Get the line separator. This value defaults to {@value #DEFAULT_LINE_SEPARATOR}.
66       * @return the current line separator
67       */
68      public String getLineSeparator() {
69          return lineSeparator;
70      }
71  
72      /** Set the line separator.
73       * @param lineSeparator the line separator to use
74       */
75      public void setLineSeparator(final String lineSeparator) {
76          this.lineSeparator = lineSeparator;
77      }
78  
79      /** Get the double format function used to convert double values
80       * to strings.
81       * @return double format function
82       */
83      public DoubleFunction<String> getDoubleFormat() {
84          return doubleFormat;
85      }
86  
87      /** Set the double format function used to convert double values
88       * to strings. The given function must be thread-safe if this handler
89       * is to be used in a multi-threaded context.
90       * @param doubleFormat double format function
91       */
92      public void setDoubleFormat(final DoubleFunction<String> doubleFormat) {
93          this.doubleFormat = doubleFormat;
94      }
95  
96      /** {@inheritDoc} */
97      @Override
98      public void write(final Stream<? extends PlaneConvexSubset> boundaries, final GeometryOutput out) {
99          try (TextFacetDefinitionWriter writer = getFacetDefinitionWriter(out)) {
100             final Iterator<? extends PlaneConvexSubset> it = boundaries.iterator();
101             while (it.hasNext()) {
102                 writer.write(it.next());
103             }
104         }
105     }
106 
107     /** {@inheritDoc} */
108     @Override
109     public void writeFacets(final Stream<? extends FacetDefinition> facets, final GeometryOutput out) {
110         try (TextFacetDefinitionWriter writer = getFacetDefinitionWriter(out)) {
111             final Iterator<? extends FacetDefinition> it = facets.iterator();
112             while (it.hasNext()) {
113                 writer.write(it.next());
114             }
115         }
116     }
117 
118     /** Get a configured {@link TextFacetDefinitionWriter} for writing output.
119      * @param out output stream to write to
120      * @return a new, configured text format writer
121      * @throws java.io.UncheckedIOException if an I/O error occurs
122      */
123     protected TextFacetDefinitionWriter getFacetDefinitionWriter(final GeometryOutput out) {
124         final TextFacetDefinitionWriter facetWriter =
125                 new TextFacetDefinitionWriter(GeometryIOUtils.createBufferedWriter(out, defaultCharset));
126 
127         facetWriter.setLineSeparator(lineSeparator);
128         facetWriter.setDoubleFormat(doubleFormat);
129 
130         return facetWriter;
131     }
132 }