aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/collections/Optionals.java
blob: 27a1e837900eefebbba4a233020c31e4490367d2 (plain) (blame)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
package com.yahoo.collections;

import com.google.common.collect.Comparators;

import java.util.Optional;
import java.util.function.BinaryOperator;

/**
 * @author jonmv
 */
public class Optionals {

    private Optionals() { }

    /** Returns the first non-empty optional, or empty if all are empty. */
    @SafeVarargs
    public static <T> Optional<T> firstNonEmpty(Optional<T>... optionals) {
        for (Optional<T> optional : optionals)
            if (optional.isPresent())
                return optional;
        return Optional.empty();
    }

    /** Returns the non-empty optional with the lowest value, or empty if all are empty. */
    @SafeVarargs
    public static <T extends Comparable<T>> Optional<T> min(Optional<T>... optionals) {
        Optional<T> best = Optional.empty();
        for (Optional<T> optional : optionals)
            if (best.isEmpty() || optional.isPresent() && optional.get().compareTo(best.get()) < 0)
                best = optional;
        return best;
    }

    /** Returns the non-empty optional with the highest value, or empty if all are empty. */
    @SafeVarargs
    public static <T extends Comparable<T>> Optional<T> max(Optional<T>... optionals) {
        Optional<T> best = Optional.empty();
        for (Optional<T> optional : optionals)
            if (best.isEmpty() || optional.isPresent() && optional.get().compareTo(best.get()) > 0)
                best = optional;
        return best;
    }

    /** Returns whether either optional is empty, or both are present and equal. */
    public static <T> boolean equalIfBothPresent(Optional<T> first, Optional<T> second) {
        return first.isEmpty() || second.isEmpty() || first.equals(second);
    }

    /** Returns whether the optional is empty, or present and equal to the given value. */
    public static <T> boolean emptyOrEqual(Optional<T> optional, T value) {
        return optional.isEmpty() || optional.equals(Optional.of(value));
    }

}