aboutsummaryrefslogtreecommitdiffstats
path: root/client/src/main/java/ai/vespa/client/dsl/A.java
blob: b8c1cd4f5b2639b78adf230199012b27ad1b928b (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 Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.client.dsl;

import java.util.HashMap;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * Helper class for generating Annotation
 * https://docs.vespa.ai/en/reference/query-language-reference.html#annotations
 */
public class A {

    private A() { }

    private final static Annotation EMPTY = new Annotation();

    /**
     * Empty annotation.
     *
     * @return the annotation
     */
    static public Annotation empty() {
        return EMPTY;
    }

    /**
     * Filter annotation.
     *
     * @return the annotation
     */
    static public Annotation filter() {
        return a("filter", true);
    }

    /**
     * Default index annotation.
     *
     * @param index the search index
     * @return the annotation
     */
    static public Annotation defaultIndex(String index) {
        return a("defaultIndex", index);
    }

    /**
     * Arbitrary key-value pair annotation.
     *
     * @param name  the name
     * @param value the value
     * @return the annotation
     */
    public static Annotation a(String name, Object value) {
        Map<String, Object> map = new HashMap<>();
        map.put(name, value);
        return new Annotation(map);
    }

    /**
     * Arbitrary annotation given by the map.
     *
     * @param annotation the annotation
     * @return the annotation
     */
    public static Annotation a(Map<String, Object> annotation) {
        if (annotation.isEmpty()) {
            return empty();
        }
        return new Annotation(annotation);
    }

    public static Annotation a(String n1, Object v1, String n2, Object v2) {
        return a(new Object[][]{{n1, v1}, {n2, v2}});
    }

    public static Annotation a(String n1, Object v1, String n2, Object v2, String n3, Object v3) {
        return a(new Object[][]{{n1, v1}, {n2, v2}, {n3, v3}});
    }

    public static Annotation a(String n1, Object v1, String n2, Object v2, String n3, Object v3, String n4, Object v4) {
        return a(new Object[][]{{n1, v1}, {n2, v2}, {n3, v3}, {n4, v4}});
    }

    public static Annotation a(String n1, Object v1, String n2, Object v2, String n3, Object v3, String n4, Object v4,
                               String n5, Object v5) {
        return a(new Object[][]{{n1, v1}, {n2, v2}, {n3, v3}, {n4, v4}, {n5, v5}});
    }

    public static Annotation a(String n1, Object v1, String n2, Object v2, String n3, Object v3, String n4, Object v4,
                               String n5, Object v5, String n6, Object v6) {
        return a(new Object[][]{{n1, v1}, {n2, v2}, {n3, v3}, {n4, v4}, {n5, v5}, {n6, v6}});
    }

    private static Annotation a(Object[][] kvpairs) {
        return new Annotation(Stream.of(kvpairs).collect(Collectors.toMap(o -> o[0].toString(), o -> o[1])));
    }

    static boolean hasAnnotation(Annotation annotation) {
        return annotation != null && !EMPTY.equals(annotation);
    }

}