aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/compiled/ValueWithSource.java
blob: 6475cbd362491605cd83c22b559fb1e51632c7c5 (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
98
99
100
101
102
103
104
105
106
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.compiled;

import com.yahoo.search.query.profile.DimensionValues;
import com.yahoo.search.query.profile.types.QueryProfileType;

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

/**
 * A value in a query profile with information about its source.
 *
 * @author bratseth
 */
public class ValueWithSource {

    private final Object value;

    private final String source;

    private final boolean isUnoverridable;

    private final boolean isQueryProfile;

    private final QueryProfileType type;

    /** The dimension values specifying a variant in that profile, or null if it is not in a variant */
    private final DimensionValues variant;

    public ValueWithSource(Object value,
                           String source,
                           boolean isUnoverridable, boolean isQueryProfile, QueryProfileType type,
                           DimensionValues variant) {
        this.value = value;
        this.source = source;
        this.isUnoverridable = isUnoverridable;
        this.isQueryProfile = isQueryProfile;
        this.type = type;
        this.variant = variant;
    }

    /**
     * Returns the value at this key, or null if none
     * (in which case this is references a query profile which has no value set).
     */
    public Object value() { return value; }

    /** Returns the source of the query profile having a value */
    public String source() { return source; }

    /** Returns true if this value cannot be overridden in queries */
    public boolean isUnoverridable() { return isUnoverridable; }

    /**
     * Returns true if this key references a query profile (i.e a non-leaf).
     * In this case the value may or may not be null, as non-leafs may have values.
     */
    public boolean isQueryProfile() { return isQueryProfile; }

    /** Returns tye type of this if it refers to a query profile (not a leaf value), and it has a type */
    public QueryProfileType queryProfileType() { return type; }

    public ValueWithSource withValue(Object value) {
        return new ValueWithSource(value, source, isUnoverridable, isQueryProfile, type, variant);
    }

    public ValueWithSource withSource(String source) {
        return new ValueWithSource(value, source, isUnoverridable, isQueryProfile, type, variant);
    }

    public ValueWithSource withVariant(Optional<DimensionValues> variant) {
        return new ValueWithSource(value, source, isUnoverridable, isQueryProfile, type, variant.orElse(null));
    }

    /** Returns the variant having this value, or empty if it's not in a variant */
    public Optional<DimensionValues> variant() { return Optional.ofNullable(variant); }

    @Override
    public int hashCode() {
        // Value is always a value object. Don't include source in identity.
        return Objects.hash(value, isUnoverridable, type);
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof ValueWithSource)) return false;

        ValueWithSource other = (ValueWithSource)o;
        if ( ! Objects.equals(this.value, other.value)) return false;
        if ( ! Objects.equals(this.isUnoverridable, other.isUnoverridable)) return false;
        if ( ! Objects.equals(this.type, other.type)) return false;
        return true;
    }

    @Override
    public String toString() {
        if (source == null && variant == null) return value.toString();

        return value + " (" +
               ( source != null ? "from query profile '" + source + "'" : "") +
               ( variant != null ? " variant " + variant : "") +
                ")";
    }

}