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

Java tutorial

Introduction

Here is the source code for ei.ne.ke.cassandra.cql3.template.Term.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 java.util.regex.Pattern;

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

import com.google.common.base.Preconditions;

/**
 * A CQL3 <term>.
 */
public class Term {

    /**
     * The regex of a CQL3 term.
     * @see http://cassandra.apache.org/doc/cql3/CQL.html#identifiers
     */
    public static final Pattern PATTERN = Pattern.compile("^((\\w+)|(\"\\w+\")|(\\?))$");
    public static final Term VARIABLE = new Term("?");

    private final String value;

    /**
     *
     *
     * @param value
     */
    public Term(String value) {
        String trimmedValue = StringUtils.trim(value);
        Preconditions.checkArgument(PATTERN.matcher(trimmedValue).matches(), "Value must match pattern " + PATTERN);
        this.value = trimmedValue;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String toString() {
        return value;
    }

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

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

}