summaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/Alternative.java
blob: 441082e95c1cd50432ca408aba11e309fa21d9fa (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;

import java.util.Objects;
import java.util.function.Supplier;

/**
 * Simple monad class, like Optional but with support for chaining alternatives in preferred order.
 *
 * Holds a current value (immutably), but if the current value is null provides an easy way to obtain an instance
 * with another value, ad infinitum.
 *
 * Instances of this class are immutable and thread-safe.
 *
 * @author bakksjo
 */
public class Alternative<T> {
    private final T value;

    private Alternative(final T value) {
        this.value = value;
    }

    /**
     * Creates an instance with the supplied value.
     */
    public static <T> Alternative<T> preferred(final T value) {
        return new Alternative<>(value);
    }

    /**
     * Returns itself (unchanged) iff current value != null,
     * otherwise returns a new instance with the value supplied by the supplier.
     */
    public Alternative<T> alternatively(final Supplier<? extends T> supplier) {
        if (value != null) {
            return this;
        }

        return new Alternative<>(supplier.get());
    }

    /**
     * Returns the held value iff != null, otherwise invokes the supplier and returns its value.
     */
    public T orElseGet(final Supplier<? extends T> supplier) {
        if (value != null) {
            return value;
        }
        return supplier.get();
    }

    @Override
    public boolean equals(final Object o) {
        if (!(o instanceof Alternative<?>)) {
            return false;
        }

        final Alternative<?> other = (Alternative<?>) o;

        return Objects.equals(value, other.value);
    }

    @Override
    public int hashCode() {
        return Objects.hashCode(value);
    }
}