All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cinchapi.concourse.export.DelimitedLinesExporter Maven / Gradle / Ivy

/*
 * Copyright (c) 2013-2022 Cinchapi Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.cinchapi.concourse.export;

import java.io.OutputStream;
import java.util.Map;
import java.util.function.Function;

import com.cinchapi.common.base.AnyStrings;
import com.google.common.collect.Iterables;

/**
 * An {@link Exporter} for delimited lines (e.g. CSV).
 *
 * @author Jeff Nelson
 */
final class DelimitedLinesExporter extends Exporter {

    /**
     * The delimiter to use.
     */
    private final char delimiter;

    /**
     * Construct a new instance.
     * 
     * @param data
     * @param output
     */
    protected DelimitedLinesExporter(OutputStream output, char delimiter) {
        super(output);
        this.delimiter = delimiter;
    }

    /**
     * Construct a new instance.
     * 
     * @param output
     * @param toStringFunction
     */
    protected DelimitedLinesExporter(OutputStream output,
            Function toStringFunction, char delimiter) {
        super(output, toStringFunction);
        this.delimiter = delimiter;
    }

    /**
     * {@inheritDoc}
     * 

* /** * Output the {@code data} as a sequence of lines (one line per item) where * each key/value pair is separated by a {@code delimiter}. Conceptually, * this method can be used to transform a collection of {@link Map Maps} to * a CSV file. *

*

* The keys of all the items are joined to form a header line. If an * item does not contain a value for a header key, {@code null} is output * instead. *

* * @param data */ @Override public void export(Iterable> data) { Iterable headers = getHeaders(data); print(headers, delimiter); for (Map item : data) { output.println(); Iterable values = Iterables.transform(headers, header -> item.get(header)); print(values, delimiter); } } /** * Output the {@code values} to the {@code output} where each one is * separated by a {@code delimiter}. * * @param values * @param delimiter * @param output */ @SuppressWarnings("unchecked") private void print(Iterable values, char delimiter) { boolean prependComma = false; for (Object value : values) { if(prependComma) { output.print(','); } String $value; try { $value = toStringFunction.apply((V) value); } catch (ClassCastException e) { $value = value.toString(); } output.print( AnyStrings.ensureWithinQuotesIfNeeded($value, delimiter)); prependComma = true; } output.flush(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy