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

com.regnosys.rosetta.translate.CustomSerialise Maven / Gradle / Ivy

There is a newer version: 11.25.1
Show newest version
package com.regnosys.rosetta.translate;

import java.io.OutputStream;
import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.BiConsumer;

import com.google.common.collect.Multimap;

public class CustomSerialise {
	
	protected Map, BiConsumer> serialiseMethods = new HashMap<>();
	
	public void serialiseObject(Object o, OutputStream out) {
		SerialiseContext context = new SerialiseContext(new PrintStream(out, false, StandardCharsets.UTF_8));
		writeObjOrRef(o, context);
	}

	public  void serialiseList(List col, OutputStream out) {
		SerialiseContext context = new SerialiseContext(new PrintStream(out, false, StandardCharsets.UTF_8));
		context.incrementIndent();
		writeCollection("", col, context);
		context.decrementIndent();
	}
	
	@SuppressWarnings("unchecked")
	protected  void serialiseObject(T o, SerialiseContext context) {
		BiConsumer serialiseMethod = (BiConsumer) serialiseMethods.get(o.getClass());
		if (serialiseMethod==null) {
			throw new UnsupportedOperationException("Don't know how to serialise " + o.getClass().getName());
		}
		serialiseMethod.accept(o, context);
	}
	
	protected void write(String label, Object o, SerialiseContext context) {
		print(label, context);
		writeObjOrRef(o, context);
	}
	protected void writeObjOrRef(Object o, SerialiseContext context) {
		context.incrementIndent();
		if (o==null) {
			print("null", context);
		}
		else {
			Long existingId = context.objectIds.get(o);
			if (existingId != null) {
				print("=", existingId, context);
			} else {
				long newId = context.currentId++;
				context.objectIds.put(o, newId);
				print("#",newId,context);
				String className = o.getClass().getName();
				print("class:", className, context);
				serialiseObject(o,context);
			}
		}
		context.decrementIndent();
	}
	
	protected  void writeCollection(String label, Collection col, SerialiseContext context) {
		print(label, "[", context);
		for (T t:col) {
			writeObjOrRef(t, context);
		}
		print("]", context);
	}
	
	protected  void writeMap(String label, Map map, SerialiseContext context) {
		print(label, "[", context);
		for (Map.Entry entry:map.entrySet()) {
			writeObjOrRef(entry.getKey(), context);
			print("->", context);
			writeObjOrRef(entry.getValue(), context);
		}
		print("]", context);
	}
	
	protected  void writeMultimap(String label, Multimap map, SerialiseContext context) {
		print(label, "[", context);
		for (Map.Entry entry:map.entries()) {
			writeObjOrRef(entry.getKey(), context);
			print("->", context);
			writeObjOrRef(entry.getValue(), context);
		}
		print("]", context);
	}

	protected void print(String label, SerialiseContext context) {
		indent(context);
		context.pout.println(label);
	}
	protected void print(String label, String value, SerialiseContext context) {
		indent(context);
		context.pout.print(label);
		context.pout.println(value);
	}
	protected void print(String label, Long value, SerialiseContext context) {
		indent(context);
		context.pout.print(label);
		context.pout.println(value);
	}
	
	protected void print(String label, int value, SerialiseContext context) {
		indent(context);
		context.pout.print(label);
		context.pout.println(value);
	}
	protected void print(String label,  boolean value, SerialiseContext context) {
		indent(context);
		context.pout.print(label);
		context.pout.println(value);
	}
	private void indent(SerialiseContext context) {
		if (context.indent<0) throw new RuntimeException("Error serializing objects indent became negative");
		context.pout.write(context.tabs,0,context.indent);
	}

	protected class SerialiseContext {
		private int indent=-1;//gets incremented before the first object
		byte[] tabs = new byte[100];
		private PrintStream pout;
		
		private final IdentityHashMap objectIds = new IdentityHashMap<>();
		private long currentId = 0;
		
		public SerialiseContext(PrintStream pout) {
			this.pout = pout;
			Arrays.fill(tabs, (byte)'\t');
		}
		
		public void incrementIndent() {
			++indent;
			if (indent>tabs.length-1) {
				tabs = new byte[indent*2];
				Arrays.fill(tabs, (byte)'\t');
			}
		}
		
		public void decrementIndent() {
			--indent;
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy