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

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

The 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;

/** Ordering that is used in an SELECT statement to order the results. */
@Immutable
public class Ordering {

  public final ColumnRef column;
  public final Order order;

  private Ordering(ColumnRef column, Order order) {
    this.column = Objects.requireNonNull(column);
    this.order = Objects.requireNonNull(order);
  }

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

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (!(o instanceof Ordering)) {
      return false;
    }
    Ordering that = (Ordering) o;
    return Objects.equals(column, that.column) && order == that.order;
  }

  @Override
  public int hashCode() {
    return Objects.hash(column, order);
  }

  /**
   * Returns a builder object for an ordering for the specified column.
   *
   * @param columnName the name of the target column
   * @return a builder object
   */
  public static Builder column(String columnName) {
    return new Builder(ColumnRef.of(columnName));
  }

  /**
   * Returns a builder object for an ordering for the specified column.
   *
   * @param tableName the name of the table for target column
   * @param columnName the name of the target column
   * @return a builder object
   */
  public static Builder column(@Nullable String tableName, String columnName) {
    if (tableName == null) {
      return column(columnName);
    }
    return new Builder(ColumnRef.of(TableRef.of(tableName), columnName));
  }

  /**
   * Returns a builder object for an ordering for the specified column.
   *
   * @param namespaceName the name of the namespace for target column
   * @param tableName the name of the table for target column
   * @param columnName the name of the target column
   * @return a builder object
   */
  public static Builder column(
      @Nullable String namespaceName, @Nullable String tableName, String columnName) {
    if (namespaceName == null) {
      return column(tableName, columnName);
    }
    return new Builder(ColumnRef.of(TableRef.of(namespaceName, tableName), columnName));
  }

  /**
   * Returns a builder object for an ordering for the specified column.
   *
   * @param column the target column
   * @return a builder object
   */
  public static Builder column(ColumnRef column) {
    return new Builder(column);
  }

  public static class Builder {
    private final ColumnRef column;

    public Builder(ColumnRef column) {
      this.column = column;
    }

    /**
     * Returns a {code Ordering} object for ascending order.
     *
     * @return a {code Ordering} object for ascending order
     */
    public Ordering asc() {
      return new Ordering(column, Order.ASC);
    }

    /**
     * Returns a {code Ordering} object for descending order.
     *
     * @return a {code Ordering} object for descending order
     */
    public Ordering desc() {
      return new Ordering(column, Order.DESC);
    }
  }

  public enum Order {
    ASC,
    DESC
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy