AbstractTextFormatWriter.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.core.utils;

  18. import java.io.Closeable;
  19. import java.io.IOException;
  20. import java.io.Writer;
  21. import java.util.function.DoubleFunction;

  22. import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;

  23. /** Base type for classes that write text-based data formats. This class
  24.  * provides a number of common configuration options and utility methods.
  25.  */
  26. public abstract class AbstractTextFormatWriter implements Closeable {

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

  29.     /** Underlying writer instance. */
  30.     private final Writer writer;

  31.     /** Line separator string. */
  32.     private String lineSeparator = DEFAULT_LINE_SEPARATOR;

  33.     /** Double format function. */
  34.     private DoubleFunction<String> doubleFormat;

  35.     /** Construct a new instance that writes content to the given writer.
  36.      * @param writer writer instance
  37.      */
  38.     protected AbstractTextFormatWriter(final Writer writer) {
  39.         this(writer, Double::toString);
  40.     }

  41.     /** Construct a new instance that writes content to the given writer and uses the
  42.      * decimal format instance for creating floating-point string representations.
  43.      * @param writer writer instance
  44.      * @param doubleFormat double format function
  45.      */
  46.     protected AbstractTextFormatWriter(final Writer writer, final DoubleFunction<String> doubleFormat) {
  47.         this.writer = writer;
  48.         this.doubleFormat = doubleFormat;
  49.     }

  50.     /** Get the current line separator. This value defaults to {@value #DEFAULT_LINE_SEPARATOR}.
  51.      * @return the current line separator
  52.      */
  53.     public String getLineSeparator() {
  54.         return lineSeparator;
  55.     }

  56.     /** Set the line separator.
  57.      * @param lineSeparator the line separator to use
  58.      */
  59.     public void setLineSeparator(final String lineSeparator) {
  60.         this.lineSeparator = lineSeparator;
  61.     }

  62.     /** Get the function used to format floating point output.
  63.      * @return the double format function
  64.      */
  65.     public DoubleFunction<String> getDoubleFormat() {
  66.         return doubleFormat;
  67.     }

  68.     /** Set the function used to format floating point output.
  69.      * @param doubleFormat double format function
  70.      */
  71.     public void setDoubleFormat(final DoubleFunction<String> doubleFormat) {
  72.         this.doubleFormat = doubleFormat;
  73.     }

  74.     /** {@inheritDoc} */
  75.     @Override
  76.     public void close() {
  77.         GeometryIOUtils.closeUnchecked(writer);
  78.     }

  79.     /** Get the underlying writer instance.
  80.      * @return writer instance
  81.      */
  82.     protected Writer getWriter() {
  83.         return writer;
  84.     }

  85.     /** Write a double value formatted using the configured decimal format function.
  86.      * @param d value to write
  87.      * @throws java.io.UncheckedIOException if an I/O error occurs
  88.      */
  89.     protected void write(final double d) {
  90.         write(doubleFormat.apply(d));
  91.     }

  92.     /** Write an integer value.
  93.      * @param n value to write
  94.      * @throws java.io.UncheckedIOException if an I/O error occurs
  95.      */
  96.     protected void write(final int n) {
  97.         write(String.valueOf(n));
  98.     }

  99.     /** Write a char value.
  100.      * @param c character to write
  101.      * @throws java.io.UncheckedIOException if an I/O error occurs
  102.      */
  103.     protected void write(final char c) {
  104.         try {
  105.             writer.write(c);
  106.         } catch (IOException exc) {
  107.             throw GeometryIOUtils.createUnchecked(exc);
  108.         }
  109.     }

  110.     /** Write a string.
  111.      * @param str string to write
  112.      * @throws java.io.UncheckedIOException if an I/O error occurs
  113.      */
  114.     protected void write(final String str) {
  115.         try {
  116.             writer.write(str);
  117.         } catch (IOException exc) {
  118.             throw GeometryIOUtils.createUnchecked(exc);
  119.         }
  120.     }

  121.     /** Write the configured line separator to the output.
  122.      * @throws java.io.UncheckedIOException if an I/O error occurs
  123.      */
  124.     protected void writeNewLine() {
  125.         write(lineSeparator);
  126.     }
  127. }