package org.zwobble.mammoth.internal.results; import com.google.common.collect.ImmutableList; import org.zwobble.mammoth.internal.documents.Style; import org.zwobble.mammoth.internal.util.MammothLists; import java.util.List; import java.util.Optional; import java.util.function.BiFunction; import java.util.function.Function; import static org.zwobble.mammoth.internal.util.MammothLists.list; public class InternalResult implements org.zwobble.mammoth.Result { public static InternalResult> concat(Iterable> results) { ImmutableList.Builder elements = ImmutableList.builder(); ImmutableList.Builder warnings = ImmutableList.builder(); for (InternalResult result : results) { elements.add(result.value); warnings.addAll(result.warnings); } return new InternalResult<>(elements.build(), warnings.build()); } public static InternalResult map( InternalResult first, InternalResult second, BiFunction function) { return new InternalResult<>( function.apply(first.value, second.value), MammothLists.concat(first.warnings, second.warnings)); } public static InternalResult> empty() { return new InternalResult<>(Optional.empty(), list()); } public static InternalResult success(T value) { return new InternalResult<>(value, list()); } private final T value; private final List warnings; public InternalResult(T value, List warnings) { this.value = value; this.warnings = warnings; } public T getValue() { return value; } public List getWarnings() { return warnings; } public InternalResult map(Function function) { return new InternalResult<>(function.apply(value), warnings); } public InternalResult flatMap(Function> function) { InternalResult intermediateResult = function.apply(value); return new InternalResult<>( intermediateResult.value, MammothLists.concat(warnings, intermediateResult.warnings)); } }