All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.adgear.anoa.write.ThriftEncoders Maven / Gradle / Ivy

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.
 */
final public class ThriftEncoders {

  private ThriftEncoders() {
  }

  /**
   * @param  Thrift record type
   * @return A function for serializing Thrift records as compact binary blobs.
   */
  static public  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  Function, Anoa> compact(
      AnoaHandler anoaHandler) {
    return fn(anoaHandler, TCompactProtocol::new);
  }

  /**
   * @param  Thrift record type
   * @return A function for serializing Thrift records as compact binary blobs.
   */
  static public  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  Function, Anoa> binary(
      AnoaHandler anoaHandler) {
    return fn(anoaHandler, TBinaryProtocol::new);
  }

  /**
   * @param  Thrift record type
   * @return A function for serializing Thrift records in Thrift JSON format.
   */
  static public  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  Function, Anoa> json(
      AnoaHandler anoaHandler) {
    return fn(anoaHandler, TJSONProtocol::new);
  }

  static  Function fn(
      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  Function, Anoa> fn(
      AnoaHandler anoaHandler,
      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, in compact form.
   */
  static public  Function jackson(
      Class recordClass,
      Supplier supplier) {
    return new ThriftWriter<>(recordClass).encoder(supplier);
  }

  /**
   * @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, in strict form.
   */
  static public  Function jacksonStrict(
      Class recordClass,
      Supplier supplier) {
    return new ThriftWriter<>(recordClass).encoderStrict(supplier);
  }

  /**
   * @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, in compact form.
   */
  static public 
  Function, Anoa> jackson(
      AnoaHandler anoaHandler,
      Class recordClass,
      Supplier supplier) {
    return new ThriftWriter<>(recordClass).encoder(anoaHandler, supplier);
  }

  /**
   * @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, in strict form.
   */
  static public 
  Function, Anoa> jacksonStrict(
      AnoaHandler anoaHandler,
      Class recordClass,
      Supplier supplier) {
    return new ThriftWriter<>(recordClass).encoderStrict(anoaHandler, supplier);
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy