2022-05-20 10:20:00 +02:00
|
|
|
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;
|
2022-05-20 10:20:00 +02:00
|
|
|
import java.util.function.Supplier;
|
|
|
|
|
2022-06-30 15:06:10 +02:00
|
|
|
public abstract class BufSupplier implements SafeCloseable, DiscardingCloseable, Supplier<Buffer> {
|
2022-05-20 10:20:00 +02:00
|
|
|
|
|
|
|
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) {
|
2022-07-03 01:32:13 +02:00
|
|
|
return new SimpleBufSupplier(() -> supplier.copy());
|
2022-05-20 10:20:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|