package org.simantics.databoard.serialization.impl; import gnu.trove.map.hash.TObjectIntHashMap; import java.io.DataInput; import java.io.DataOutput; import java.io.IOException; import java.util.List; import org.simantics.databoard.binding.RecordBinding; import org.simantics.databoard.binding.error.BindingException; import org.simantics.databoard.serialization.SerializationException; import org.simantics.databoard.serialization.Serializer; import org.simantics.databoard.serialization.Serializer.RecursiveSerializer; import org.simantics.databoard.type.RecordType; public class ReferableRecordSerializer extends RecursiveSerializer { RecordBinding binding; public Serializer[] componentSerializers; /** The combined size elements, or null */ Integer fixedSizeOfContent; int minContentSize; public ReferableRecordSerializer(RecordBinding binding, Serializer[] componentSerializers) { this.binding = binding; this.componentSerializers = componentSerializers; } @Override public void finalizeConstruction() { fixedSizeOfContent = null; for (Serializer componentSerializer : componentSerializers) { minContentSize += componentSerializer.getMinSize(); Integer componentFixedSize = componentSerializer.getConstantSize(); if (componentFixedSize==null) { fixedSizeOfContent = null; break; } fixedSizeOfContent = fixedSizeOfContent==null ? componentFixedSize : fixedSizeOfContent + componentFixedSize; } } boolean referable() { return ((RecordType)binding.type()).isReferable(); } @Override public Object deserialize(DataInput in, List identities) throws IOException { try { int id = in.readInt(); if(id > 0) return identities.get(id-1); Object result = binding.createPartial(); identities.add(result); // assertRemainingBytes(in, minContentSize); Object[] temp = new Object[componentSerializers.length]; for(int i=0;i identities, Object obj) throws IOException { try { int id = in.readInt(); if(id > 0) { Object alreadyDeserializedObject = identities.get(id-1); if (alreadyDeserializedObject!=obj) { for (int i=0; i identities) throws IOException, SerializationException { int id = in.readInt(); if(id > 0) return; for(int i=0;i identities, Object obj) throws IOException { try { int id = identities.get(obj); if(id > 0) { out.writeInt(id); } else { out.writeInt(0); identities.put(obj, identities.size() + 1); for(int i=0;i identities) throws IOException { try { int id = identities.get(obj); if (id>0) return 4; identities.put(obj, identities.size()+1); if (fixedSizeOfContent!=null) return 4+fixedSizeOfContent; int result = 4; for(int i=0;i