package de.peeeq.wurstio.objectreader;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ObjectTable {

    private Map<Integer, ObjectDefinition> objectDefinitions = new HashMap<>();
    private ObjectFileType fileType;

    public ObjectTable(ObjectFileType fileType2) {
        this.fileType = fileType2;
    }

    public void add(ObjectDefinition objDef) {
        objectDefinitions.put(objDef.getNewObjectId(), objDef);
    }

    static ObjectTable readFromStream(BinaryDataInputStream in, ObjectFileType fileType) throws IOException {
        ObjectTable objectTable = new ObjectTable(fileType);


        int numberOfObjects = in.readInt();

        for (int i = 0; i < numberOfObjects; i++) {
            ObjectDefinition objDef = ObjectDefinition.readFromStream(in, objectTable);
            objectTable.add(objDef);
        }

        return objectTable;
    }


    public Map<Integer, ObjectDefinition>  getObjectDefinitions() {
        return objectDefinitions;
    }

    public void writeToStream(BinaryDataOutputStream out, ObjectFileType fileType) throws IOException {

        // write number of objects
        out.writeInt(objectDefinitions.size());

        for (ObjectDefinition od : objectDefinitions.values()) {
            od.writeToStream(out, fileType);
        }

    }

    public void prettyPrint(StringBuilder sb) {
        for (ObjectDefinition od : objectDefinitions.values()) {
            od.prettyPrint(sb);
        }

    }

    public void exportToWurst(Appendable out) throws IOException {
        for (ObjectDefinition od : objectDefinitions.values()) {
            od.exportToWurst(out);
        }

    }

    public ObjectFileType getFileType() {
        return fileType;
    }

    public boolean isEmpty() {
        return objectDefinitions.isEmpty();
    }

}

@@ -162,16 +162,22 @@
 til.
+Linked
 HashMap;
 %0Aimp
@@ -176,53 +176,8 @@
 Map;
-%0Aimport java.util.List;%0Aimport java.util.Map;
 %0A%0Apu
@@ -214,16 +214,26 @@
 private 
+LinkedHash
 Map%3CInte
@@ -279,16 +279,22 @@
 s = new 
+Linked
 HashMap%3C
@@ -488,30 +488,91 @@
     
-objectDefinitions.put(
+int objId = objDef.getNewObjectId();%0A        if (objId == 0) %7B%0A            objId = 
 objD
@@ -569,35 +569,36 @@
 jId = objDef.get
-New
+Orig
 ObjectId(), objD
@@ -591,16 +591,63 @@
 jectId()
+;%0A        %7D%0A        objectDefinitions.put(objId
 , objDef
@@ -1109,16 +1109,26 @@
  public 
+LinkedHash
 Map%3CInte
@@ -1149,17 +1149,16 @@
 inition%3E
- 
  getObje

Use original object ID if new object ID is 0 (#985)