soc.game
Class SOCResourceSet

java.lang.Object
  extended by soc.game.SOCResourceSet
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable

public class SOCResourceSet
extends java.lang.Object
implements java.io.Serializable, java.lang.Cloneable

This represents a collection of clay, ore, sheep, wheat, and wood resources. Unknown resources are also tracked here. Although it's possible to store negative amounts of resources, it's discouraged.

See Also:
SOCResourceConstants, SOCPlayingPiece.getResourcesToBuild(int), Serialized Form

Field Summary
static SOCResourceSet EMPTY_SET
          Resource set with zero of each resource type
private  int[] resources
          the number of each resource type.
 
Constructor Summary
SOCResourceSet()
          Make an empty resource set
SOCResourceSet(int[] rset)
          Make a resource set from an array
SOCResourceSet(int cl, int or, int sh, int wh, int wo, int uk)
          Make a resource set with stuff in it
 
Method Summary
 void add(int amt, int rtype)
          add an amount to a resource
 void add(SOCResourceSet rs)
          add an entire resource set's amounts into this set.
 void clear()
          set the number of resources to zero
 boolean contains(int rtype)
          Does the set contain any resources of this type?
 boolean contains(SOCResourceSet sub)
           
 void convertToUnknown()
          Convert all these resources to type SOCResourceConstants.UNKNOWN.
 SOCResourceSet copy()
          Make a copy of this resource set.
 boolean equals(java.lang.Object anObject)
           
 int getAmount(int rtype)
          How many resources of this type are contained in the set?
 int getKnownTotal()
          Get the total amount of resources of known types: SOCResourceConstants.CLAY to SOCResourceConstants.WOOD, excluding SOCResourceConstants.UNKNOWN or SOCResourceConstants.GOLD_LOCAL.
 int getKnownTypesCount()
          Get the number of known resource types contained in this set: SOCResourceConstants.CLAY to SOCResourceConstants.WOOD, excluding SOCResourceConstants.UNKNOWN or SOCResourceConstants.GOLD_LOCAL.
 int getTotal()
          Get the total number of resources in this set, including unknown types.
static boolean gte(SOCResourceSet a, SOCResourceSet b)
          Are set A's resources each greater than or equal to set B's?
 int hashCode()
           
static boolean lte(SOCResourceSet a, SOCResourceSet b)
          Are set A's resources each less than or equal to set B's?
 void setAmount(int amt, int rtype)
          Set the amount of a resource.
 void setAmounts(SOCResourceSet set)
          copy a resource set into this one.
 void subtract(int amt, int rtype)
          subtract an amount from a resource.
 void subtract(SOCResourceSet rs)
          subtract an entire resource set.
 java.lang.String toFriendlyString()
          Human-readable form of the set, with format "5 clay,1 ore,3 wood".
 boolean toFriendlyString(java.lang.StringBuffer sb)
          Human-readable form of the set, with format "5 clay, 1 ore, 3 wood".
 java.lang.String toShortString()
          Human-readable form of the set, with format "Resources: 5 1 0 0 3 0".
 java.lang.String toString()
          Human-readable form of the set, with format "clay=5|ore=1|sheep=0|wheat=0|wood=3|unknown=0"
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

EMPTY_SET

public static final SOCResourceSet EMPTY_SET
Resource set with zero of each resource type


resources

private int[] resources
the number of each resource type. Indexes 1 to n are used: 1 == SOCResourceConstants.CLAY, 2 == SOCResourceConstants.ORE, ... 5 = SOCResourceConstants.WHEAT, 6 = SOCResourceConstants.UNKNOWN.

Constructor Detail

SOCResourceSet

public SOCResourceSet()
Make an empty resource set


SOCResourceSet

public SOCResourceSet(int cl,
                      int or,
                      int sh,
                      int wh,
                      int wo,
                      int uk)
Make a resource set with stuff in it

Parameters:
cl - number of clay resources
or - number of ore resources
sh - number of sheep resources
wh - number of wheat resources
wo - number of wood resources
uk - number of unknown resources

SOCResourceSet

public SOCResourceSet(int[] rset)
Make a resource set from an array

Parameters:
rset - resource set, of length 5 or 6 (clay, ore, sheep, wheat, wood, unknown). If length is 5, unknown == 0.
Since:
1.1.08
Method Detail

clear

public void clear()
set the number of resources to zero


contains

public boolean contains(int rtype)
Does the set contain any resources of this type?

Parameters:
rtype - the type of resource, like SOCResourceConstants.CLAY
Returns:
true if the set's amount of this resource > 0
Since:
2.0.00
See Also:
getAmount(int), contains(SOCResourceSet)

getAmount

public int getAmount(int rtype)
How many resources of this type are contained in the set?

Parameters:
rtype - the type of resource, like SOCResourceConstants.CLAY
Returns:
the number of a kind of resource
See Also:
contains(int)

getTotal

public int getTotal()
Get the total number of resources in this set, including unknown types.

Returns:
the total number of resources
See Also:
getKnownTotal(), getKnownTypesCount()

getKnownTypesCount

public int getKnownTypesCount()
Get the number of known resource types contained in this set: SOCResourceConstants.CLAY to SOCResourceConstants.WOOD, excluding SOCResourceConstants.UNKNOWN or SOCResourceConstants.GOLD_LOCAL. An empty set returns 0, a set containing only wheat returns 1, that same set after adding wood and sheep returns 3, etc.

Returns:
The number of resource types in this set with nonzero resource counts.
Since:
2.0.00

getKnownTotal

public int getKnownTotal()
Get the total amount of resources of known types: SOCResourceConstants.CLAY to SOCResourceConstants.WOOD, excluding SOCResourceConstants.UNKNOWN or SOCResourceConstants.GOLD_LOCAL.

Returns:
the total number of known-type resources
Since:
1.1.14

setAmount

public void setAmount(int amt,
                      int rtype)
Set the amount of a resource. To set all resources from another set, use add(SOCResourceSet), subtract(SOCResourceSet) or setAmounts(SOCResourceSet).

Parameters:
rtype - the type of resource, like SOCResourceConstants.CLAY
amt - the amount

add

public void add(int amt,
                int rtype)
add an amount to a resource

Parameters:
rtype - the type of resource, like SOCResourceConstants.CLAY
amt - the amount; if below 0 (thus subtracting resources), the subtraction occurs and no special action is taken. subtract(int, int) takes special action in some cases.

subtract

public void subtract(int amt,
                     int rtype)
subtract an amount from a resource. If we're subtracting more from a resource than there are of that resource, set that resource to zero, and then take the difference away from the SOCResourceConstants.UNKNOWN resources. As a result, UNKNOWN may be less than zero afterwards.

Parameters:
rtype - the type of resource, like SOCResourceConstants.CLAY
amt - the amount; unlike in add(int, int), any amount that takes the resource below 0 is treated specially.

add

public void add(SOCResourceSet rs)
add an entire resource set's amounts into this set.

Parameters:
rs - the resource set

subtract

public void subtract(SOCResourceSet rs)
subtract an entire resource set. If any type's amount would go below 0, set it to 0.

Parameters:
rs - the resource set

convertToUnknown

public void convertToUnknown()
Convert all these resources to type SOCResourceConstants.UNKNOWN. Information on amount of wood, wheat, etc is no longer available. Equivalent to: int numTotal = resSet.getTotal(); resSet.clear(); resSet.setAmount (SOCResourceConstants.UNKNOWN, numTotal);


gte

public static boolean gte(SOCResourceSet a,
                          SOCResourceSet b)
Are set A's resources each greater than or equal to set B's?

Parameters:
a - set A, cannot be null
b - set B, can be null for an empty resource set
Returns:
true if each resource type in set A is >= each resource type in set B

lte

public static boolean lte(SOCResourceSet a,
                          SOCResourceSet b)
Are set A's resources each less than or equal to set B's?

Parameters:
a - set A, cannot be null
b - set B, cannot be null
Returns:
true if each resource type in set A is <= each resource type in set B

toString

public java.lang.String toString()
Human-readable form of the set, with format "clay=5|ore=1|sheep=0|wheat=0|wood=3|unknown=0"

Overrides:
toString in class java.lang.Object
Returns:
a human readable longer form of the set
See Also:
toShortString(), toFriendlyString()

toShortString

public java.lang.String toShortString()
Human-readable form of the set, with format "Resources: 5 1 0 0 3 0". Order of types is Clay, ore, sheep, wheat, wood, unknown.

Returns:
a human readable short form of the set
See Also:
toFriendlyString()

toFriendlyString

public java.lang.String toFriendlyString()
Human-readable form of the set, with format "5 clay,1 ore,3 wood". Unknown resources aren't mentioned.

Returns:
a human readable longer form of the set; if the set is empty, return the string "nothing".
See Also:
toShortString()

toFriendlyString

public boolean toFriendlyString(java.lang.StringBuffer sb)
Human-readable form of the set, with format "5 clay, 1 ore, 3 wood". Unknown resources aren't mentioned.

Parameters:
sb - Append into this buffer.
Returns:
true if anything was appended, false if sb unchanged (this resource set is empty).
See Also:
toFriendlyString()

contains

public boolean contains(SOCResourceSet sub)
Parameters:
sub - the sub set, can be null for an empty resource subset
Returns:
true if sub is in this set
See Also:
contains(int)

equals

public boolean equals(java.lang.Object anObject)
Overrides:
equals in class java.lang.Object
Parameters:
anObject - the object in question
Returns:
true if the argument is a SOCResourceSet containing the same amounts of each resource, including UNKNOWN

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object
Returns:
a hashcode for this data

copy

public SOCResourceSet copy()
Make a copy of this resource set. To instead copy another set into this one, use setAmounts(SOCResourceSet).

Returns:
a copy of this resource set

setAmounts

public void setAmounts(SOCResourceSet set)
copy a resource set into this one. This one's current data is lost and overwritten.

Parameters:
set - the set to copy from