com.scalar.db.sql.JoinPredicate Maven / Gradle / Ivy
package com.scalar.db.sql;
import com.google.common.base.MoreObjects;
import java.util.Objects;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
@Immutable
public class JoinPredicate {
public final ColumnRef leftColumn;
public final ColumnRef rightColumn;
private JoinPredicate(ColumnRef leftColumn, ColumnRef rightColumn) {
this.leftColumn = Objects.requireNonNull(leftColumn);
this.rightColumn = Objects.requireNonNull(rightColumn);
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof JoinPredicate)) {
return false;
}
JoinPredicate that = (JoinPredicate) o;
return Objects.equals(leftColumn, that.leftColumn)
&& Objects.equals(rightColumn, that.rightColumn);
}
@Override
public int hashCode() {
return Objects.hash(leftColumn, rightColumn);
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("leftColumn", leftColumn)
.add("rightColumn", rightColumn)
.toString();
}
/**
* Returns a builder object for a join predicate 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 a join predicate 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 a join predicate 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 a join predicate 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 leftColumn;
private Builder(ColumnRef leftColumn) {
this.leftColumn = leftColumn;
}
/**
* Builds an "equal to" join predicate with the specified column.
*
* @param columnName the name of the target column
* @return a {code JoinPredicate} object
*/
public JoinPredicate isEqualTo(String columnName) {
return new JoinPredicate(leftColumn, ColumnRef.of(columnName));
}
/**
* Builds an "equal to" join predicate with the specified column.
*
* @param tableName the name of the table for target column
* @param columnName the name of the target column
* @return a {code JoinPredicate} object
*/
public JoinPredicate isEqualTo(@Nullable String tableName, String columnName) {
if (tableName == null) {
return isEqualTo(columnName);
}
return new JoinPredicate(leftColumn, ColumnRef.of(TableRef.of(tableName), columnName));
}
/**
* Builds an "equal to" join predicate with 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 {code JoinPredicate} object
*/
public JoinPredicate isEqualTo(
@Nullable String namespaceName, @Nullable String tableName, String columnName) {
if (namespaceName == null) {
return isEqualTo(tableName, columnName);
}
return new JoinPredicate(
leftColumn, ColumnRef.of(TableRef.of(namespaceName, tableName), columnName));
}
/**
* Builds an "equal to" join predicate with the specified column.
*
* @param column the target column
* @return a {code JoinPredicate} object
*/
public JoinPredicate isEqualTo(ColumnRef column) {
return new JoinPredicate(leftColumn, column);
}
}
}