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

com.yahoo.document.predicate.FeatureSet Maven / Gradle / Ivy

The newest version!
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.document.predicate;

import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;

/**
 * @author Simon Thoresen Hult
 */
public class FeatureSet extends PredicateValue {

    private Set values;
    private String key;

    public FeatureSet(String key, String... values) {
        this(key, List.of(values));
    }

    public FeatureSet(String key, Collection values) {
        Objects.requireNonNull(key, "key");
        if (values == null) {
            throw new NullPointerException("values");
        }
        this.key = key;
        this.values = new TreeSet<>(values);
    }

    public FeatureSet setKey(String key) {
        Objects.requireNonNull(key, "key");
        this.key = key;
        return this;
    }

    public String getKey() {
        return key;
    }

    public FeatureSet addValue(String value) {
        Objects.requireNonNull(value, "value");
        values.add(value);
        return this;
    }

    public FeatureSet addValues(Collection values) {
        if (values == null) {
            throw new NullPointerException("values");
        }
        this.values.addAll(values);
        return this;
    }

    public FeatureSet setValues(Collection values) {
        if (values == null) {
            throw new NullPointerException("values");
        }
        this.values.clear();
        this.values.addAll(values);
        return this;
    }

    public Set getValues() {
        return values;
    }

    @Override
    public FeatureSet clone() throws CloneNotSupportedException {
        FeatureSet obj = (FeatureSet)super.clone();
        obj.values = new TreeSet<>(values);
        return obj;
    }

    @Override
    public int hashCode() {
        return (key.hashCode() + values.hashCode()) * 31;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == this) {
            return true;
        }
        if (!(obj instanceof FeatureSet rhs)) {
            return false;
        }
        if (!key.equals(rhs.key)) {
            return false;
        }
        if (!values.equals(rhs.values)) {
            return false;
        }
        return true;
    }

    @Override
    protected void appendTo(StringBuilder out) {
        appendInAsTo("in", out);
    }

    protected void appendNegatedTo(StringBuilder out) {
        appendInAsTo("not in", out);
    }

    private void appendInAsTo(String in, StringBuilder out) {
        appendQuotedTo(key, out);
        out.append(' ').append(in).append(" [");
        for (Iterator it = values.iterator(); it.hasNext(); ) {
            appendQuotedTo(it.next(), out);
            if (it.hasNext()) {
                out.append(", ");
            }
        }
        out.append("]");
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy