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