Class Digraph

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

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

Digraph represents a pair of letters and their frequency of occurrence. Digraphs are ordered alphabetically.

Version:
2007.01.01
Author:
John Dalbey

Constructor Summary
Digraph()
          A Digraph is constructed with an empty letter pair, a count of zero and a default format pattern.
Digraph(LetterPair pair, Natural frequency)
          A Digraph is constructed from the provided LetterPair, the provided frequency count, and a default format pattern.
Digraph(java.lang.String pair)
          A Digraph is constructed from the provided String, a count of zero and a default format pattern.
 
Method Summary
 int compareTo(Digraph other)
          Compares this Digraph with the provided Digraph.
 boolean equals(java.lang.Object other)
          Compare two digraphs for equality.
 Natural getCount()
          Get the count from this digraph.
 void increment()
          Increment the count by 1.
 void setPattern(java.lang.String pattern)
          Set a new format pattern for this digraph's numeric display.
 java.lang.String toString()
          Return a formatted string representation of this letter pair.
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Digraph

public Digraph()
A Digraph is constructed with an empty letter pair, a count of zero and a default format pattern. The default pattern is " 0", causing one blank to appear between the letters and the count.


Digraph

public Digraph(java.lang.String pair)
A Digraph is constructed from the provided String, a count of zero and a default format pattern.

Postcondition:
a digraph is created from the string if pair.length == 2 and both characters are alphabetic. otherwise an empty digraph is created.

Digraph

public Digraph(LetterPair pair,
               Natural frequency)
A Digraph is constructed from the provided LetterPair, the provided frequency count, and a default format pattern.

Method Detail

setPattern

public void setPattern(java.lang.String pattern)
Set a new format pattern for this digraph's numeric display.

Throws:
java.lang.IllegalArgumentException - if the given pattern is not a valid java.text.DecimalFormat pattern.

getCount

public Natural getCount()
Get the count from this digraph. (May be used by a Comparator.)

Returns:
the count

increment

public void increment()
Increment the count by 1.


compareTo

public int compareTo(Digraph other)
Compares this Digraph with the provided Digraph.

Specified by:
compareTo in interface java.lang.Comparable<Digraph>
Parameters:
other - Digraph to which this Digraph is to be compared.
Returns:
-1, 0 or 1 as this Digraph's letter pair is alphabetically less than, equal to, or greater than other.

equals

public boolean equals(java.lang.Object other)
Compare two digraphs for equality. This method is provided to facilitate JUnit testing. Return true if other is equal to this digraph, i.e., compareTo() == 0.

Overrides:
equals in class java.lang.Object

toString

public java.lang.String toString()
Return a formatted string representation of this letter pair. The representation is "XXn" where XX is the letter pair and n is the count formatted according the current formatter.

Overrides:
toString in class java.lang.Object