FieldModifier.java

package de.siegmar.fastcsv.reader;

import java.util.Objects;
import java.util.function.Function;

/// Implementations of this interface are used within [CsvCallbackHandler] implementations to modify the fields of
/// a CSV record before storing them in the resulting object.
///
/// @see FieldModifiers
public interface FieldModifier {

    /// Builds a modifier that modifies the field content using the provided function.
    /// Comments are not modified.
    ///
    /// @param function the function to modify the field content. The contract of
    ///                 [FieldModifier#modify(long, int, boolean, String)] applies:
    ///                 the field content passed to the function is never `null` and the return value must not be
    ///                 `null`
    /// @return a new field modifier that applies the function to the field content
    /// @throws NullPointerException if `function` is `null`
    static FieldModifier modify(final Function<? super String, String> function) {
        Objects.requireNonNull(function, "function must not be null");
        return new FieldModifier() {
            @Override
            public String modify(final long startingLineNumber, final int fieldIdx,
                                 final boolean quoted, final String field) {
                return function.apply(field);
            }
        };
    }

    /// Called for every field, including the fields of a header read from the first record.
    /// Not called for comments, empty lines or an external header.
    /// The default implementation returns the field content as is.
    ///
    /// @param startingLineNumber the starting line number (starting with 1)
    /// @param fieldIdx           the field index (starting with 0)
    /// @param quoted             `true` if the field was a quoted field
    /// @param field              the field content, never `null`
    /// @return the modified field content (must not be `null`)
    default String modify(final long startingLineNumber, final int fieldIdx, final boolean quoted, final String field) {
        return field;
    }

    /// Called for every comment.
    /// The default implementation returns the comment text as is.
    ///
    /// @param startingLineNumber the starting line number (starting with 1)
    /// @param field              the comment text, never `null`
    /// @return the modified comment text (must not be `null`)
    default String modifyComment(final long startingLineNumber, final String field) {
        return field;
    }

    /// Chains multiple modifiers.
    ///
    /// @param after the next modifier to use
    /// @return a composed field modifier that first applies this modifier and then applies the after modifier
    default FieldModifier andThen(final FieldModifier after) {
        return new FieldModifier() {
            @Override
            public String modify(final long startingLineNumber, final int fieldIdx, final boolean quoted,
                                 final String field) {
                return after.modify(startingLineNumber, fieldIdx, quoted,
                    FieldModifier.this.modify(startingLineNumber, fieldIdx, quoted, field));
            }

            @Override
            public String modifyComment(final long startingLineNumber, final String field) {
                return after.modifyComment(startingLineNumber,
                    FieldModifier.this.modifyComment(startingLineNumber, field));
            }
        };
    }

}