com.adgear.anoa.read.ThriftDecoders Maven / Gradle / Ivy
Show all versions of anoa-core Show documentation
package com.adgear.anoa.read;
import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.JsonParser;
import org.apache.thrift.TBase;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TCompactProtocol;
import org.apache.thrift.protocol.TJSONProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TMemoryInputTransport;
import org.apache.thrift.transport.TTransport;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Utility class for generating functions for deserializing Thrift records. Unless specified
* otherwise, the functions should not be deemed thread-safe.
*/
public class ThriftDecoders {
protected ThriftDecoders() {
}
/**
* @param supplier provides the returned Thrift record instances
* @param Thrift record type
* @return A function which deserializes a Thrift record from its compact binary encoding
*/
static public /*@NonNull*/ Function compact(
/*@NonNull*/ Supplier supplier) {
return fn(supplier, TCompactProtocol::new);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param supplier provides the returned Thrift record instances
* @param Thrift record type
* @param Metadata type
* @return A function which deserializes a Thrift record from its compact binary encoding
*/
static public
/*@NonNull*/ Function, Anoa> compact(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Supplier supplier) {
return fn(anoaHandler, supplier, TCompactProtocol::new);
}
/**
* @param supplier provides the returned Thrift record instances
* @param Thrift record type
* @return A function which deserializes a Thrift record from its standard binary encoding
*/
static public /*@NonNull*/ Function binary(
/*@NonNull*/ Supplier supplier) {
return fn(supplier, TBinaryProtocol::new);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param supplier provides the returned Thrift record instances
* @param Thrift record type
* @param Metadata type
* @return A function which deserializes a Thrift record from its standard binary encoding
*/
static public
/*@NonNull*/ Function, Anoa> binary(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Supplier supplier) {
return fn(anoaHandler, supplier, TBinaryProtocol::new);
}
/**
* @param supplier provides the returned Thrift record instances
* @param Thrift record type
* @return A function which deserializes a Thrift record from its Thrift JSON encoding
*/
static public /*@NonNull*/ Function json(
/*@NonNull*/ Supplier supplier) {
return fn(supplier, TJSONProtocol::new);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param supplier provides the returned Thrift record instances
* @param Thrift record type
* @param Metadata type
* @return A function which deserializes a Thrift record from its Thrift JSON encoding
*/
static public
/*@NonNull*/ Function, Anoa> json(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Supplier supplier) {
return fn(anoaHandler, supplier, TJSONProtocol::new);
}
static /*@NonNull*/ Function fn(
/*@NonNull*/ Supplier supplier,
/*@NonNull*/ Function protocolFactory) {
final TMemoryInputTransport tTransport = new TMemoryInputTransport();
final TProtocol tProtocol = protocolFactory.apply(tTransport);
final LookAheadIterator lookAheadIterator =
LookAheadIteratorFactory.thrift(tProtocol, supplier);
return (byte[] bytes) -> {
lookAheadIterator.reset(null);
tTransport.reset(bytes);
return lookAheadIterator.next();
};
}
static /*@NonNull*/ Function, Anoa> fn(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Supplier supplier,
/*@NonNull*/ Function protocolFactory) {
final TMemoryInputTransport tTransport = new TMemoryInputTransport();
final TProtocol tProtocol = protocolFactory.apply(tTransport);
final LookAheadIterator> lookAheadIterator =
LookAheadIteratorFactory.thrift(anoaHandler, tProtocol, supplier);
return (Anoa bytesWrapped) -> bytesWrapped.flatMap(bytes -> {
lookAheadIterator.reset(null);
tTransport.reset(bytesWrapped.get());
return lookAheadIterator.next();
});
}
/**
* @param recordClass Thrift record class object
* @param strict enable strict type checking
* @param Jackson JsonParser type
* @param Thrift record type
* @return A function which reads a Thrift record from a JsonParser, in its 'natural' encoding.
*/
static public
/*@NonNull*/ Function
jackson(
/*@NonNull*/ Class recordClass,
boolean strict) {
final AbstractReader reader = new ThriftReader<>(recordClass);
return (P jp) -> reader.read(jp, strict);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Thrift record class object
* @param strict enable strict type checking
* @param Jackson JsonParser type
* @param Thrift record type
* @param Metadata type
* @return A function which reads a Thrift record from a JsonParser, in its 'natural' encoding.
*/
static public
/*@NonNull*/ Function, Anoa> jackson(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Class recordClass,
boolean strict) {
final AbstractReader reader = new ThriftReader<>(recordClass);
return anoaHandler.functionChecked((P jp) -> (T) reader.readChecked(jp, strict));
}
}