001/*
002 * This file is part of the JDrupes non-blocking HTTP Codec
003 * Copyright (C) 2016, 2017  Michael N. Lipp
004 *
005 * This program is free software; you can redistribute it and/or modify it 
006 * under the terms of the GNU Lesser General Public License as published
007 * by the Free Software Foundation; either version 3 of the License, or 
008 * (at your option) any later version.
009 *
010 * This program is distributed in the hope that it will be useful, but 
011 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
012 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 
013 * License for more details.
014 *
015 * You should have received a copy of the GNU Lesser General Public License along 
016 * with this program; if not, see <http://www.gnu.org/licenses/>.
017 */
018
019package org.jdrupes.httpcodec.types;
020
021import java.util.function.BiConsumer;
022import java.util.function.Supplier;
023import java.util.stream.Collectors;
024import java.util.stream.StreamSupport;
025
026/**
027 * Implemented by converters that convert header fields with a list of values.
028 * 
029 * Minimal restrictions are imposed on the type used as container for the 
030 * values. It must be {@link Iterable} to provide read access. A supplier
031 * and a function for appending values provide the required write access.
032 * 
033 * @param <T> the container for the values
034 * @param <V> the type of the values
035 */
036public interface MultiValueConverter<T extends Iterable<V>, V>
037        extends Converter<T> {
038
039    /**
040     * Returns the container supplier
041     * 
042     * @return the container supplier
043     */
044    Supplier<T> containerSupplier();
045
046    /**
047     * Returns the value adder
048     * 
049     * @return the value adder
050     */
051    BiConsumer<T, V> valueAdder();
052
053    /**
054     * Returns the value converter.
055     * 
056     * @return the value converter
057     * @deprecated Use {@link #valueConverter(Iterable)} instead. 
058     */
059    @Deprecated
060    Converter<V> valueConverter();
061
062    /**
063     * Returns the value converter. In most cases, the result will be
064     * independent of the container type or instance. However, passing
065     * it makes the selection more flexible.
066     * 
067     * @return the value converter
068     */
069    Converter<V> valueConverter(T value);
070
071    /**
072     * Return whether values should be converted to separate
073     * header fields in {@link Converter#asFieldValue(Object)}.
074     * 
075     * @return the value
076     */
077    boolean separateValues();
078
079    /**
080     * Returns the string representation of this header field as it 
081     * appears in an HTTP message. Note that the returned string may 
082     * span several lines (may contain CR/LF), if the converter is a
083     * {@link MultiValueConverter} with separate values, but never 
084     * has a trailing CR/LF.
085     *
086     * @param fieldName the field name
087     * @param value the value
088     * @return the field as it occurs in a header
089     */
090    default String asHeaderField(String fieldName, T value) {
091        if (!separateValues()) {
092            // Cannot call super here.
093            return fieldName + ": " + asFieldValue(value);
094        }
095        // Convert list of items to separate fields
096        var valueConverter = valueConverter(value);
097        return StreamSupport.stream(value.spliterator(), false).map(
098            item -> fieldName + ": " + valueConverter.asFieldValue(item))
099            .collect(Collectors.joining("\r\n"));
100
101    }
102
103}