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;
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;
if (ancestor != null) ancestor.releaseShip(ship);
}
+ public void releaseAll() {
+ HashSet<Ship> toRelease = new HashSet<Ship>();
+ toRelease.addAll(allocatedShips);
+ for (Ship ship : toRelease)
+ releaseShip(ship);
+ }
}