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

com.scalar.db.sql.Literal 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 java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

/**
 * A literal value, which is primarily used within SQL parsers. This class is needed because, based
 * on the statement alone, the SQL parser cannot distinguish between certain value types, such as
 * BIGINT vs. INT or FLOAT vs. DOUBLE. As a result, it retains these values as string
 * representations. These string representations must be converted to their corresponding types when
 * executing the statement.
 */
@Immutable
public class Literal 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 DECIMAL, the value should be of type java.lang.String, and
  // the content of the value should be a valid decimal number. If the type is FLOAT, the value
  // should be of type java.lang.String, and the content of the value should be a valid float
  // number. If the type is STRING, the value should be of type java.lang.String. If the type is
  // NULL, the value should be null.
  @Nullable public final Object value;

  private Literal(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 Literal)) {
      return false;
    }
    Literal literal = (Literal) o;
    return type == literal.type && Objects.equals(value, literal.value);
  }

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

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

  /**
   * Returns a {@code Literal} object for Decimal.
   *
   * @param value a decimal number as a string representation
   * @return a {@code Literal} object
   */
  public static Literal ofDecimal(String value) {
    return new Literal(Type.DECIMAL, value);
  }

  /**
   * Returns a {@code Literal} object for Float.
   *
   * @param value a float number as a string representation
   * @return a {@code Literal} object
   */
  public static Literal ofFloat(String value) {
    return new Literal(Type.FLOAT, value);
  }

  /**
   * Returns a {@code Literal} object for String.
   *
   * @param value a string value
   * @return a {@code Literal} object
   */
  public static Literal ofString(String value) {
    return new Literal(Type.STRING, value);
  }

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

  public enum Type {
    BOOLEAN,
    DECIMAL,
    FLOAT,
    STRING,
    NULL
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy