package openmods.structured;

import java.util.*;

import com.google.common.base.Preconditions;
import com.google.common.collect.Maps;
import com.google.common.collect.TreeMultimap;

public abstract class StructuredData<C extends IStructureContainer<E>, E extends IStructureElement> {
	protected final SortedMap<Integer, E> elements = Maps.newTreeMap();
	protected final SortedMap<Integer, C> containers = Maps.newTreeMap();
	protected final TreeMultimap<Integer, Integer> containerToElement = TreeMultimap.create();
	protected final Map<Integer, Integer> elementToContainer = Maps.newHashMap();

	public boolean isEmpty() {
		return elements.isEmpty() && containers.isEmpty();
	}

	public void reset() {
		elements.clear();
		containers.clear();
		containerToElement.clear();
		elementToContainer.clear();
	}

	protected SortedSet<Integer> removeContainer(int containerId) {
		Preconditions.checkArgument(containerToElement.containsKey(containerId), "Container %s doesn't exists", containerId);
		SortedSet<Integer> removedElements = containerToElement.removeAll(containerId);

		for (Integer id : removedElements) {
			E element = elements.remove(id);
			elementToContainer.remove(element);
		}

		containers.remove(containerId);

		return removedElements;
	}

	protected int addContainer(int containerId, C container, int firstElementId) {
		final List<E> newElements = container.createElements();
		Preconditions.checkArgument(!newElements.isEmpty(), "New container %s has no elements", container);
		for (E element : newElements) {
			int elementId = firstElementId++;
			elements.put(elementId, element);
			containerToElement.put(containerId, elementId);
			elementToContainer.put(elementId, containerId);
			element.setId(elementId);

			container.onElementAdded(element);
		}
		containers.put(containerId, container);
		return firstElementId;
	}
}

@@ -1087,17 +1087,24 @@
 Integer 
-i
+elementI
 d : remo
@@ -1125,20 +1125,8 @@
 %0A%09%09%09
-E element = 
 elem
@@ -1137,17 +1137,24 @@
 .remove(
-i
+elementI
 d);%0A%09%09%09e
@@ -1181,24 +1181,26 @@
 move(element
+Id
 );%0A%09%09%7D%0A%0A%09%09co

Fix derp