ShipPool.allocateShip() needs to invoke ancestor/parent allocateShip()
[fleet.git] / src / edu / berkeley / fleet / loops / ShipPool.java
index 673872a..82a5296 100644 (file)
@@ -16,6 +16,7 @@ public class ShipPool implements Iterable<Ship> {
     private HashSet<Ship> allocatedShips = new HashSet<Ship>();
 
     public ShipPool(Fleet fleet) { this(fleet, null); }
+    public ShipPool(ShipPool ancestor) { this(ancestor.fleet, ancestor); }
     public ShipPool(Fleet fleet, ShipPool ancestor) {
         this.fleet = fleet;
         this.ancestor = ancestor;
@@ -23,6 +24,14 @@ public class ShipPool implements Iterable<Ship> {
 
     public Iterator<Ship> iterator() { return allocatedShips.iterator(); }
 
+    public void allocateShip(Ship ship) {
+        if (allocatedShips.contains(ship))
+            throw new RuntimeException("already allocated!");
+        if (parent != null)
+            parent.allocateShip(ship);
+        allocatedShips.add(ship);
+    }
+
     /** allocate a ship */
     public Ship allocateShip(String type) {
         Ship ship = null;
@@ -48,4 +57,10 @@ public class ShipPool implements Iterable<Ship> {
         if (ancestor != null) ancestor.releaseShip(ship);
     }
 
+    public void releaseAll() {
+        HashSet<Ship> toRelease = new HashSet<Ship>();
+        toRelease.addAll(allocatedShips);
+        for (Ship ship : toRelease)
+            releaseShip(ship);
+    }
 }