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

com.datastax.driver.core.AbstractAddressableByIndexData Maven / Gradle / Ivy

The newest version!
/*
 * Copyright DataStax, 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 com.datastax.driver.core;

import com.datastax.driver.core.utils.MoreObjects;
import com.google.common.reflect.TypeToken;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.InetAddress;
import java.nio.ByteBuffer;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

abstract class AbstractAddressableByIndexData>
    extends AbstractGettableByIndexData implements SettableByIndexData {

  final ByteBuffer[] values;

  protected AbstractAddressableByIndexData(ProtocolVersion protocolVersion, int size) {
    super(protocolVersion);
    this.values = new ByteBuffer[size];
  }

  @SuppressWarnings("unchecked")
  protected T setValue(int i, ByteBuffer value) {
    values[i] = value;
    return (T) this;
  }

  @Override
  protected ByteBuffer getValue(int i) {
    return values[i];
  }

  @Override
  public T setBool(int i, boolean v) {
    TypeCodec codec = codecFor(i, Boolean.class);
    ByteBuffer bb;
    if (codec instanceof TypeCodec.PrimitiveBooleanCodec)
      bb = ((TypeCodec.PrimitiveBooleanCodec) codec).serializeNoBoxing(v, protocolVersion);
    else bb = codec.serialize(v, protocolVersion);
    return setValue(i, bb);
  }

  @Override
  public T setByte(int i, byte v) {
    TypeCodec codec = codecFor(i, Byte.class);
    ByteBuffer bb;
    if (codec instanceof TypeCodec.PrimitiveByteCodec)
      bb = ((TypeCodec.PrimitiveByteCodec) codec).serializeNoBoxing(v, protocolVersion);
    else bb = codec.serialize(v, protocolVersion);
    return setValue(i, bb);
  }

  @Override
  public T setShort(int i, short v) {
    TypeCodec codec = codecFor(i, Short.class);
    ByteBuffer bb;
    if (codec instanceof TypeCodec.PrimitiveShortCodec)
      bb = ((TypeCodec.PrimitiveShortCodec) codec).serializeNoBoxing(v, protocolVersion);
    else bb = codec.serialize(v, protocolVersion);
    return setValue(i, bb);
  }

  @Override
  public T setInt(int i, int v) {
    TypeCodec codec = codecFor(i, Integer.class);
    ByteBuffer bb;
    if (codec instanceof TypeCodec.PrimitiveIntCodec)
      bb = ((TypeCodec.PrimitiveIntCodec) codec).serializeNoBoxing(v, protocolVersion);
    else bb = codec.serialize(v, protocolVersion);
    return setValue(i, bb);
  }

  @Override
  public T setLong(int i, long v) {
    TypeCodec codec = codecFor(i, Long.class);
    ByteBuffer bb;
    if (codec instanceof TypeCodec.PrimitiveLongCodec)
      bb = ((TypeCodec.PrimitiveLongCodec) codec).serializeNoBoxing(v, protocolVersion);
    else bb = codec.serialize(v, protocolVersion);
    return setValue(i, bb);
  }

  @Override
  public T setTimestamp(int i, Date v) {
    return setValue(i, codecFor(i, Date.class).serialize(v, protocolVersion));
  }

  @Override
  public T setDate(int i, LocalDate v) {
    return setValue(i, codecFor(i, LocalDate.class).serialize(v, protocolVersion));
  }

  @Override
  public T setTime(int i, long v) {
    TypeCodec codec = codecFor(i, Long.class);
    ByteBuffer bb;
    if (codec instanceof TypeCodec.PrimitiveLongCodec)
      bb = ((TypeCodec.PrimitiveLongCodec) codec).serializeNoBoxing(v, protocolVersion);
    else bb = codec.serialize(v, protocolVersion);
    return setValue(i, bb);
  }

  @Override
  public T setFloat(int i, float v) {
    TypeCodec codec = codecFor(i, Float.class);
    ByteBuffer bb;
    if (codec instanceof TypeCodec.PrimitiveFloatCodec)
      bb = ((TypeCodec.PrimitiveFloatCodec) codec).serializeNoBoxing(v, protocolVersion);
    else bb = codec.serialize(v, protocolVersion);
    return setValue(i, bb);
  }

  @Override
  public T setDouble(int i, double v) {
    TypeCodec codec = codecFor(i, Double.class);
    ByteBuffer bb;
    if (codec instanceof TypeCodec.PrimitiveDoubleCodec)
      bb = ((TypeCodec.PrimitiveDoubleCodec) codec).serializeNoBoxing(v, protocolVersion);
    else bb = codec.serialize(v, protocolVersion);
    return setValue(i, bb);
  }

  @Override
  public T setString(int i, String v) {
    return setValue(i, codecFor(i, String.class).serialize(v, protocolVersion));
  }

  @Override
  public T setBytes(int i, ByteBuffer v) {
    return setValue(i, codecFor(i, ByteBuffer.class).serialize(v, protocolVersion));
  }

  @Override
  public T setBytesUnsafe(int i, ByteBuffer v) {
    return setValue(i, v == null ? null : v.duplicate());
  }

  @Override
  public T setVarint(int i, BigInteger v) {
    return setValue(i, codecFor(i, BigInteger.class).serialize(v, protocolVersion));
  }

  @Override
  public T setDecimal(int i, BigDecimal v) {
    return setValue(i, codecFor(i, BigDecimal.class).serialize(v, protocolVersion));
  }

  @Override
  public T setUUID(int i, UUID v) {
    return setValue(i, codecFor(i, UUID.class).serialize(v, protocolVersion));
  }

  @Override
  public T setInet(int i, InetAddress v) {
    return setValue(i, codecFor(i, InetAddress.class).serialize(v, protocolVersion));
  }

  @Override
  @SuppressWarnings("unchecked")
  public  T setList(int i, List v) {
    return setValue(i, codecFor(i).serialize(v, protocolVersion));
  }

  @Override
  public  T setList(int i, List v, Class elementsClass) {
    return setValue(i, codecFor(i, TypeTokens.listOf(elementsClass)).serialize(v, protocolVersion));
  }

  @Override
  public  T setList(int i, List v, TypeToken elementsType) {
    return setValue(i, codecFor(i, TypeTokens.listOf(elementsType)).serialize(v, protocolVersion));
  }

  @Override
  @SuppressWarnings("unchecked")
  public  T setMap(int i, Map v) {
    return setValue(i, codecFor(i).serialize(v, protocolVersion));
  }

  @Override
  public  T setMap(int i, Map v, Class keysClass, Class valuesClass) {
    return setValue(
        i, codecFor(i, TypeTokens.mapOf(keysClass, valuesClass)).serialize(v, protocolVersion));
  }

  @Override
  public  T setMap(int i, Map v, TypeToken keysType, TypeToken valuesType) {
    return setValue(
        i, codecFor(i, TypeTokens.mapOf(keysType, valuesType)).serialize(v, protocolVersion));
  }

  @Override
  @SuppressWarnings("unchecked")
  public  T setSet(int i, Set v) {
    return setValue(i, codecFor(i).serialize(v, protocolVersion));
  }

  @Override
  public  T setSet(int i, Set v, Class elementsClass) {
    return setValue(i, codecFor(i, TypeTokens.setOf(elementsClass)).serialize(v, protocolVersion));
  }

  @Override
  public  T setSet(int i, Set v, TypeToken elementsType) {
    return setValue(i, codecFor(i, TypeTokens.setOf(elementsType)).serialize(v, protocolVersion));
  }

  @Override
  public T setUDTValue(int i, UDTValue v) {
    return setValue(i, codecFor(i, UDTValue.class).serialize(v, protocolVersion));
  }

  @Override
  public T setTupleValue(int i, TupleValue v) {
    return setValue(i, codecFor(i, TupleValue.class).serialize(v, protocolVersion));
  }

  @Override
  public  T set(int i, V v, Class targetClass) {
    return set(i, v, codecFor(i, targetClass));
  }

  @Override
  public  T set(int i, V v, TypeToken targetType) {
    return set(i, v, codecFor(i, targetType));
  }

  @Override
  public  T set(int i, V v, TypeCodec codec) {
    checkType(i, codec.getCqlType().getName());
    return setValue(i, codec.serialize(v, protocolVersion));
  }

  @Override
  public T setToNull(int i) {
    return setValue(i, null);
  }

  @Override
  public boolean equals(Object o) {
    if (!(o instanceof AbstractAddressableByIndexData)) return false;

    AbstractAddressableByIndexData that = (AbstractAddressableByIndexData) o;
    if (values.length != that.values.length) return false;

    if (this.protocolVersion != that.protocolVersion) return false;

    // Deserializing each value is slightly inefficient, but comparing
    // the bytes could in theory be wrong (for varint for instance, 2 values
    // can have different binary representation but be the same value due to
    // leading zeros). So we don't take any risk.
    for (int i = 0; i < values.length; i++) {
      DataType thisType = getType(i);
      DataType thatType = that.getType(i);
      if (!thisType.equals(thatType)) return false;

      Object thisValue = this.codecFor(i).deserialize(this.values[i], this.protocolVersion);
      Object thatValue = that.codecFor(i).deserialize(that.values[i], that.protocolVersion);
      if (!MoreObjects.equal(thisValue, thatValue)) return false;
    }
    return true;
  }

  @Override
  public int hashCode() {
    // Same as equals
    int hash = 31;
    for (int i = 0; i < values.length; i++)
      hash +=
          values[i] == null ? 1 : codecFor(i).deserialize(values[i], protocolVersion).hashCode();
    return hash;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy