Class LetterInventory

java.lang.Object
  extended by LetterInventory
All Implemented Interfaces:
java.lang.Comparable<LetterInventory>

public class LetterInventory
extends java.lang.Object
implements java.lang.Comparable<LetterInventory>

A LetterInventory object represents the count of each letter A-Z found in a given String.


Constructor Summary
LetterInventory(java.lang.String s)
          Constructs a letter inventory to count the letters in the given string.
 
Method Summary
 void add(LetterInventory other)
          Adds the letters in the given inventory to those in this one.
 void add(java.lang.String s)
          Adds the letters in the given string to those in this inventory.
 int compareTo(LetterInventory other)
          Compares this inventory to the given other one by the alphabetical order of their toString representations.
 boolean contains(LetterInventory other)
          Returns true if this inventory contains all of the letters in the given inventory in at least as large of a count.
 boolean contains(java.lang.String s)
          Returns true if this inventory contains all of the letters in the given string in at least as large of a count.
 boolean equals(java.lang.Object o)
          Returns whether o refers to a LetterInventory object with equal counts of letters to this LetterInventory.
 boolean isEmpty()
          Returns whether there are no letters in this inventory.
 int size()
          Returns the total number of letters in this inventory.
 void subtract(LetterInventory other)
          Removes the letters in the given inventory from this one.
 void subtract(java.lang.String s)
          Removes the letters in the given string from this one.
 java.lang.String toString()
          Returns a string representation of this inventory, such as "[ehhhillo]".
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

LetterInventory

public LetterInventory(java.lang.String s)
Constructs a letter inventory to count the letters in the given string.

Parameters:
s - The string to inventory.
Throws:
java.lang.NullPointerException - if the string passed is null.
Method Detail

add

public void add(LetterInventory other)
Adds the letters in the given inventory to those in this one. For example, adding inventories [ehllo] and [hhio] yields [ehhhillo].

Parameters:
other - the inventory to add.
Throws:
java.lang.NullPointerException - if the inventory passed is null.

add

public void add(java.lang.String s)
Adds the letters in the given string to those in this inventory. For example, adding inventories [ehllo] and "hi ho" yields [ehhhillo].

Parameters:
s - the string to add.
Throws:
java.lang.NullPointerException - if the string passed is null.

compareTo

public int compareTo(LetterInventory other)
Compares this inventory to the given other one by the alphabetical order of their toString representations. In other words, [bbcfy] < [bdfy] < [gil] < [xyz].

Specified by:
compareTo in interface java.lang.Comparable<LetterInventory>
Parameters:
other - the inventory to compare against.
Returns:
< 0 if this inventory comes "before" the other; 0 if they contain the same letters; or 1 if this inventory comes "after" the other one.
Throws:
java.lang.NullPointerException - if the inventory passed is null.

contains

public boolean contains(LetterInventory other)
Returns true if this inventory contains all of the letters in the given inventory in at least as large of a count. For example, [ehhhillo] contains [ehho].

Parameters:
other - The other inventory to examine.
Returns:
true if this inventory contains all of the letters in the given inventory, otherwise false.
Throws:
java.lang.NullPointerException - if the inventory passed is null.

contains

public boolean contains(java.lang.String s)
Returns true if this inventory contains all of the letters in the given string in at least as large of a count. For example, [ehhhillo] contains "he ho".

Parameters:
s - The string to examine.
Returns:
true if this inventory contains all of the letters in the given inventory, otherwise false.
Throws:
java.lang.NullPointerException - if the string passed is null.

equals

public boolean equals(java.lang.Object o)
Returns whether o refers to a LetterInventory object with equal counts of letters to this LetterInventory.

Overrides:
equals in class java.lang.Object
Parameters:
o - the object to compare against.
Returns:
true if o refers to a LetterInventory object with equal counts of letters to this LetterInventory, otherwise false.

isEmpty

public boolean isEmpty()
Returns whether there are no letters in this inventory.

Returns:
true if there are no letters in this inventory, otherwise false

size

public int size()
Returns the total number of letters in this inventory.

Returns:
the total number of letters in this inventory (0 if the inventory is empty).

subtract

public void subtract(LetterInventory other)
Removes the letters in the given inventory from this one. For example, [ehhhillo] minus [ehho] yields [hill].

Parameters:
other - the inventory to subtract.
Throws:
java.lang.IllegalArgumentException - if the letters in the other inventory are not contained in this one.
java.lang.NullPointerException - if the inventory passed is null.

subtract

public void subtract(java.lang.String s)
Removes the letters in the given string from this one. For example, [ehhhillo] minus "he ho" yields [hill].

Parameters:
s - the string to subtract.
Throws:
java.lang.IllegalArgumentException - if the letters in the string are not contained in this one.
java.lang.NullPointerException - if the string passed is null.

toString

public java.lang.String toString()
Returns a string representation of this inventory, such as "[ehhhillo]".

Overrides:
toString in class java.lang.Object
Returns:
a string representation of this inventory, such as "[ehhhillo]", or "[]" for an empty inventory.