org.infinispan.commands.functional.Mutations Maven / Gradle / Ivy
package org.infinispan.commands.functional;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectOutput;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Function;
import org.infinispan.functional.EntryView;
/**
* Helper class for marshalling, also hiding implementations of {@link Mutation} from the interface.
*/
final class Mutations {
private Mutations() {}
// No need to occupy externalizer ids when we have a limited set of options
static void writeTo(ObjectOutput output, Mutation mutation) throws IOException {
byte type = mutation.type();
output.writeByte(type);
switch (type) {
case ReadWrite.TYPE:
output.writeObject(((ReadWrite) mutation).f);
break;
case ReadWriteWithValue.TYPE:
output.writeObject(((ReadWriteWithValue) mutation).value);
output.writeObject(((ReadWriteWithValue) mutation).f);
break;
case Write.TYPE:
output.writeObject(((Write) mutation).f);
break;
case WriteWithValue.TYPE:
output.writeObject(((WriteWithValue) mutation).value);
output.writeObject(((WriteWithValue) mutation).f);
break;
}
}
static Mutation readFrom(ObjectInput input) throws IOException, ClassNotFoundException {
switch (input.readByte()) {
case ReadWrite.TYPE:
return new ReadWrite<>((Function, ?>) input.readObject());
case ReadWriteWithValue.TYPE:
return new ReadWriteWithValue<>((V) input.readObject(), (BiFunction, ?>) input.readObject());
case Write.TYPE:
return new Write((Consumer>) input.readObject());
case WriteWithValue.TYPE:
return new WriteWithValue((V) input.readObject(), (BiConsumer>) input.readObject());
default:
throw new IllegalStateException("Unknown type of mutation, broken input?");
}
}
static class ReadWrite implements Mutation {
static final byte TYPE = 0;
private final Function, R> f;
public ReadWrite(Function, R> f) {
this.f = f;
}
@Override
public byte type() {
return TYPE;
}
@Override
public R apply(EntryView.ReadWriteEntryView view) {
return f.apply(view);
}
}
static class ReadWriteWithValue implements Mutation {
static final byte TYPE = 1;
private final V value;
private final BiFunction, R> f;
public ReadWriteWithValue(V value, BiFunction, R> f) {
this.value = value;
this.f = f;
}
@Override
public byte type() {
return TYPE;
}
@Override
public R apply(EntryView.ReadWriteEntryView view) {
return f.apply(value, view);
}
}
static class Write implements Mutation {
static final byte TYPE = 2;
private final Consumer> f;
public Write(Consumer> f) {
this.f = f;
}
@Override
public byte type() {
return TYPE;
}
@Override
public Void apply(EntryView.ReadWriteEntryView view) {
f.accept(view);
return null;
}
}
static class WriteWithValue implements Mutation {
static final byte TYPE = 3;
private final V value;
private final BiConsumer> f;
public WriteWithValue(V value, BiConsumer> f) {
this.value = value;
this.f = f;
}
@Override
public byte type() {
return TYPE;
}
@Override
public Void apply(EntryView.ReadWriteEntryView view) {
f.accept(value, view);
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy