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

io.datakernel.codec.StructuredInput Maven / Gradle / Ivy

There is a newer version: 3.1.0
Show newest version
package io.datakernel.codec;

import io.datakernel.common.parse.ParseException;
import org.jetbrains.annotations.Nullable;

import java.lang.reflect.Type;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

/**
 * This is an abstraction that allows you to read data in uniform way
 * from different sources with different implementations of this interface
 */
public interface StructuredInput {
	void readNull() throws ParseException;

	boolean readBoolean() throws ParseException;

	byte readByte() throws ParseException;

	int readInt() throws ParseException;

	long readLong() throws ParseException;

	int readInt32() throws ParseException;

	long readLong64() throws ParseException;

	float readFloat() throws ParseException;

	double readDouble() throws ParseException;

	byte[] readBytes() throws ParseException;

	String readString() throws ParseException;

	@Nullable  T readNullable(StructuredDecoder decoder) throws ParseException;

	boolean hasNext() throws ParseException;

	String readKey() throws ParseException;

	default void readKey(String expectedName) throws ParseException {
		String actualName = readKey();
		if (!expectedName.equals(actualName)) {
			throw new ParseException("Expected field: " + expectedName + ", but was: " + actualName);
		}
	}

	default  T readKey(String expectedName, StructuredDecoder decoder) throws ParseException {
		readKey(expectedName);
		return decoder.decode(this);
	}

	 List readList(StructuredDecoder decoder) throws ParseException;

	 Map readMap(StructuredDecoder keyDecoder, StructuredDecoder valueDecoder) throws ParseException;

	 T readTuple(StructuredDecoder decoder) throws ParseException;

	 T readObject(StructuredDecoder decoder) throws ParseException;

	@FunctionalInterface
	interface ParserRunnable {
		void run() throws ParseException;
	}

	default void readTuple(ParserRunnable decoder) throws ParseException {
		readTuple(in -> {
			decoder.run();
			return null;
		});
	}

	default void readObject(ParserRunnable decoder) throws ParseException {
		readObject(in -> {
			decoder.run();
			return null;
		});
	}

	 T readCustom(Type type) throws ParseException;

	enum Token {
		NULL, BOOLEAN, BYTE, INT, LONG, FLOAT, DOUBLE, STRING, BYTES, LIST, MAP, TUPLE, OBJECT
	}

	EnumSet getNext() throws ParseException;
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy