org.pealfactory.ring
Class ImmutableRow

java.lang.Object
  extended by org.pealfactory.ring.Row
      extended by org.pealfactory.ring.ImmutableRow
All Implemented Interfaces:
java.lang.Cloneable, Constants
Direct Known Subclasses:
RowNode

public class ImmutableRow
extends Row

This is an immutable version of the basic class which represents rows - Row. It is often more convenient and less error-prone to deal with immutable rows, especially when you need to keep semi-permanent references to them. When applying permutations to ImmutableRows, the Row methods applyPermutation() and applyPN() cannot be used. Instead, permute(org.pealfactory.ring.ImmutableRow) and change(byte[]) methods are provided, which return a new ImmutableRow and do not alter the current one.

Performance is critical for this class, so some methods are final to allow inlining by compiling VMs. The hashcode, rounds, tenorsTogether, tenorsHome and PBrow properties provided by the Row class are also cached.

In an ideal class design ImmutableRow and Row would share a common interface and have different implementations. However these classes are used in applet code where download size is crucial, so to keep the code size and number of classes as small as possible, ImmutableRow extends Row directly. All Row methods which alter internal state are overridden and will throw RuntimeExceptions - a somewhat messy way to provide immutability, but it works.


Field Summary
private  int fHashcode
           
private  boolean fHashcodeSet
           
private  boolean fPBRow
           
private  boolean fPBRowSet
           
private  boolean fRounds
           
private  boolean fTenorsHome
           
private  boolean fTenorsTogether
           
private  boolean fTHSet
           
private  boolean fTTSet
           
static ImmutableRow kROUNDS_ROW
           
 
Fields inherited from class org.pealfactory.ring.Row
fNBells, fRow
 
Fields inherited from interface org.pealfactory.ring.Constants
kCALL_BOB, kCALL_EXTREME, kCALL_PLAIN, kCALL_SINGLE, kCALL_USER, kCHAR_CROSS, kCHAR_LH, kMAXNBELLS, kMINNBELLS, kROUNDS
 
Constructor Summary
ImmutableRow(int nbells)
          Construct a row containing rounds on nbells.
ImmutableRow(Row row)
          Construct a row from another Row - faster than doing new ImmutableRow(row.toString()).
ImmutableRow(java.lang.String row)
          Construct a row from the String s, setting the number of bells from the string length.
 
Method Summary
 void applyPermutation(byte[] permutation)
          Not allowed because we are immutable - use permute() instead.
 void applyPermutation(Row permutation)
          Not allowed because we are immutable - use permute() instead.
 void applyPN(byte[] pn)
          Not allowed because we are immutable - use change() instead.
 ImmutableRow change(byte[] pn)
          Permute the row by a single change of place notation.
 int hashCode()
          Hashcode same as underlying string.
 boolean isPBrow()
          Cache value for speed
 boolean isRounds()
          Final for speed
 boolean isTenorsHome()
          Cache value for speed
 boolean isTenorsTogether()
          Cache value for speed
 ImmutableRow permute(byte[] perm)
          Permutes this row by the given change.
 ImmutableRow permute(ImmutableRow r)
          Permutes this row by the given transformation.
 void setRow(Row r)
          Not allowed - we are immutable
 void setRow(java.lang.String s)
          Not allowed - we are immutable
 void swap(int i, int j)
          Not allowed - we are immutable
 
Methods inherited from class org.pealfactory.ring.Row
bellAt, calcPermutation, clone, equals, findBell, getCoursingPairBitmap, getNBells, nextCourseBell, nextCourseBellNoTreble, setNBells, test, toBytes, toString
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Field Detail

kROUNDS_ROW

public static final ImmutableRow kROUNDS_ROW

fRounds

private boolean fRounds

fHashcodeSet

private transient boolean fHashcodeSet

fHashcode

private transient int fHashcode

fPBRowSet

private transient boolean fPBRowSet

fPBRow

private transient boolean fPBRow

fTTSet

private transient boolean fTTSet

fTenorsTogether

private transient boolean fTenorsTogether

fTHSet

private transient boolean fTHSet

fTenorsHome

private transient boolean fTenorsHome
Constructor Detail

ImmutableRow

public ImmutableRow(int nbells)
Construct a row containing rounds on nbells.


ImmutableRow

public ImmutableRow(Row row)
Construct a row from another Row - faster than doing new ImmutableRow(row.toString()).


ImmutableRow

public ImmutableRow(java.lang.String row)
Construct a row from the String s, setting the number of bells from the string length.

Method Detail

hashCode

public final int hashCode()
Hashcode same as underlying string. Cached for speed.

Overrides:
hashCode in class Row

isRounds

public final boolean isRounds()
Final for speed


isTenorsTogether

public final boolean isTenorsTogether()
Cache value for speed

Overrides:
isTenorsTogether in class Row

isTenorsHome

public final boolean isTenorsHome()
Cache value for speed

Overrides:
isTenorsHome in class Row

isPBrow

public final boolean isPBrow()
Cache value for speed

Overrides:
isPBrow in class Row

permute

public ImmutableRow permute(ImmutableRow r)
Permutes this row by the given transformation. The ImmutableRow r should be a valid change formed from kROUNDS characters and of the right length. Then, if e.g. permutation = reverse rounds, the effect is to reverse this row.


permute

public ImmutableRow permute(byte[] perm)
Permutes this row by the given change. Bells in the byte array are represented by numbers 1..nbells.


change

public ImmutableRow change(byte[] pn)
Permute the row by a single change of place notation. The place notation is represented as a byte array, and should be in the format provided by PN.getChange(int).


setRow

public void setRow(java.lang.String s)
Not allowed - we are immutable

Overrides:
setRow in class Row

setRow

public void setRow(Row r)
Not allowed - we are immutable

Overrides:
setRow in class Row

swap

public void swap(int i,
                 int j)
Not allowed - we are immutable

Overrides:
swap in class Row

applyPermutation

public void applyPermutation(Row permutation)
Not allowed because we are immutable - use permute() instead.

Overrides:
applyPermutation in class Row

applyPermutation

public void applyPermutation(byte[] permutation)
Not allowed because we are immutable - use permute() instead.

Overrides:
applyPermutation in class Row

applyPN

public void applyPN(byte[] pn)
Not allowed because we are immutable - use change() instead.

Overrides:
applyPN in class Row