org.nohope.cassandra.mapservice.Value Maven / Gradle / Ivy
The newest version!
package org.nohope.cassandra.mapservice;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import org.nohope.cassandra.mapservice.columns.CColumn;
import java.util.Objects;
import java.util.Optional;
/**
* @author Ketoth Xupack
* @since 2015-02-12 14:26
*/
public final class Value {
private final CColumn column;
private final Object value;
private final Type type;
private Value(final Type type, final CColumn column, final Object value) {
this.column = column;
this.value = value;
this.type = type;
}
public Object asCassandraValue() {
return column.asCassandraValue(this);
}
public static Value bound(final CColumn column, final T value) {
return new Value<>(Type.BOUND, column, value);
}
@Deprecated
public static Value boundMany(final CColumn column, final T... value) {
return new Value<>(Type.BOUND, (CColumn) column, value);
}
public static Value unbound(final CColumn column) {
return new Value<>(Type.UNBOUND, column, QueryBuilder.bindMarker(column.getName()));
}
public static Value unboundArray(final CColumn column) {
return new Value<>(Type.UNBOUND,
(CColumn) column,
QueryBuilder.bindMarker(column.getName()));
}
public static Value fcall(final CColumn column, final String function, final Object... parameters) {
return new Value<>(Type.FCALL, column, QueryBuilder.fcall(function, parameters));
}
public CColumn getColumn() {
return column;
}
public Object getValue() {
return value;
}
@SuppressWarnings("unchecked")
public Optional getBoundValue() {
return (type == Type.BOUND)
? Optional.of((T) value)
: Optional. empty();
}
public Type getType() {
return type;
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if ((o == null) || (getClass() != o.getClass())) {
return false;
}
final Value> that = (Value>) o;
return column.equals(that.column)
&& (type == that.type)
&& Objects.deepEquals(value, that.value);
}
@Override
public int hashCode() {
int result = column.hashCode();
result = (31 * result) + value.hashCode();
result = (31 * result) + type.hashCode();
return result;
}
@Override
public String toString() {
return "Value{" +
"column=" + column +
", value=" + value +
", type=" + type +
'}';
}
public enum Type {
BOUND, UNBOUND, FCALL
}
}