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

com.bazaarvoice.emodb.sor.condition.impl.InConditionImpl Maven / Gradle / Ivy

The newest version!
package com.bazaarvoice.emodb.sor.condition.impl;

import com.bazaarvoice.emodb.common.json.JsonValidator;
import com.bazaarvoice.emodb.common.json.OrderedJson;
import com.bazaarvoice.emodb.sor.condition.ConditionVisitor;
import com.bazaarvoice.emodb.sor.condition.InCondition;
import com.google.common.base.Joiner;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.Set;

public class InConditionImpl extends AbstractCondition implements InCondition {

    private final Set _values;

    public InConditionImpl(Set values) {
        for (Object value : values) {
            JsonValidator.checkValid(value);
        }
        _values = values;
    }

    @Override
    public Set getValues() {
        return _values;
    }

    @Override
    public  V visit(ConditionVisitor visitor, @Nullable T context) {
        return visitor.visit(this, context);
    }

    @Override
    public void appendTo(Appendable buf) throws IOException {
        buf.append("in(");
        Joiner.on(',').appendTo(buf, OrderedJson.orderedStrings(_values));
        buf.append(")");
    }

    @Override
    public boolean equals(Object o) {
        return (this == o) || (o instanceof InCondition) && _values.equals(((InCondition) o).getValues());
    }

    @Override
    public int hashCode() {
        return _values != null ? _values.hashCode() : 0;
    }
}