aboutsummaryrefslogtreecommitdiffstats
path: root/document/src/main/java/com/yahoo/document/TestAndSetCondition.java
blob: 6f72f03fd542073ca9a57cbd8d8d8b9e7ba774cc (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 Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document;

import java.util.Objects;
import java.util.Optional;

/**
 * The TestAndSetCondition class represents a test and set condition.
 * A test and set condition is an (optional) string representing a
 * document selection (cf. document selection language), which is used
 * to match a document for test and set. If #isPresent evaluates to false,
 * the condition is not present and matches any document.
 *
 * @author Vegard Sjonfjell
 */
public class TestAndSetCondition {

    public static final TestAndSetCondition NOT_PRESENT_CONDITION = new TestAndSetCondition();

    private final String conditionStr;

    public TestAndSetCondition() {
        this("");
    }

    public TestAndSetCondition(String conditionStr) {
        this.conditionStr = conditionStr;
    }

    public String getSelection() { return conditionStr; }

    public boolean isPresent() { return !conditionStr.isEmpty(); }

    /**
     * Maps and optional test and set condition string to a TestAndSetCondition.
     * If the condition string is not present, a "not present" condition is returned
     * @param conditionString test and set condition string (document selection)
     * @return a TestAndSetCondition representing the condition string or a "not present" condition
     */
    public static TestAndSetCondition fromConditionString(Optional<String> conditionString) {
        return conditionString
                .map(TestAndSetCondition::new)
                .orElse(TestAndSetCondition.NOT_PRESENT_CONDITION);
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        TestAndSetCondition that = (TestAndSetCondition) o;
        return conditionStr.equals(that.conditionStr);
    }

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

    @Override
    public String toString() {
        StringBuilder string = new StringBuilder();
        string.append("condition '");
        string.append(conditionStr);
        string.append("'");

        return string.toString();
    }
}