com.adgear.anoa.write.ThriftEncoders Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of anoa-core Show documentation
Show all versions of anoa-core Show documentation
Core classes for Anoa library, which aims to be a safe, convenient and fast record
de/serialization wrapper for the Avro, Thrift and Jackson libraries, using the functional idioms
of Java 8.
The anoa-core module tries to keep upstream dependencies to a minimum.
package com.adgear.anoa.write;
import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.JsonGenerator;
import org.apache.thrift.TBase;
import org.apache.thrift.TException;
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.TTransport;
import java.util.function.Function;
import java.util.function.Supplier;
/**
* Utility class for generating functions for serializing Thrift records. Unless specified
* otherwise, the functions should not be deemed thread-safe.
*/
public class ThriftEncoders {
protected ThriftEncoders() {
}
/**
* @param Thrift record type
* @return A function for serializing Thrift records as compact binary blobs.
*/
static public /*@NonNull*/ Function compact() {
return fn(TCompactProtocol::new);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param Thrift record type
* @param Metadata type
* @return A function for serializing Thrift records as compact binary blobs.
*/
static public /*@NonNull*/ Function, Anoa> compact(
/*@NonNull*/ AnoaHandler anoaHandler) {
return fn(anoaHandler, TCompactProtocol::new);
}
/**
* @param Thrift record type
* @return A function for serializing Thrift records as compact binary blobs.
*/
static public /*@NonNull*/ Function binary() {
return fn(TBinaryProtocol::new);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param Thrift record type
* @param Metadata type
* @return A function for serializing Thrift records as standard binary blobs.
*/
static public /*@NonNull*/ Function, Anoa> binary(
/*@NonNull*/ AnoaHandler anoaHandler) {
return fn(anoaHandler, TBinaryProtocol::new);
}
/**
* @param Thrift record type
* @return A function for serializing Thrift records in Thrift JSON format.
*/
static public /*@NonNull*/ Function json() {
return fn(TJSONProtocol::new);
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param Thrift record type
* @param Metadata type
* @return A function for serializing Thrift records in Thrift JSON format.
*/
static public /*@NonNull*/ Function, Anoa> json(
/*@NonNull*/ AnoaHandler anoaHandler) {
return fn(anoaHandler, TJSONProtocol::new);
}
static /*@NonNull*/ Function fn(
/*@NonNull*/ Function protocolFactory) {
TMemoryOutputTransport tTransport = new TMemoryOutputTransport();
TProtocol tProtocol = protocolFactory.apply(tTransport);
return (T t) -> {
tTransport.baos.reset();
try {
t.write(tProtocol);
} catch (TException e) {
throw new RuntimeException(e);
}
return tTransport.baos.toByteArray();
};
}
static /*@NonNull*/ Function, Anoa> fn(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Function protocolFactory) {
TMemoryOutputTransport tTransport = new TMemoryOutputTransport();
TProtocol tProtocol = protocolFactory.apply(tTransport);
return anoaHandler.functionChecked((T record) -> {
tTransport.baos.reset();
record.write(tProtocol);
return tTransport.baos.toByteArray();
});
}
/**
* @param recordClass Thrift record class object
* @param supplier called for each new record serialization
* @param Thrift record type
* @param JsonGenerator type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it.
*/
static public
/*@NonNull*/ Function jackson(
/*@NonNull*/ Class recordClass,
/*@NonNull*/ Supplier supplier) {
AbstractWriter thriftWriter = new ThriftWriter<>(recordClass);
return (T record) -> {
G jg = supplier.get();
thriftWriter.write(record, jg);
return jg;
};
}
/**
* @param anoaHandler {@code AnoaHandler} instance to use for exception handling
* @param recordClass Thrift record class object
* @param supplier called for each new record serialization
* @param Thrift record type
* @param JsonGenerator type
* @param Metadata type
* @return A function which calls the supplier for a JsonGenerator object and writes the record
* into it.
*/
static public
/*@NonNull*/ Function, Anoa> jackson(
/*@NonNull*/ AnoaHandler anoaHandler,
/*@NonNull*/ Class recordClass,
/*@NonNull*/ Supplier supplier) {
AbstractWriter thriftWriter = new ThriftWriter<>(recordClass);
return anoaHandler.functionChecked((T record) -> {
G jg = supplier.get();
thriftWriter.writeChecked(record, jg);
return jg;
});
}
}