package de.eightbitboy.ecorealms.simulation;

import java.util.Set;
import java.util.stream.Collectors;

public class InventoryProcessor {

	public Set<Resource> getResources() {
		return resources.keySet().stream()
				.filter(resource -> resources.get(resource) != 0)
				.collect(Collectors.toSet());
	}

	public int get(Resource resource) {
		if (!resources.containsKey(resource)) {
			return 0;
		}
		return resources.get(resource);
	}

	public void add(Resource resource, int amount) {
		checkAmount(resource, amount);

		if (!resources.containsKey(resource)) {
			resources.put(resource, amount);
		} else {
			resources.put(resource, resources.get(resource) + amount);
		}
	}

	public int remove(Resource resource, int amount) {
		checkAmount(resource, amount);

		if (!resources.containsKey(resource)) {
			return 0;
		}

		int currentAmount = resources.get(resource);

		if (amount > currentAmount) {
			resources.put(resource, 0);
			return currentAmount;
		} else {
			resources.put(resource, currentAmount - amount);
			return amount;
		}
	}

	private void checkAmount(Resource resource, int amount) {
		if (amount < 0) {
			throw new RuntimeException("The resource amount is negative! "
					+ resource.name() + ": " + amount);
		}
	}
}

@@ -98,16 +98,92 @@
 ctors;%0A%0A
+//TODO Find an alternative to %22Processor%22. An inventory is not %22processed%22!%0A
 public c
@@ -244,16 +244,35 @@
 sources(
+Inventory inventory
 ) %7B%0A%09%09re
@@ -268,32 +268,42 @@
 ory) %7B%0A%09%09return 
+inventory.
 resources.keySet
@@ -338,16 +338,26 @@
 urce -%3E 
+inventory.
 resource
@@ -432,16 +432,37 @@
 int get(
+Inventory inventory, 
 Resource
@@ -473,32 +473,42 @@
 ource) %7B%0A%09%09if (!
+inventory.
 resources.contai
@@ -552,16 +552,26 @@
 %09return 
+inventory.
 resource
@@ -608,16 +608,37 @@
 oid add(
+Inventory inventory, 
 Resource
@@ -695,32 +695,42 @@
 mount);%0A%0A%09%09if (!
+inventory.
 resources.contai
@@ -747,24 +747,34 @@
 urce)) %7B%0A%09%09%09
+inventory.
 resources.pu
@@ -800,32 +800,42 @@
 ;%0A%09%09%7D else %7B%0A%09%09%09
+inventory.
 resources.put(re
@@ -842,16 +842,26 @@
 source, 
+inventory.
 resource
@@ -914,16 +914,37 @@
  remove(
+Inventory inventory, 
 Resource
@@ -1009,16 +1009,26 @@
 %0A%09%09if (!
+inventory.
 resource
@@ -1094,16 +1094,26 @@
 mount = 
+inventory.
 resource
@@ -1161,24 +1161,34 @@
 mount) %7B%0A%09%09%09
+inventory.
 resources.pu
@@ -1242,16 +1242,26 @@
 se %7B%0A%09%09%09
+inventory.
 resource

Migrate code