Package sim.des

Class CountableResource

java.lang.Object
sim.des.Resource
sim.des.CountableResource
All Implemented Interfaces:
Serializable, sim.util.Valuable
Direct Known Subclasses:
Money, UncountableResource

public class CountableResource extends Resource implements sim.util.Valuable
A CountableResource is a Resource which can be merged with resources of the same type. CountabeResources have AMOUNTS, which must be integers >= 0. CountableResources are thus finitely divisible. You can also split a CountableResource into a group of smaller CountableResources, and can compare their amounts against each other.

Examples of CountableResources might be: cars, bricks, workers, and so on. Money is also a countableResource as it is not infinitely divisible: but we have a special subclass for Money so it prints out in a cute way.

These amounts are stored as doubles for two bad reasons: first, to allow UncountableResource to cleanly subclass from Resource, and second, because double has a larger range of integers than int does.

CountableResource has a subclass called UncountableResource, which represents infinitely divisible resources (like water or gasoline). This subclass arrangement may seem strange at first, but it makes sense given that UncountableResources can do everything CountableResources can do, plus some extra things.

See Also:
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final double
    This is the largest possible integer that can be held in a double without skipping integers
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    This is solely for the benefit of UncountableResource(UncountableResource)
     
    Returns a new kind of CountableResource with a given name, and an initial amount of 0.
     
    CountableResource(String name, double initialAmount)
    Returns a new kind of CountableResource with a given name, and initial amount.
     
    Returns a CountableResource of the same type, name, and amount as the provided CountableResource.
     
    CountableResource(CountableResource other, double amount)
    Returns a CountableResource of the same type, name, as the provided CountableResource, but a different amount.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Adds the other CountableResource amount into this one.
    void
    Adds the other CountableResource amounts into this one.
    void
    add(CountableResource other, double atMostThisMuch)
    Adds *at most* a certain amount of the other CountableResource amount into this one.
    void
    bound(double max)
    Bounds the resource to be no more than max and no less than 0.
    void
    bound(double min, double max)
    Bounds the resource to be no more than max and no less than min.
    void
    Sets the amount to zero.
    int
    Note: this class has a natural ordering that is inconsistent with equals, because equals(other) tests by pointer rather than value.
    boolean
    decrease(double val)
    Decreases the amount by the given value: if the value drops to beneath 0, nothing happens and FALSE is returned.
    boolean
    Decrements the amount by 1.0: if the value drops to beneath 0, it is set to 0 and FALSE is returned.
    double
     
    Returns a CountableResource of the same type, name, and amount as the provided CountableResource.
    boolean
    Does comparison by value, as opposed to equals(...) which does comparison by pointer.
    double
    Returns the amount of the resource.
    boolean
    Returns true if this CountableResource amount is greater than to the other.
    boolean
    Returns true if this CountableResource amount is greater than or equal to the other.
    boolean
    increase(double val)
    Increases the amount by the given value: if the value exceeds MAXIMUM_INTEGER, nothing happens and FALSE is returned.
    boolean
    Increments the amount by 1.0.
    boolean
    Returns true if this is NOT an instance of UncountableResource
    boolean
    Returns true if this is an instance of UncountableResource
    boolean
    Returns true if this CountableResource amount is less than to the other.
    boolean
    Returns true if this CountableResource amount is less than or equal to the other.
    reduce(double byExactly)
    Subtracts an exact amount from this resource and puts it in a new CountableResource, returning that.
    reduce(double atLeast, double atMost)
    Subtracts at least a certain amount and at most a certain amount from this resource and puts it in a new Resource, returning that.
    void
    setAmount(double val)
    Sets the amount.
    Prints the resource out in a pleasing manner.

    Methods inherited from class sim.des.Resource

    duplicate, equals, getName, getType, hashCode, isSameType, setName

    Methods inherited from class java.lang.Object

    clone, finalize, getClass, notify, notifyAll, wait, wait, wait
  • Field Details

    • MAXIMUM_INTEGER

      public static final double MAXIMUM_INTEGER
      This is the largest possible integer that can be held in a double without skipping integers
      See Also:
  • Constructor Details

    • CountableResource

      public CountableResource(String name, double initialAmount)
      Returns a new kind of CountableResource with a given name, and initial amount. The name is informal: It's legal for two different kinds of countable resources to have the same name. CountableResource types are distinguishe internally using unique integers.
    • CountableResource

      public CountableResource(String name)
      Returns a new kind of CountableResource with a given name, and an initial amount of 0. The name is informal: It's legal for two different kinds of countable resources to have the same name. CountableResource types are distinguishe internally using unique integers.
    • CountableResource

      protected CountableResource()
      This is solely for the benefit of UncountableResource(UncountableResource)
    • CountableResource

      public CountableResource(CountableResource other)
      Returns a CountableResource of the same type, name, and amount as the provided CountableResource. This is essentially a clone of the CountableResource.
    • CountableResource

      public CountableResource(CountableResource other, double amount)
      Returns a CountableResource of the same type, name, as the provided CountableResource, but a different amount.
  • Method Details

    • doubleValue

      public double doubleValue()
      Specified by:
      doubleValue in interface sim.util.Valuable
    • isUncountable

      public boolean isUncountable()
      Returns true if this is an instance of UncountableResource
    • isCountable

      public boolean isCountable()
      Returns true if this is NOT an instance of UncountableResource
    • toString

      public String toString()
      Description copied from class: Resource
      Prints the resource out in a pleasing manner.
      Specified by:
      toString in class Resource
    • duplicate

      public Resource duplicate()
      Returns a CountableResource of the same type, name, and amount as the provided CountableResource. This is essentially a clone of the CountableResource.
      Specified by:
      duplicate in class Resource
    • getAmount

      public double getAmount()
      Description copied from class: Resource
      Returns the amount of the resource.
      Specified by:
      getAmount in class Resource
    • clear

      public void clear()
      Sets the amount to zero.
      Specified by:
      clear in class Resource
    • setAmount

      public void setAmount(double val)
      Sets the amount.
    • increment

      public boolean increment()
      Increments the amount by 1.0.
    • decrement

      public boolean decrement()
      Decrements the amount by 1.0: if the value drops to beneath 0, it is set to 0 and FALSE is returned. Else TRUE is returned.
    • bound

      public void bound(double min, double max)
      Bounds the resource to be no more than max and no less than min. It must be the case that max >= min >= 0.
    • bound

      public void bound(double max)
      Bounds the resource to be no more than max and no less than 0.
    • increase

      public boolean increase(double val)
      Increases the amount by the given value: if the value exceeds MAXIMUM_INTEGER, nothing happens and FALSE is returned. Else TRUE is returned.
    • decrease

      public boolean decrease(double val)
      Decreases the amount by the given value: if the value drops to beneath 0, nothing happens and FALSE is returned. Else TRUE is returned.
    • reduce

      public CountableResource reduce(double byExactly)
      Subtracts an exact amount from this resource and puts it in a new CountableResource, returning that. If we cannot do this, then null is returned instead.
    • reduce

      public CountableResource reduce(double atLeast, double atMost)
      Subtracts at least a certain amount and at most a certain amount from this resource and puts it in a new Resource, returning that. If we cannot do this, then null is returned instead.
    • add

      public void add(CountableResource other)
      Adds the other CountableResource amount into this one. The other CountableResource amount is set to 0. A NullPointerException is thrown if the other is null. A RuntimeException is thrown if the other is a CountableResource of a different type.
    • add

      public void add(CountableResource other, double atMostThisMuch)
      Adds *at most* a certain amount of the other CountableResource amount into this one. The other CountableResource amount is reduced by that amount. A NullPointerException is thrown if the other is null. A RuntimeException is thrown if the other is a CountableResource of a different type.
    • add

      public void add(CountableResource[] other)
      Adds the other CountableResource amounts into this one. The other CountableResource amounts are set to 0. A NullPointerException is thrown if any of the other CountableResources, or the array, is null. A RuntimeException is thrown if any of the other CountableResources are of a different type.
    • equalTo

      public boolean equalTo(CountableResource other)
      Does comparison by value, as opposed to equals(...) which does comparison by pointer. Returns true if this CountableResource amount is greater than to the other. A NullPointerException is thrown if the other is null. A RuntimeException is thrown if the other is a CountableResource of a different type.
    • greaterThan

      public boolean greaterThan(CountableResource other)
      Returns true if this CountableResource amount is greater than to the other. A NullPointerException is thrown if the other is null. A RuntimeException is thrown if the other is a CountableResource of a different type.
    • greaterThanOrEqualTo

      public boolean greaterThanOrEqualTo(CountableResource other)
      Returns true if this CountableResource amount is greater than or equal to the other. A NullPointerException is thrown if the other is null. A RuntimeException is thrown if the other is a CountableResource of a different type.
    • lessThan

      public boolean lessThan(CountableResource other)
      Returns true if this CountableResource amount is less than to the other. A NullPointerException is thrown if the other is null. A RuntimeException is thrown if the other is a CountableResource of a different type.
    • lessThanOrEqualTo

      public boolean lessThanOrEqualTo(CountableResource other)
      Returns true if this CountableResource amount is less than or equal to the other. A NullPointerException is thrown if the other is null. A RuntimeException is thrown if the other is a CountableResource of a different type.
    • compareTo

      public int compareTo(Object other)
      Note: this class has a natural ordering that is inconsistent with equals, because equals(other) tests by pointer rather than value. This is not a contract requirement of compareTo but it is unusual.