aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/Result.java
blob: 0665364bf62a07413fedc1f327aa9fadda1d1b81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package org.zwobble.mammoth;

import com.google.common.collect.ImmutableList;

import java.util.List;
import java.util.function.Function;

public class Result<T> {
    public static <T> Result<List<T>> concat(Iterable<Result<T>> results) {
        ImmutableList.Builder<T> elements = ImmutableList.builder();
        for (Result<T> result : results) {
            elements.add(result.value);
        }
        return new Result<>(elements.build());
    }

    public static <T> Result<T> success(T value) {
        return new Result<>(value);
    }

    private final T value;

    public Result(T value) {
        this.value = value;
    }

    public T getValue() {
        return value;
    }

    public <R> Result<R> map(Function<T, R> function) {
        return new Result<>(function.apply(value));
    }
}