package org.zwobble.mammoth.internal.results; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import org.zwobble.mammoth.internal.documents.Style; import java.util.List; import java.util.Optional; import java.util.Set; import java.util.function.BiFunction; import java.util.function.Function; import static org.zwobble.mammoth.internal.util.MammothSets.set; public class InternalResult implements org.zwobble.mammoth.Result { public static InternalResult> concat(Iterable> results) { ImmutableList.Builder elements = ImmutableList.builder(); ImmutableSet.Builder warnings = ImmutableSet.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), Sets.union(first.warnings, second.warnings).immutableCopy()); } public static InternalResult> empty() { return new InternalResult<>(Optional.empty(), set()); } public static InternalResult success(T value) { return new InternalResult<>(value, set()); } private final T value; private final Set warnings; public InternalResult(T value, Set warnings) { this.value = value; this.warnings = warnings; } public T getValue() { return value; } public Set 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, Sets.union(warnings, intermediateResult.warnings).immutableCopy()); } }