aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/vespa/documentmodel/FieldView.java
blob: 73d699dda1b66709713a78c5bd9e0ab0d56b1459 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.documentmodel;

import com.yahoo.document.Field;

import java.io.Serializable;
import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * @author    baldersheim
 */
public class FieldView implements Serializable {

    private final String name;
    private final Map<String, Field> fields = new LinkedHashMap<>();

    /**
     * Creates a view with a name
     * @param name Name of the view.
     */
    public FieldView(String name) {
        this.name = name;
    }
    public String getName()              { return name; }
    public Collection<Field> getFields() { return fields.values(); }
    public Field get(String name)        { return fields.get(name); }
    public void remove(String name)      { fields.remove(name); }

    /**
     * This method will add a field to a view. All fields must come from the same document type. Not enforced here.
     * @param field The field to add.
     * @return Itself for chaining purposes.
     */
    public FieldView add(Field field) {
        if (fields.containsKey(field.getName())) {
            if ( ! fields.get(field.getName()).equals(field)) {
                throw new IllegalArgumentException(
                        "View '" + name + "' already contains a field with name '" +
                        field.getName() + "' and definition : " +
                        fields.get(field.getName()).toString() + ". Your is : " + field.toString());
            }
        } else {
            fields.put(field.getName(), field);
        }
        return this;
    }

    /**
     * This method will join the two views.
     * @param view The view to be joined in to this.
     * @return Itself for chaining.
     */
    public FieldView add(FieldView view) {
        for(Field field : view.getFields()) {
            add(field);
        }
        return this;
    }
}