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 void allocateShip(Ship ship) {
if (allocatedShips.contains(ship))
throw new RuntimeException("already allocated!");
+ if (parent != null)
+ parent.allocateShip(ship);
allocatedShips.add(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);
+ }
}