package com.datastax.oss.driver.internal.core.metadata.schema.events;
import com.datastax.oss.driver.api.core.metadata.schema.TableMetadata;
import com.datastax.oss.driver.internal.core.metadata.schema.SchemaChangeType;
import java.util.Objects;
import net.jcip.annotations.Immutable;
@Immutable
public class TableChangeEvent {
public static TableChangeEvent dropped(TableMetadata oldTable) {
return new TableChangeEvent(SchemaChangeType.DROPPED, oldTable, null);
}
public static TableChangeEvent created(TableMetadata newTable) {
return new TableChangeEvent(SchemaChangeType.CREATED, null, newTable);
}
public static TableChangeEvent updated(TableMetadata oldTable, TableMetadata newTable) {
return new TableChangeEvent(SchemaChangeType.UPDATED, oldTable, newTable);
}
public final SchemaChangeType changeType;
public final TableMetadata oldTable;
public final TableMetadata newTable;
private TableChangeEvent(
SchemaChangeType changeType, TableMetadata oldTable, TableMetadata newTable) {
this.changeType = changeType;
this.oldTable = oldTable;
this.newTable = newTable;
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
} else if (other instanceof TableChangeEvent) {
TableChangeEvent that = (TableChangeEvent) other;
return this.changeType == that.changeType
&& Objects.equals(this.oldTable, that.oldTable)
&& Objects.equals(this.newTable, that.newTable);
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(changeType, oldTable, newTable);
}
@Override
public String toString() {
switch (changeType) {
case CREATED:
return String.format("TableChangeEvent(CREATED %s)", newTable.getName());
case UPDATED:
return String.format(
"TableChangeEvent(UPDATED %s=>%s)", oldTable.getName(), newTable.getName());
case DROPPED:
return String.format("TableChangeEvent(DROPPED %s)", oldTable.getName());
default:
throw new IllegalStateException("Unsupported change type " + changeType);
}
}
}