soc.game
Class SOCResourceSet

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

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:
Serialized Form

Field Summary
static SOCResourceSet EMPTY_SET
          Resource set with zero of each resource type
private  int[] resources
          the number of resources
 
Constructor Summary
SOCResourceSet()
          Make an empty resource set
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(SOCResourceSet sub)
           
 void convertToUnknown()
          Convert all these resources to type SOCResourceConstants.UNKNOWN.
 SOCResourceSet copy()
           
 boolean equals(java.lang.Object anObject)
           
 int getAmount(int rtype)
           
 int getTotal()
           
static boolean gte(SOCResourceSet a, SOCResourceSet b)
           
 int hashCode()
           
static boolean lte(SOCResourceSet a, SOCResourceSet b)
           
 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 resources

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
Method Detail

clear

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


getAmount

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

getTotal

public int getTotal()
Returns:
the total number of resources

setAmount

public void setAmount(int amt,
                      int rtype)
set the amount of a resource

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)
Parameters:
a - set A
b - set B
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)
Parameters:
a - set A
b - set B
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"

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"

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"

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
Returns:
true if sub is in this set

equals

public boolean equals(java.lang.Object anObject)
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()
Returns:
a hashcode for this data

copy

public SOCResourceSet copy()
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