CavalliumDBEngine/src/main/java/it/cavallium/dbengine/database/BufSupplier.java

63 lines
1.3 KiB
Java
Raw Normal View History

package it.cavallium.dbengine.database;
2022-10-02 03:09:50 +02:00
import io.netty5.buffer.Buffer;
2022-07-15 02:44:50 +02:00
import io.netty5.util.Send;
import java.util.function.Supplier;
2022-06-30 15:06:10 +02:00
public abstract class BufSupplier implements SafeCloseable, DiscardingCloseable, Supplier<Buffer> {
public static BufSupplier of(Supplier<Buffer> supplier) {
return new SimpleBufSupplier(supplier);
}
public static BufSupplier of(Send<Buffer> supplier) {
return new CopyBufSupplier(supplier.receive());
}
public static BufSupplier ofOwned(Buffer supplier) {
return new CopyBufSupplier(supplier);
}
public static BufSupplier ofShared(Buffer supplier) {
return new SimpleBufSupplier(() -> supplier.copy());
}
private static final class SimpleBufSupplier extends BufSupplier {
private final Supplier<Buffer> supplier;
public SimpleBufSupplier(Supplier<Buffer> supplier) {
this.supplier = supplier;
}
@Override
public Buffer get() {
return supplier.get();
}
@Override
public void close() {
}
}
private static final class CopyBufSupplier extends BufSupplier {
private final Buffer supplier;
public CopyBufSupplier(Buffer supplier) {
this.supplier = supplier;
}
@Override
public Buffer get() {
return supplier.copy();
}
@Override
public void close() {
supplier.close();
}
}
}