aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/searchdefinition/parser/ParsedDocument.java
blob: 8cd64ef16f7e0b98dd3f1185a4ecb76c1018a3bb (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.searchdefinition.parser;


import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * This class holds the extracted information after parsing a
 * "document" block in a schema (.sd) file, using simple data
 * structures as far as possible.  Do not put advanced logic here!
 * @author arnej27959
 **/
public class ParsedDocument extends ParsedBlock {
    private final List<String> inherited = new ArrayList<>();
    private final Map<String, ParsedDocument> resolvedInherits = new HashMap();
    private final Map<String, ParsedField> docFields = new HashMap<>();
    private final Map<String, ParsedStruct> docStructs = new HashMap<>();
    private final Map<String, ParsedAnnotation> docAnnotations = new HashMap<>();

    public ParsedDocument(String name) {
        super(name, "document");
    }

    List<String> getInherited() { return List.copyOf(inherited); }
    List<ParsedAnnotation> getAnnotations() { return List.copyOf(docAnnotations.values()); }
    List<ParsedDocument> getResolvedInherits() { return List.copyOf(resolvedInherits.values()); }
    List<ParsedField> getFields() { return List.copyOf(docFields.values()); }
    List<ParsedStruct> getStructs() { return List.copyOf(docStructs.values()); }

    void inherit(String other) { inherited.add(other); }

    void addField(ParsedField field) {
        String fieldName = field.name();
        verifyThat(! docFields.containsKey(fieldName), "already has field", fieldName);
        docFields.put(fieldName, field);
    }

    void addStruct(ParsedStruct struct) {
        String sName = struct.name();
        verifyThat(! docStructs.containsKey(sName), "already has struct", sName);
        docStructs.put(sName, struct);
    }

    void addAnnotation(ParsedAnnotation annotation) {
        String annName = annotation.name();
        verifyThat(! docAnnotations.containsKey(annName), "already has annotation", annName);
        docAnnotations.put(annName, annotation);
    }

    public String toString() { return "document " + name(); }

    void resolveInherit(String name, ParsedDocument parsed) {
        verifyThat(inherited.contains(name), "resolveInherit for non-inherited name", name);
        verifyThat(name.equals(parsed.name()), "resolveInherit name mismatch for", name);
        verifyThat(! resolvedInherits.containsKey(name), "double resolveInherit for", name);
        resolvedInherits.put(name, parsed);
    }
}