aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client-api/src/main/java/ai/vespa/feed/client/OperationParameters.java
blob: 0ec40e114df0f415c04fe2cf5ae8507b22ebca4c (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.feed.client;

import java.time.Duration;
import java.util.Objects;
import java.util.Optional;
import java.util.OptionalInt;

/**
 * Per-operation feed parameters
 *
 * @author bjorncs
 * @author jonmv
 */
public class OperationParameters {

    static final OperationParameters empty = new OperationParameters(false, null, null, null, 0);

    private final boolean create;
    private final String condition;
    private final Duration timeout;
    private final String route;
    private final int tracelevel;

    private OperationParameters(boolean create, String condition, Duration timeout, String route, int tracelevel) {
        this.create = create;
        this.condition = condition;
        this.timeout = timeout;
        this.route = route;
        this.tracelevel = tracelevel;
    }

    public static OperationParameters empty() { return empty; }

    public OperationParameters createIfNonExistent(boolean create) {
        return new OperationParameters(create, condition, timeout, route, tracelevel);
    }

    public OperationParameters testAndSetCondition(String condition) {
        if (condition.isEmpty())
            throw new IllegalArgumentException("TestAndSetCondition must be non-empty");

        return new OperationParameters(create, condition, timeout, route, tracelevel);
    }

    public OperationParameters timeout(Duration timeout) {
        if (timeout.isNegative() || timeout.isZero())
            throw new IllegalArgumentException("Timeout must be positive, but was " + timeout);

        return new OperationParameters(create, condition, timeout, route, tracelevel);
    }

    public OperationParameters route(String route) {
        if (route.isEmpty())
            throw new IllegalArgumentException("Route must be non-empty");

        return new OperationParameters(create, condition, timeout, route, tracelevel);
    }

    public OperationParameters tracelevel(int tracelevel) {
        if (tracelevel < 1 || tracelevel > 9)
            throw new IllegalArgumentException("Tracelevel must be in [1, 9]");

        return new OperationParameters(create, condition, timeout, route, tracelevel);
    }

    public boolean createIfNonExistent() { return create; }
    public Optional<String> testAndSetCondition() { return Optional.ofNullable(condition); }
    public Optional<Duration> timeout() { return Optional.ofNullable(timeout); }
    public Optional<String> route() { return Optional.ofNullable(route); }
    public OptionalInt tracelevel() { return tracelevel == 0 ? OptionalInt.empty() : OptionalInt.of(tracelevel); }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        OperationParameters that = (OperationParameters) o;
        return create == that.create && tracelevel == that.tracelevel && Objects.equals(condition, that.condition) && Objects.equals(timeout, that.timeout) && Objects.equals(route, that.route);
    }

    @Override
    public int hashCode() {
        return Objects.hash(create, condition, timeout, route, tracelevel);
    }

    @Override
    public String toString() {
        return "OperationParameters{" +
               "create=" + create +
               ", condition='" + condition + '\'' +
               ", timeout=" + timeout +
               ", route='" + route + '\'' +
               ", tracelevel=" + tracelevel +
               '}';
    }

}