# PutField.java

    /*
    ** Module   : PutField.java
    ** Abstract : handle data fields for I/O helpers
    **
    ** Copyright (c) 2006-2020, Golden Code Development Corporation.
    **
    ** -#- -I- --Date-- --JPRM-- ---------------------------------Description-----------------------------------
    ** 001 GES 20060925   @29900 Created initial version to handle data fields for I/O helpers.
    ** 002 SIY 20071001   @35320 Made class suitable for network transfers. Ref: #35314
    ** 003 SIY 20071003   @35351 Added default constructor. Header cleanup. Fixed NPE during
    **                           serialization. Ref: #35347.
    ** 004 EVL 20160224          Javadoc fixes to make compatible with Oracle Java 8 for Solaris 10.
    ** 005 GES 20160530          Use lambdas instead of Resolvable instances.
    ** 006 GES 20200605          Added direct support for enums (implicitly treats them as being wrapped in the
    **                           STRING() builtin function since the usage works with character format strings.
    ** 007 IAS 20200908          Rework (de)serialization.
    */

    /*
    ** This program is free software: you can redistribute it and/or modify
    ** it under the terms of the GNU Affero General Public License as
    ** published by the Free Software Foundation, either version 3 of the
    ** License, or (at your option) any later version.
    **
    ** This program is distributed in the hope that it will be useful,
    ** but WITHOUT ANY WARRANTY; without even the implied warranty of
    ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    ** GNU Affero General Public License for more details.
    **
    ** You may find a copy of the GNU Affero GPL version 3 at the following
    ** location: https://www.gnu.org/licenses/agpl-3.0.en.html
    **
    ** Additional terms under GNU Affero GPL version 3 section 7:
    **
    **   Under Section 7 of the GNU Affero GPL version 3, the following additional
    **   terms apply to the works covered under the License.  These additional terms
    **   are non-permissive additional terms allowed under Section 7 of the GNU
    **   Affero GPL version 3 and may not be removed by you.
    **
    **   0. Attribution Requirement.
    **
    **     You must preserve all legal notices or author attributions in the covered
    **     work or Appropriate Legal Notices displayed by works containing the covered
    **     work.  You may not remove from the covered work any author or developer
    **     credit already included within the covered work.
    **
    **   1. No License To Use Trademarks.
    **
    **     This license does not grant any license or rights to use the trademarks
    **     Golden Code, FWD, any Golden Code or FWD logo, or any other trademarks
    **     of Golden Code Development Corporation. You are not authorized to use the
    **     name Golden Code, FWD, or the names of any author or contributor, for
    **     publicity purposes without written authorization.
    **
    **   2. No Misrepresentation of Affiliation.
    **
    **     You may not represent yourself as Golden Code Development Corporation or FWD.
    **
    **     You may not represent yourself for publicity purposes as associated with
    **     Golden Code Development Corporation, FWD, or any author or contributor to
    **     the covered work, without written authorization.
    **
    **   3. No Misrepresentation of Source or Origin.
    **
    **     You may not represent the covered work as solely your work.  All modified
    **     versions of the covered work must be marked in a reasonable way to make it
    **     clear that the modified work is not originating from Golden Code Development
    **     Corporation or FWD.  All modified versions must contain the notices of
    **     attribution required in this license.
    */

    package com.goldencode.p2j.util;

    import static com.goldencode.util.NativeTypeSerializer.*;
    import java.io.*;
    import java.util.function.*;
    import com.goldencode.p2j.oo.lang.*;

    /**
     * Handles output of data fields for I/O helpers.
     */
    public class PutField
    extends FieldEntry
    {
       /** The data to output to the stream. */
       private Supplier<BaseDataType> data = null;

       /**
        * The format string to use on output. TODO: the format string needs to be validated, before
        * starting the output. Note that a "?" string represents the unknown value.
        */
       private String fmt = null;

       /** <code>true</code> to force either AT or TO positional modes. */
       private boolean positional = false;

       /** <code>true</code> if using AT mode, <code>false</code> for TO mode. */
       private boolean atMode = false;

       /** The column to honor in AT or TO mode. */
       private int column = -1;

       /** Resolved variable value. */
       private BaseDataType savedVar = null;

       /**
        * Default constructor which is necessary to restores instance when it is
        * transferred via network.
        */
       public PutField()
       {
       }

       /**
        * Append the <code>int</code> value to the output buffer with the default
        * format string of '-&gt;,&gt;&gt;&gt;,&gt;&gt;9' at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        */
       public PutField(int val)
       {
          this(val, null);
       }

       /**
        * Append the <code>int</code> value to the output buffer with the given
        * format string at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           '-&gt;,&gt;&gt;&gt;,&gt;&gt;9' will be used.
        */
       public PutField(int val, String fmt)
       {
          this(new integer(val), fmt);
       }

       /**
        * Append the <code>int</code> value to the output buffer with the default
        * format string of '-&gt;,&gt;&gt;&gt;,&gt;&gt;9' at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(int val, boolean starts, int col)
       {
          this(val, null, starts, col);
       }

       /**
        * Append the <code>int</code> value to the output buffer with the given
        * format string at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           '-&gt;,&gt;&gt;&gt;,&gt;&gt;9' will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(int val, String fmt, boolean starts, int col)
       {
          this(new integer(val), fmt, starts, col);
       }

       /**
        * Append the <code>int</code> value to the output buffer with the default
        * format string of '-&gt;,&gt;&gt;&gt;,&gt;&gt;9' at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(int val, boolean starts, NumberType col)
       {
          this(val, null, starts, col.intValue());
       }

       /**
        * Append the <code>int</code> value to the output buffer with the given
        * format string at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           '-&gt;,&gt;&gt;&gt;,&gt;&gt;9' will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(int val, String fmt, boolean starts, NumberType col)
       {
          this(new integer(val), fmt, starts, col.intValue());
       }

       /**
        * Append the <code>double</code> value to the output buffer with the
        * default format string of '-&gt;&gt;,&gt;&gt;9.99' at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        */
       public PutField(double val)
       {
          this(val, null);
       }

       /**
        * Append the <code>double</code> value to the output buffer with the
        * given format string at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           '-&gt;&gt;,&gt;&gt;9.99' will be used.
        */
       public PutField(double val, String fmt)
       {
          this(new decimal(val), fmt);
       }

       /**
        * Append the <code>double</code> value to the output buffer with the
        * default format string of '-&gt;&gt;,&gt;&gt;9.99' at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(double val, boolean starts, int col)
       {
          this(val, null, starts, col);
       }

       /**
        * Append the <code>double</code> value to the output buffer with the
        * given format string at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           '-&gt;&gt;,&gt;&gt;9.99' will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(double val, String fmt, boolean starts, int col)
       {
          this(new decimal(val), fmt, starts, col);
       }

       /**
        * Append the <code>double</code> value to the output buffer with the
        * default format string of '-&gt;&gt;,&gt;&gt;9.99' at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(double val, boolean starts, NumberType col)
       {
          this(val, null, starts, col.intValue());
       }

       /**
        * Append the <code>double</code> value to the output buffer with the
        * given format string at the current output column.
        * <p>
        * Note that this format string works as the display form, which pads on
        * the left to the full size of the format string.  This is different from
        * the normal operation of <code>toString</code>.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           '-&gt;&gt;,&gt;&gt;9.99' will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(double     val,
                    String     fmt,
                    boolean    starts,
                    NumberType col)
       {
          this(new decimal(val), fmt, starts, col.intValue());
       }

       /**
        * Append the <code>boolean</code> value to the output buffer with the
        * default format string of 'yes/no' at the current output column.
        *
        * @param    val
        *           The data to be output.
        */
       public PutField(boolean val)
       {
          this(val, null);
       }

       /**
        * Append the <code>boolean</code> value to the output buffer with the
        * given format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           'yes/no' will be used.
        */
       public PutField(boolean val, String fmt)
       {
          this(new logical(val), fmt);
       }

       /**
        * Append the <code>boolean</code> value to the output buffer with the
        * default format string of 'yes/no' at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(boolean val, boolean starts, int col)
       {
          this(val, null, starts, col);
       }

       /**
        * Append the <code>boolean</code> value to the output buffer with the
        * given format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           'yes/no' will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(boolean val, String fmt, boolean starts, int col)
       {
          this(new logical(val), fmt, starts, col);
       }

       /**
        * Append the <code>boolean</code> value to the output buffer with the
        * default format string of 'yes/no' at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(boolean val, boolean starts, NumberType col)
       {
          this(val, null, starts, col.intValue());
       }

       /**
        * Append the <code>boolean</code> value to the output buffer with the
        * given format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           'yes/no' will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(boolean    val,
                       String     fmt,
                       boolean    starts,
                       NumberType col)
       {
          this(new logical(val), fmt, starts, col.intValue());
       }

       /**
        * Append the <code>String</code> value to the output buffer with a
        * format string of 'x(N)' (where N is the length of the value) at the
        * current output column.
        *
        * @param    val
        *           The data to be output.
        */
       public PutField(String val)
       {
          this(val, null);
       }

       /**
        * Append the <code>String</code> value to the output buffer with the
        * given format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           'x(N)' will be used (where N is the length of the value).
        */
       public PutField(String val, String fmt)
       {
          this(new character(val),
               Stream.generateStringFormat(fmt, val.length()));
       }

       /**
        * Append the <code>String</code> value to the output buffer with the
        * default format string of 'x(N)' will be used (where N is the length of
        * the value) at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(String val, boolean starts, int col)
       {
          this(val, null, starts, col);
       }

       /**
        * Append the <code>String</code> value to the output buffer with the
        * given format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           'x(N)' will be used (where N is the length of the value) will
        *           be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(String val, String fmt, boolean starts, int col)
       {
          this(new character(val),
               Stream.generateStringFormat(fmt, val.length()),
               starts,
               col);
       }

       /**
        * Append the <code>String</code> value to the output buffer with the
        * default format string of 'x(N)' will be used (where N is the length of
        * the value) at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(String val, boolean starts, NumberType col)
       {
          this(val, null, starts, col.intValue());
       }

       /**
        * Append the <code>String</code> value to the output buffer with the
        * given format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           'x(N)' will be used (where N is the length of the value) will
        *           be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(String     val,
                    String     fmt,
                    boolean    starts,
                    NumberType col)
       {
          this(new character(val),
               Stream.generateStringFormat(fmt, val.length()),
               starts,
               col.intValue());
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        */
       public PutField(BaseDataType val)
       {
          this(val, null);
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        */
       public PutField(object<? extends LegacyEnum> val)
       {
          this(() -> character.valueOf(val), null);
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        */
       public PutField(Resolvable val)
       {
          this(val, null);
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The function which supplies the value to be output.
        */
       public PutField(Supplier<BaseDataType> val)
       {
          this(val, null);
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        */
       public PutField(final BaseDataType val, String fmt)
       {
          this.data = () -> val;
          this.fmt  = fmt;
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        */
       public PutField(final object<? extends LegacyEnum> val, String fmt)
       {
          this(() -> character.valueOf(val), fmt);
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        */
       public PutField(final Resolvable val, String fmt)
       {
          this.data = () -> val.resolve();
          this.fmt  = fmt;
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The function which supplies the value to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        */
       public PutField(Supplier<BaseDataType> val, String fmt)
       {
          this.data = val;
          this.fmt  = fmt;
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(BaseDataType val, boolean starts, int col)
       {
          this(val, null, starts, col);
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(Resolvable val, boolean starts, int col)
       {
          this(val, null, starts, col);
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The function which supplies the value to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(Supplier<BaseDataType> val, boolean starts, int col)
       {
          this(val, null, starts, col);
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(BaseDataType val, boolean starts, NumberType col)
       {
          this(val, null, starts, col.intValue());
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(Resolvable val, boolean starts, NumberType col)
       {
          this(val, null, starts, col.intValue());
       }

       /**
        * Append the value to the output buffer with the default type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The function which supplies the value to be output.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(Supplier<BaseDataType> val, boolean starts, NumberType col)
       {
          this(val, null, starts, col.intValue());
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(BaseDataType val, String fmt, boolean starts, NumberType col)
       {
          this(val, fmt, starts, col.intValue());
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(Resolvable val, String fmt, boolean starts, NumberType col)
       {
          this(val, fmt, starts, col.intValue());
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The function which supplies the value to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(Supplier<BaseDataType> val, String fmt, boolean starts, NumberType col)
       {
          this(val, fmt, starts, col.intValue());
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(final BaseDataType val, String fmt, boolean starts, int col)
       {
          this.data       = () -> val;
          this.fmt        = fmt;
          this.positional = true;
          this.atMode     = starts;
          this.column     = col;
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(final object<? extends LegacyEnum> val, String fmt, boolean starts, int col)
       {
          this(() -> character.valueOf(val), fmt, starts, col);
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The data to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(final Resolvable val, String fmt, boolean starts, int col)
       {
          this.data       = () -> val.resolve();
          this.fmt        = fmt;
          this.positional = true;
          this.atMode     = starts;
          this.column     = col;
       }

       /**
        * Append the value to the output buffer with the given type-specific
        * format string at the current output column.
        *
        * @param    val
        *           The function which supplies the value to be output.
        * @param    fmt
        *           The format string to use.  If <code>null</code>, the default
        *           type-specific format will be used.
        * @param    starts
        *           If <code>true</code>, the output will be placed starting at
        *           the given column (or on the next line at that column, if the
        *           given column is already in use on the current line). If
        *           <code>false</code>, the output will be placed so that it
        *           ends at the given column (or on the next line at that column,
        *           if the calculated start column is already in use on the
        *           current line).
        * @param    col
        *           The start or end column (which it is depends on the previous
        *           parameter's value). This is used as a 1-based index into the
        *           current output line.
        */
       public PutField(Supplier<BaseDataType> val, String fmt, boolean starts, int col)
       {
          this.data       = val;
          this.fmt        = fmt;
          this.positional = true;
          this.atMode     = starts;
          this.column     = col;
       }

       /**
        * Renders the contained field definition to the given stream.
        *
        * @param    out
        *           The stream to which the field should be rendered.
        * @throws ErrorConditionException
        */
       public void output(Stream out)
       throws ErrorConditionException
       {
          if (positional)
          {
             out.putField(data.get(), fmt, atMode, column);
          }
          else
          {
             out.putField(data.get(), fmt);
          }
       }

       /**
        * Notify instance that it is about to be transferred so all resolvable
        * data should be resolved and prepared for sending via network.
        */
       public void resolveData()
       {
          savedVar = data.get();
       }

       /**
        * Replacement for the default object reading method. The latest
        * state is read from the input source.
        *
        * @param    in
        *           The input source from which fields will be restored.
        *
        * @throws   IOException
        *           In case of I/O errors.
        * @throws   ClassNotFoundException
        *           If payload can't be instantiated.
        */
       public void readExternal(ObjectInput in)
       throws IOException,
              ClassNotFoundException
       {
          boolean isNull;

          atMode     = in.readBoolean();
          positional = in.readBoolean();
          column     = in.readInt();
          fmt = readString(in);
          final BaseDataType data = readBaseDataType(in);
          this.data = () -> data;
       }

       /**
        * Replacement for the default object writing method. The latest
        * state is written to the output destination.
        *
        * @param    out
        *           The output destination to which fields will be saved.
        *
        * @throws   IOException
        *           In case of I/O errors.
        */
       public void writeExternal(ObjectOutput out)
       throws IOException
       {
          out.writeBoolean(atMode);
          out.writeBoolean(positional);
          out.writeInt(column);
          writeString(out, fmt);
          writeBaseDataType(out, savedVar);
       }
    }