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

com.scalar.db.sql.Value Maven / Gradle / Ivy

There is a newer version: 3.14.0
Show newest version
package com.scalar.db.sql;

import com.google.common.base.MoreObjects;
import com.scalar.db.sql.common.SqlError;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/** A value of a column. */
@Immutable
public class Value implements Term {

  public final Type type;

  // The type of value depends on the type. If the type is BOOLEAN, the value should be of type
  // java.lang.Boolean. If the type is INT, the value should be of type java.lang.Integer. If the
  // type is BIGINT, the value should be of type java.lang.Long. If the type is FLOAT, the value
  // should be of type java.lang.Float.  If the type is DOUBLE, the value should be of type
  // java.lang.Double. If the type is TEXT, the value should be of type java.lang.String. If the
  // type is BLOB_BYTE_BUFFER, the value should be of type java.nio.ByteBuffer. If the type is
  // BLOB_BYTES, the value should be of type byte[]. If the type is NULL, the value should be null.
  @Nullable public final Object value;

  private Value(Type type, @Nullable Object value) {
    this.type = Objects.requireNonNull(type);
    this.value = value;
  }

  @Override
  public String toString() {
    return MoreObjects.toStringHelper(this).add("type", type).add("value", value).toString();
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof Value)) {
      return false;
    }
    Value that = (Value) o;
    if (type != that.type) {
      return false;
    }
    if (type == Type.BLOB_BYTES) {
      return Arrays.equals((byte[]) value, (byte[]) that.value);
    }
    return Objects.equals(value, that.value);
  }

  @Override
  public int hashCode() {
    return Objects.hash(type, value);
  }

  /**
   * Returns a {@code Value} object for Boolean.
   *
   * @param value a Boolean value
   * @return a {@code Value} object
   */
  public static Value ofBoolean(boolean value) {
    return new Value(Type.BOOLEAN, value);
  }

  /**
   * Returns a {@code Value} object for Int.
   *
   * @param value a Int value
   * @return a {@code Value} object
   */
  public static Value ofInt(int value) {
    return new Value(Type.INT, value);
  }

  /**
   * Returns a {@code Value} object for BigInt.
   *
   * @param value a BigInt value
   * @return a {@code Value} object
   */
  public static Value ofBigInt(long value) {
    return new Value(Type.BIGINT, value);
  }

  /**
   * Returns a {@code Value} object for Float.
   *
   * @param value a Float value
   * @return a {@code Value} object
   */
  public static Value ofFloat(float value) {
    return new Value(Type.FLOAT, value);
  }

  /**
   * Returns a {@code Value} object for Double.
   *
   * @param value a Double value
   * @return a {@code Value} object
   */
  public static Value ofDouble(double value) {
    return new Value(Type.DOUBLE, value);
  }

  /**
   * Returns a {@code Value} object for Text.
   *
   * @param value a Text value
   * @return a {@code Value} object
   */
  public static Value ofText(String value) {
    return new Value(Type.TEXT, Objects.requireNonNull(value));
  }

  /**
   * Returns a {@code Value} object for Blob.
   *
   * @param value a Blob value
   * @return a {@code Value} object
   */
  public static Value ofBlob(ByteBuffer value) {
    return new Value(Type.BLOB_BYTE_BUFFER, Objects.requireNonNull(value));
  }

  /**
   * Returns a {@code Value} object for Blob.
   *
   * @param value a Blob value
   * @return a {@code Value} object
   */
  public static Value ofBlob(byte[] value) {
    return new Value(Type.BLOB_BYTES, Objects.requireNonNull(value));
  }

  /**
   * Returns a {@code Value} object for null.
   *
   * @return a {@code Value} object
   */
  public static Value ofNull() {
    return new Value(Type.NULL, null);
  }

  /**
   * Returns a {@code Value} object for a corresponding type of the specified value.
   *
   * @param value a value
   * @return a {@code Value} object
   */
  public static Value of(@Nullable Object value) {
    if (value == null) {
      return Value.ofNull();
    }
    if (value instanceof Boolean) {
      return Value.ofBoolean((boolean) value);
    }
    if (value instanceof Integer) {
      return Value.ofInt((int) value);
    }
    if (value instanceof Long) {
      return Value.ofBigInt((long) value);
    }
    if (value instanceof Float) {
      return Value.ofFloat((float) value);
    }
    if (value instanceof Double) {
      return Value.ofDouble((double) value);
    }
    if (value instanceof String) {
      return Value.ofText((String) value);
    }
    if (value instanceof ByteBuffer) {
      return Value.ofBlob((ByteBuffer) value);
    }
    if (value instanceof byte[]) {
      return Value.ofBlob((byte[]) value);
    }

    throw new IllegalArgumentException(
        SqlError.UNSUPPORTED_TYPE.buildMessage(value.getClass().getName()));
  }

  public enum Type {
    BOOLEAN,
    INT,
    BIGINT,
    FLOAT,
    DOUBLE,
    TEXT,
    BLOB_BYTE_BUFFER,
    BLOB_BYTES,
    NULL
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy