All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.yahoo.vespa.documentmodel.FieldView Maven / Gradle / Ivy

There is a newer version: 8.409.18
Show newest version
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.documentmodel;

import com.yahoo.vespa.objects.FieldBase;

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 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 getFields() { return fields.values(); }
    public FieldBase 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(FieldBase 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(var field : view.getFields()) {
            add(field);
        }
        return this;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy