class AbstractExhaustibleResource implements ResourceType {

  @Override
  public Resource deduct(Resource total, Resource deduction) throws NoEnoughResourcesException {
    checkResource(total);
    checkResource(deduction);

    if (total.getAmount() == -1) {
      return total;
    }

    if (deduction.getAmount() == -1) {
      throw new NoEnoughResourcesException(total, deduction, deduction);
    }

    final long resultAmount = total.getAmount() - deduction.getAmount();
    if (resultAmount < 0) {
      throw new NoEnoughResourcesException(
          total, deduction, new ResourceImpl(getId(), -resultAmount, getDefaultUnit()));
    }
    return new ResourceImpl(getId(), resultAmount, getDefaultUnit());
  }

}