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

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

Go to download

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.

There is a newer version: 3.1.2
Show newest version
package com.adgear.anoa.write;

import com.fasterxml.jackson.core.FormatSchema;
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.TreeNode;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.io.Writer;
import java.util.Optional;

/**
 * Utility class for generating {@code WriteConsumer} instances to write Jackson
 * {@link com.fasterxml.jackson.core.TreeNode} instances.
 *
 * Intended to be used as a base class, subclasses should wrap appropriate Jackson databinding types
 * together; see the anoa-tools module for examples.
 *
 * @param  Record type
 * @param  Mapper type
 * @param  Factory type
 * @param  Schema type
 * @param  Generator type
 *
 */
public class JacksonConsumers<
    N extends TreeNode,
    C extends ObjectCodec,
    F extends JsonFactory,
    S extends FormatSchema,
    G extends JsonGenerator> {

  /**
   * The object mapper used by this instance
   */
  final public C objectCodec;

  /**
   * The factory used by this instance
   */
  final public F factory;

  /**
   * The (optional) format schema used by this instance
   */
  final public Optional schema;

  /**
   *
   * @param objectCodec Jackson object mapper instance
   * @param schema Jackson format schema (optional)
   */
  @SuppressWarnings("unchecked")
  public JacksonConsumers(/*@NonNull*/ C objectCodec, /*@NonNull*/ Optional schema) {
    this.objectCodec = objectCodec;
    this.factory = (F) objectCodec.getFactory();
    this.factory.setCodec(objectCodec);
    this.schema = schema;
  }

  @SuppressWarnings("unchecked")
  public /*@NonNull*/ G generator(/*@NonNull*/ Writer writer) {
    try {
      return with((G) factory.createGenerator(new BufferedWriter(writer)));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  @SuppressWarnings("unchecked")
  public /*@NonNull*/ G generator(/*@NonNull*/ OutputStream outputStream) {
    try {
      return with((G) factory.createGenerator(new BufferedOutputStream(outputStream),
                                              JsonEncoding.UTF8));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  @SuppressWarnings("unchecked")
  public /*@NonNull*/ G generator(/*@NonNull*/ File file) {
    try {
      return with((G) factory.createGenerator(file, JsonEncoding.UTF8));
    } catch (IOException e) {
      throw new UncheckedIOException(e);
    }
  }

  /**
   * @return the generator passed as argument, after setting the current format schema
   */
  public /*@NonNull*/ G with(/*@NonNull*/ G generator) {
    schema.ifPresent(generator::setSchema);
    return generator;
  }

  public /*@NonNull*/ WriteConsumer to(/*@NonNull*/ G generator) {
    return new TreeNodeWriteConsumer<>(generator);
  }

  public /*@NonNull*/ WriteConsumer to(/*@NonNull*/ Writer writer) {
    return to(generator(writer));
  }

  public /*@NonNull*/ WriteConsumer to(/*@NonNull*/ OutputStream outputStream) {
    return to(generator(outputStream));
  }

  public /*@NonNull*/ WriteConsumer to(/*@NonNull*/ File file) {
    return to(generator(file));
  }


}