ei.ne.ke.cassandra.cql3.template.Relation.java Source code

Java tutorial

Introduction

Here is the source code for ei.ne.ke.cassandra.cql3.template.Relation.java

Source

/*
 * Copyright 2013 EK3 Technologies, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package ei.ne.ke.cassandra.cql3.template;

import org.apache.commons.lang.builder.HashCodeBuilder;
import org.springframework.util.ObjectUtils;

import com.google.common.base.Preconditions;

/**
 * CQL3 relation.
 */
public class Relation {

    protected final Identifier identifier;
    private final Comparator comparator;
    private final Term term;

    /**
     * Creates a new relation with the given identifier and comparator, assuming
     * a variable term.
     * @param identifier the identifier.
     * @param comparator the comparator.
     */
    public Relation(Identifier identifier, Comparator comparator) {
        this.identifier = Preconditions.checkNotNull(identifier);
        this.comparator = Preconditions.checkNotNull(comparator);
        this.term = Term.VARIABLE;
    }

    /**
     * Creates a new relation with the given identifier, comparator, and term.
     * @param identifier the identifier.
     * @param comparator the comparator.
     * @param term the term.
     */
    public Relation(Identifier identifier, Comparator comparator, Term term) {
        this.identifier = Preconditions.checkNotNull(identifier);
        this.comparator = Preconditions.checkNotNull(comparator);
        this.term = Preconditions.checkNotNull(term);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(identifier);
        sb.append(" ");
        sb.append(comparator);
        sb.append(" ");
        sb.append(term);
        return sb.toString();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean equals(Object rhs) {
        if (this == rhs) {
            return true;
        } else if (!(rhs instanceof Relation)) {
            return false;
        } else {
            Relation that = (Relation) rhs;
            return ObjectUtils.nullSafeEquals(this.identifier, that.identifier)
                    && ObjectUtils.nullSafeEquals(this.comparator, that.comparator)
                    && ObjectUtils.nullSafeEquals(this.term, that.term);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return HashCodeBuilder.reflectionHashCode(this);
    }

    /**
     * @return the identifier
     */
    public Identifier getIdentifier() {
        return identifier;
    }

    /**
     * @return the comparator
     */
    public Comparator getComparator() {
        return comparator;
    }

    /**
     * @return the term
     */
    public Term getTerm() {
        return term;
    }

}