AbstractTextBoundaryWriteHandler3D.java

  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. import java.nio.charset.Charset;
  19. import java.nio.charset.StandardCharsets;
  20. import java.util.Iterator;
  21. import java.util.function.DoubleFunction;
  22. import java.util.stream.Stream;

  23. import org.apache.commons.geometry.euclidean.threed.PlaneConvexSubset;
  24. import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
  25. import org.apache.commons.geometry.io.core.output.GeometryOutput;
  26. import org.apache.commons.geometry.io.euclidean.threed.AbstractBoundaryWriteHandler3D;
  27. import org.apache.commons.geometry.io.euclidean.threed.FacetDefinition;

  28. /** Abstract based class for write handlers that output text formats produced
  29.  * by {@link TextFacetDefinitionWriter}.
  30.  * @see TextFacetDefinitionWriter
  31.  */
  32. public abstract class AbstractTextBoundaryWriteHandler3D extends AbstractBoundaryWriteHandler3D {

  33.     /** The default line separator value. */
  34.     private static final String DEFAULT_LINE_SEPARATOR = "\n";

  35.     /** Default charset used for text output. */
  36.     private Charset defaultCharset = StandardCharsets.UTF_8;

  37.     /** Line separator string. */
  38.     private String lineSeparator = DEFAULT_LINE_SEPARATOR;

  39.     /** Double format function. */
  40.     private DoubleFunction<String> doubleFormat = Double::toString;

  41.     /** Get the text output default charset, used if the output does not
  42.      * specify a charset.
  43.      * @return text output default charset
  44.      */
  45.     public Charset getDefaultCharset() {
  46.         return defaultCharset;
  47.     }

  48.     /** Set the text output default charset, used if the output does not
  49.      * specify a charset.
  50.      * @param defaultCharset text output default charset
  51.      */
  52.     public void setDefaultCharset(final Charset defaultCharset) {
  53.         this.defaultCharset = defaultCharset;
  54.     }

  55.     /** Get the line separator. This value defaults to {@value #DEFAULT_LINE_SEPARATOR}.
  56.      * @return the current line separator
  57.      */
  58.     public String getLineSeparator() {
  59.         return lineSeparator;
  60.     }

  61.     /** Set the line separator.
  62.      * @param lineSeparator the line separator to use
  63.      */
  64.     public void setLineSeparator(final String lineSeparator) {
  65.         this.lineSeparator = lineSeparator;
  66.     }

  67.     /** Get the double format function used to convert double values
  68.      * to strings.
  69.      * @return double format function
  70.      */
  71.     public DoubleFunction<String> getDoubleFormat() {
  72.         return doubleFormat;
  73.     }

  74.     /** Set the double format function used to convert double values
  75.      * to strings. The given function must be thread-safe if this handler
  76.      * is to be used in a multi-threaded context.
  77.      * @param doubleFormat double format function
  78.      */
  79.     public void setDoubleFormat(final DoubleFunction<String> doubleFormat) {
  80.         this.doubleFormat = doubleFormat;
  81.     }

  82.     /** {@inheritDoc} */
  83.     @Override
  84.     public void write(final Stream<? extends PlaneConvexSubset> boundaries, final GeometryOutput out) {
  85.         try (TextFacetDefinitionWriter writer = getFacetDefinitionWriter(out)) {
  86.             final Iterator<? extends PlaneConvexSubset> it = boundaries.iterator();
  87.             while (it.hasNext()) {
  88.                 writer.write(it.next());
  89.             }
  90.         }
  91.     }

  92.     /** {@inheritDoc} */
  93.     @Override
  94.     public void writeFacets(final Stream<? extends FacetDefinition> facets, final GeometryOutput out) {
  95.         try (TextFacetDefinitionWriter writer = getFacetDefinitionWriter(out)) {
  96.             final Iterator<? extends FacetDefinition> it = facets.iterator();
  97.             while (it.hasNext()) {
  98.                 writer.write(it.next());
  99.             }
  100.         }
  101.     }

  102.     /** Get a configured {@link TextFacetDefinitionWriter} for writing output.
  103.      * @param out output stream to write to
  104.      * @return a new, configured text format writer
  105.      * @throws java.io.UncheckedIOException if an I/O error occurs
  106.      */
  107.     protected TextFacetDefinitionWriter getFacetDefinitionWriter(final GeometryOutput out) {
  108.         final TextFacetDefinitionWriter facetWriter =
  109.                 new TextFacetDefinitionWriter(GeometryIOUtils.createBufferedWriter(out, defaultCharset));

  110.         facetWriter.setLineSeparator(lineSeparator);
  111.         facetWriter.setDoubleFormat(doubleFormat);

  112.         return facetWriter;
  113.     }
  114. }