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

com.adgear.anoa.read.JacksonStreams 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.read;

import com.adgear.anoa.Anoa;
import com.adgear.anoa.AnoaHandler;
import com.fasterxml.jackson.core.FormatSchema;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.ObjectCodec;
import com.fasterxml.jackson.core.TreeNode;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.UncheckedIOException;
import java.net.URL;
import java.util.Optional;
import java.util.stream.Stream;

/**
 * Utility class for deserializing Jackson {@link com.fasterxml.jackson.core.TreeNode} instances as
 * a {@link java.util.stream.Stream}.
 *
 * 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 

Parser type */ public class JacksonStreams< N extends TreeNode, C extends ObjectCodec, F extends JsonFactory, S extends FormatSchema, P extends JsonParser> { /** * 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 JacksonStreams(/*@NonNull*/ C objectCodec, /*@NonNull*/ Optional schema) { this.objectCodec = objectCodec; this.factory = (F) objectCodec.getFactory(); this.factory.setCodec(this.objectCodec); this.schema = schema; } @SuppressWarnings("unchecked") public /*@NonNull*/ P parser(/*@NonNull*/ InputStream inputStream) { try { return with((P) factory.createParser(new BufferedInputStream(inputStream))); } catch (IOException e) { throw new UncheckedIOException(e); } } @SuppressWarnings("unchecked") public /*@NonNull*/ P parser(/*@NonNull*/ Reader reader) { try { return with((P) factory.createParser(new BufferedReader(reader))); } catch (IOException e) { throw new UncheckedIOException(e); } } @SuppressWarnings("unchecked") public /*@NonNull*/ P parser(/*@NonNull*/ byte[] bytes) { try { return with((P) factory.createParser(bytes)); } catch (IOException e) { throw new UncheckedIOException(e); } } @SuppressWarnings("unchecked") public /*@NonNull*/ P parser(/*@NonNull*/ String string) { try { return with((P) factory.createParser(string)); } catch (IOException e) { throw new UncheckedIOException(e); } } @SuppressWarnings("unchecked") public /*@NonNull*/ P parser(/*@NonNull*/ File file) { try { return with((P) factory.createParser(file)); } catch (IOException e) { throw new UncheckedIOException(e); } } @SuppressWarnings("unchecked") public /*@NonNull*/ P parser(/*@NonNull*/ URL url) { try { return with((P) factory.createParser(url)); } catch (IOException e) { throw new UncheckedIOException(e); } } /** * @return the parser passed as argument, after setting the current object mapper and schema */ public /*@NonNull*/ P with(/*@NonNull*/ P parser) { parser.setCodec(objectCodec); schema.ifPresent(parser::setSchema); return parser; } public /*@NonNull*/ Stream from(/*@NonNull*/ P parser) { return LookAheadIteratorFactory.jackson(parser).asStream(); } public /*@NonNull*/ Stream from(/*@NonNull*/ InputStream inputStream) { return from(parser(inputStream)); } public /*@NonNull*/ Stream from(/*@NonNull*/ Reader reader) { return from(parser(reader)); } public /*@NonNull*/ Stream from(/*@NonNull*/ byte[] bytes) { return from(parser(bytes)); } public /*@NonNull*/ Stream from(/*@NonNull*/ String string) { return from(parser(string)); } public /*@NonNull*/ Stream from(/*@NonNull*/ File file) { return from(parser(file)); } public /*@NonNull*/ Stream from(/*@NonNull*/ URL url) { return from(parser(url)); } public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler, /*@NonNull*/ P parser) { return LookAheadIteratorFactory.jackson(anoaHandler, parser).asStream(); } public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler, /*@NonNull*/ InputStream inputStream) { return from(anoaHandler, parser(inputStream)); } public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler, /*@NonNull*/ Reader reader) { return from(anoaHandler, parser(reader)); } public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler, /*@NonNull*/ byte[] bytes) { return from(anoaHandler, parser(bytes)); } public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler, /*@NonNull*/ String string) { return from(anoaHandler, parser(string)); } public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler, /*@NonNull*/ File file) { return from(anoaHandler, parser(file)); } public /*@NonNull*/ Stream> from(/*@NonNull*/ AnoaHandler anoaHandler, /*@NonNull*/ URL url) { return from(anoaHandler, parser(url)); } }