summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/query/profile/types/QueryProfileFieldType.java
blob: ff12224823fc94b237cde8f535b357f0d96274f0 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.query.profile.types;

import com.yahoo.search.query.profile.QueryProfile;
import com.yahoo.search.query.profile.QueryProfileRegistry;
import com.yahoo.search.query.profile.compiled.CompiledQueryProfile;

/**
 * Represents a query profile field type which is a reference to a query profile.
 * The reference may optionally specify the type of the referred query profile.
 *
 * @author bratseth
 */
public class QueryProfileFieldType extends FieldType {

    private final QueryProfileType type;

    public static QueryProfileFieldType fromString(String queryProfileName, QueryProfileTypeRegistry registry) {
        if (queryProfileName==null || queryProfileName.equals(""))
            return new QueryProfileFieldType(null);

        if (registry==null)
            throw new IllegalArgumentException("Can not resolve query profile type '" + queryProfileName +
                                               "' because no registry is provided");
        QueryProfileType queryProfileType=registry.getComponent(queryProfileName);
        if (queryProfileType==null)
            throw new IllegalArgumentException("Could not resolve query profile type '" + queryProfileName + "'");
        return new QueryProfileFieldType(registry.getComponent(queryProfileName));
    }

    public QueryProfileFieldType() { this(null); }

    public QueryProfileFieldType(QueryProfileType type) {
        this.type = type;
    }

    /** Returns the query profile type of this, or null if any type works */
    public QueryProfileType getQueryProfileType() { return type; }

    @Override
    public Class<?> getValueClass() { return QueryProfile.class; }

    @Override
    public String stringValue() {
        return "query-profile" + (type!=null ? ":" + type.getId().getName() : "");
    }

    @Override
    public String toString() {
        return "field type " + stringValue();
    }

    @Override
    public String toInstanceDescription() {
        return "reference to a query profile" + (type!=null ? " of type '" + type.getId().getName() + "'" : "");
    }

    @Override
    public CompiledQueryProfile convertFrom(Object object, ConversionContext context) {
        String profileId = object.toString();
        if (profileId.startsWith("ref:"))
            profileId = profileId.substring("ref:".length());
        CompiledQueryProfile profile = context.getRegistry().getComponent(profileId);
        if (profile == null) return null;
        if (type != null && ! type.equals(profile.getType())) return null;
        return profile;
    }

    @Override
    public QueryProfile convertFrom(Object object, QueryProfileRegistry registry) {
        QueryProfile profile;
        if (object instanceof String)
            profile = registry.getComponent((String)object);
        else if (object instanceof QueryProfile)
            profile = (QueryProfile)object;
        else
            return null;

        // Verify its type as well
        if (type!=null && type!=profile.getType()) return null;
        return profile;
    }

    @Override
    public int hashCode() {
        if (type == null) return 17;
        return type.hashCode();
    }

    @Override
    public boolean equals(Object o) {
        if (o == this) return true;
        if ( ! (o instanceof QueryProfileFieldType)) return false;
        QueryProfileFieldType other = (QueryProfileFieldType)o;
        return equals(this.type.getId(), other.type.getId());
    }

    private boolean equals(Object o1, Object o2) {
        if (o1 == null) return o2 == null;
        return o1.equals(o2);
    }

}