com.adgear.anoa.read.ThriftDecoders Maven / Gradle / Ivy
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.
*/
final public class ThriftDecoders {
private 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 Function compact(
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
Function, Anoa> compact(
AnoaHandler anoaHandler,
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 Function binary(
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
Function, Anoa> binary(
AnoaHandler anoaHandler,
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 Function json(
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
Function, Anoa> json(
AnoaHandler anoaHandler,
Supplier supplier) {
return fn(anoaHandler, supplier, TJSONProtocol::new);
}
static Function fn(
Supplier supplier,
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 Function, Anoa> fn(
AnoaHandler anoaHandler,
Supplier supplier,
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 Jackson JsonParser type
* @param Thrift record type
* @return A function which reads a Thrift record from a JsonParser, in its 'natural' encoding.
*/
static public
Function
jackson(
Class recordClass) {
return new ThriftReader<>(recordClass).decoder();
}
/**
* @param recordClass Thrift record class object
* @param Jackson JsonParser type
* @param Thrift record type
* @return A function which reads a Thrift record from a JsonParser, in its 'natural' encoding,
* with stricter type checking.
*/
static public
Function
jacksonStrict(
Class recordClass) {
return new ThriftReader<>(recordClass).decoderStrict();
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Thrift record class object
* @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
Function, Anoa> jackson(
AnoaHandler anoaHandler,
Class recordClass) {
return new ThriftReader<>(recordClass).decoder(anoaHandler);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Thrift record class object
* @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,
* with stricter type checking.
*/
static public
Function, Anoa> jacksonStrict(
AnoaHandler anoaHandler,
Class recordClass) {
return new ThriftReader<>(recordClass).decoderStrict(anoaHandler);
}
}