Class CountableResource
- All Implemented Interfaces:
Serializable
,sim.util.Valuable
- Direct Known Subclasses:
Money
,UncountableResource
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
FieldsModifier and TypeFieldDescriptionstatic final double
This is the largest possible integer that can be held in a double without skipping integers -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
This is solely for the benefit of UncountableResource(UncountableResource)CountableResource
(String name) 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 TypeMethodDescriptionvoid
add
(CountableResource other) Adds the other CountableResource amount into this one.void
add
(CountableResource[] other) 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
clear()
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
equalTo
(CountableResource other) Does comparison by value, as opposed to equals(...) which does comparison by pointer.double
Returns the amount of the resource.boolean
greaterThan
(CountableResource other) 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 UncountableResourceboolean
Returns true if this is an instance of UncountableResourceboolean
lessThan
(CountableResource other) 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.toString()
Prints the resource out in a pleasing manner.
-
Field Details
-
MAXIMUM_INTEGER
public static final double MAXIMUM_INTEGERThis is the largest possible integer that can be held in a double without skipping integers- See Also:
-
-
Constructor Details
-
CountableResource
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
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
Returns a CountableResource of the same type, name, and amount as the provided CountableResource. This is essentially a clone of the CountableResource. -
CountableResource
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 interfacesim.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
Description copied from class:Resource
Prints the resource out in a pleasing manner. -
duplicate
Returns a CountableResource of the same type, name, and amount as the provided CountableResource. This is essentially a clone of the CountableResource. -
getAmount
public double getAmount()Description copied from class:Resource
Returns the amount of the resource. -
clear
public void clear()Sets the amount to zero. -
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
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
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
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
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
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
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
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
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
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
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
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.
-