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.core.utils;
    18
    19  import java.io.Closeable;
    20  import java.io.IOException;
    21  import java.io.Writer;
    22  import java.util.function.DoubleFunction;
    23
    24  import org.apache.commons.geometry.io.core.internal.GeometryIOUtils;
    25
    26  /** Base type for classes that write text-based data formats. This class
    27   * provides a number of common configuration options and utility methods.
    28   */
    29  public abstract class AbstractTextFormatWriter implements Closeable {
    30
    31      /** The default line separator value. */
    32      private static final String DEFAULT_LINE_SEPARATOR = "\n";
    33
    34      /** Underlying writer instance. */
    35      private final Writer writer;
    36
    37      /** Line separator string. */
    38      private String lineSeparator = DEFAULT_LINE_SEPARATOR;
    39
    40      /** Double format function. */
    41      private DoubleFunction<String> doubleFormat;
    42
    43      /** Construct a new instance that writes content to the given writer.
    44       * @param writer writer instance
    45       */
    46      protected AbstractTextFormatWriter(final Writer writer) {
    47          this(writer, Double::toString);
    48      }
    49
    50      /** Construct a new instance that writes content to the given writer and uses the
    51       * decimal format instance for creating floating-point string representations.
    52       * @param writer writer instance
    53       * @param doubleFormat double format function
    54       */
    55      protected AbstractTextFormatWriter(final Writer writer, final DoubleFunction<String> doubleFormat) {
    56          this.writer = writer;
    57          this.doubleFormat = doubleFormat;
    58      }
    59
    60      /** Get the current line separator. This value defaults to {@value #DEFAULT_LINE_SEPARATOR}.
    61       * @return the current line separator
    62       */
    63      public String getLineSeparator() {
    64          return lineSeparator;
    65      }
    66
    67      /** Set the line separator.
    68       * @param lineSeparator the line separator to use
    69       */
    70      public void setLineSeparator(final String lineSeparator) {
    71          this.lineSeparator = lineSeparator;
    72      }
    73
    74      /** Get the function used to format floating point output.
    75       * @return the double format function
    76       */
    77      public DoubleFunction<String> getDoubleFormat() {
    78          return doubleFormat;
    79      }
    80
    81      /** Set the function used to format floating point output.
    82       * @param doubleFormat double format function
    83       */
    84      public void setDoubleFormat(final DoubleFunction<String> doubleFormat) {
    85          this.doubleFormat = doubleFormat;
    86      }
    87
    88      /** {@inheritDoc} */
    89      @Override
    90      public void close() {
    91          GeometryIOUtils.closeUnchecked(writer);
    92      }
    93
    94      /** Get the underlying writer instance.
    95       * @return writer instance
    96       */
    97      protected Writer getWriter() {
    98          return writer;
    99      }
    100
    101     /** Write a double value formatted using the configured decimal format function.
    102      * @param d value to write
    103      * @throws java.io.UncheckedIOException if an I/O error occurs
    104      */
    105     protected void write(final double d) {
    106         write(doubleFormat.apply(d));
    107     }
    108
    109     /** Write an integer value.
    110      * @param n value to write
    111      * @throws java.io.UncheckedIOException if an I/O error occurs
    112      */
    113     protected void write(final int n) {
    114         write(String.valueOf(n));
    115     }
    116
    117     /** Write a char value.
    118      * @param c character to write
    119      * @throws java.io.UncheckedIOException if an I/O error occurs
    120      */
    121     protected void write(final char c) {
    122         try {
    123             writer.write(c);
    124         } catch (IOException exc) {
    125             throw GeometryIOUtils.createUnchecked(exc);
    126         }
    127     }
    128
    129     /** Write a string.
    130      * @param str string to write
    131      * @throws java.io.UncheckedIOException if an I/O error occurs
    132      */
    133     protected void write(final String str) {
    134         try {
    135             writer.write(str);
    136         } catch (IOException exc) {
    137             throw GeometryIOUtils.createUnchecked(exc);
    138         }
    139     }
    140
    141     /** Write the configured line separator to the output.
    142      * @throws java.io.UncheckedIOException if an I/O error occurs
    143      */
    144     protected void writeNewLine() {
    145         write(lineSeparator);
    146     }
    147 }