Java tutorial
/* * 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.springframework.util.ObjectUtils; import com.google.common.base.Preconditions; /** * A CQL3 <identifier>. */ public class Identifier { /** * The regex of a CQL3 identifier. * @see http://cassandra.apache.org/doc/cql3/CQL.html#identifiers */ public static final Pattern PATTERN = Pattern.compile("^((\\w+)|(\"\\w+\")|(\\*)|(COUNT\\(1\\)))$"); public static final Identifier ALL = new Identifier("*"); public static final Identifier COUNT = new Identifier("COUNT(1)"); private final String value; /** * * * @param value */ public Identifier(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 Identifier)) { return false; } else { Identifier that = (Identifier) rhs; return ObjectUtils.nullSafeEquals(this.value, that.value); } } /** * {@inheritDoc} */ @Override public int hashCode() { return this.value.hashCode(); } }