com.scalar.db.sql.springdata.twopc.LocalPrepareCommitPhaseOperations Maven / Gradle / Ivy
package com.scalar.db.sql.springdata.twopc;
import static com.google.common.base.Preconditions.checkNotNull;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
class LocalPrepareCommitPhaseOperations {
private final Operation prepare;
@Nullable private final Operation validate;
private final Operation commit;
private final Operation rollback;
@FunctionalInterface
interface Operation {
void execute();
}
private LocalPrepareCommitPhaseOperations(
@Nonnull Operation prepare,
@Nullable Operation validate,
@Nonnull Operation commit,
@Nonnull Operation rollback) {
this.prepare = checkNotNull(prepare);
this.validate = validate;
this.commit = checkNotNull(commit);
this.rollback = checkNotNull(rollback);
}
@Nonnull
static LocalPrepareCommitPhaseOperations createSerializable(
@Nonnull Operation prepare,
@Nonnull Operation validate,
@Nonnull Operation commit,
@Nonnull Operation rollback) {
return new LocalPrepareCommitPhaseOperations(prepare, validate, commit, rollback);
}
@Nonnull
static LocalPrepareCommitPhaseOperations createSnapshot(
@Nonnull Operation prepare, @Nonnull Operation commit, @Nonnull Operation rollback) {
return new LocalPrepareCommitPhaseOperations(prepare, null, commit, rollback);
}
void prepare() {
prepare.execute();
}
void validate() {
if (validate != null) {
validate.execute();
}
}
void commit() {
commit.execute();
}
void rollback() {
rollback.execute();
}
}