org.thc.shagg
Class State

java.lang.Object
  |
  +--org.thc.shagg.State
All Implemented Interfaces:
java.lang.Comparable, java.io.Serializable

public class State
extends java.lang.Object
implements java.lang.Comparable, java.io.Serializable

This class represents a state during the analysis of a set of serial a number. A state holds information about the used amount of serial numbers, algorithm, weights, and partition that matched a serial number. During further progress the hits of a state are increased if he matches another serial number.

In order to store the states in an efficient data structure this class implements the interface comparable and the states may be stored in a tree map or any other sorted map that sorts on comparable objects.

Version:
$Id: State.java,v 1.10 2003/07/18 10:08:05 plasmoid Exp $
Author:
Plasmoid (plasmoid@thc.org), THC
See Also:
Serialized Form

Field Summary
private  int algorithm
           
private  int amount
           
private  int hits
           
private  java.lang.String id
           
private  int[] partition
           
private  int[] weights
           
 
Constructor Summary
State(int[] w, int a, int[] p, int l)
          Construct a state.
 
Method Summary
 boolean check(int[] number, CheckDigitAlgorithm[] a)
          Checks a serial number against the data from this state.
static java.util.Vector clear(java.util.Map t, int limit)
          Clear map from states that don't have enough hits.
 int compareTo(java.lang.Object s)
          Compares two states.
 boolean equals(State s)
          Check if two states are equal.
private  java.lang.String generateUniqueId()
          Generates a unique id.
 int getAlgorithm()
          Get algorithm of this state.
 int getAmout()
          Get amount of serial numbers that were used generarting this state.
 int getHits()
          Get hit counter.
 java.lang.String getId()
          Get the unique id of this state.
 int getLength()
          Get length of serial number.
 int[] getPartition()
          Get partition of this state.
 int[] getWeights()
          Get weights of this state.
 void hit()
          Hit! Increase hit counter.
 void hit(java.util.Map t)
          Find a state in a map and give it a hit.
static State load(java.lang.String filename)
          Load a state from a file.
static java.util.Vector loadVector(java.lang.String filename)
          Load vector of states from a file.
 void print(CheckDigitAlgorithm[] algorithms)
          Print state.
 void save(java.lang.String filename)
          Save a state to a file.
static void saveVector(java.lang.String filename, java.util.Vector v)
          Save a vector of states to a file.
 
Methods inherited from class java.lang.Object
, clone, equals, finalize, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

weights

private int[] weights

partition

private int[] partition

algorithm

private int algorithm

amount

private int amount

hits

private int hits

id

private java.lang.String id
Constructor Detail

State

public State(int[] w,
             int a,
             int[] p,
             int l)
Construct a state.
Parameters:
w - Weights that matched.
a - Number of algorithm that matched.
p - Partition that matched.
l - Length of serial number
Method Detail

generateUniqueId

private java.lang.String generateUniqueId()
Generates a unique id. In order to perform sorting on a given set it is necessary for the elements of the set have a total ordering. This unique id helps implementing a compareTo() method.
Returns:
id represented by a string.
See Also:
compareTo(java.lang.Object)

hit

public void hit()
Hit! Increase hit counter.

getAmout

public int getAmout()
Get amount of serial numbers that were used generarting this state.
Returns:
amount

getHits

public int getHits()
Get hit counter.
Returns:
Hits.

getId

public java.lang.String getId()
Get the unique id of this state.
Returns:
Id.

getWeights

public int[] getWeights()
Get weights of this state.
Returns:
Weights.

getAlgorithm

public int getAlgorithm()
Get algorithm of this state.
Returns:
Number of algorithm

getPartition

public int[] getPartition()
Get partition of this state.
Returns:
Partition.

getLength

public int getLength()
Get length of serial number.
Returns:
Length

equals

public boolean equals(State s)
Check if two states are equal. This method is based on the equals() method of the String class.
Parameters:
s - State to compare with this state.
Returns:
true or false.
See Also:
String.equals(java.lang.Object)

compareTo

public int compareTo(java.lang.Object s)
Compares two states. This method is based on the compareTo() method of the String class.
Specified by:
compareTo in interface java.lang.Comparable
Parameters:
s - State to compare with this state.
Returns:
-1 this state is "smaller", 0 states are equal, 1 this state is "bigger".
See Also:
String.compareTo(java.lang.String)

hit

public void hit(java.util.Map t)
Find a state in a map and give it a hit. The method performs a find operation on the given map, if the state is not found it is added to the map, otherwise it receives another hit. This function is very time-consuming, I recommand using data structure that perform the find and the addition in O(log(n)), e.g. a red-black tree as used in the Java util implementation.
Parameters:
t - Map
See Also:
Map, SortedMap

clear

public static java.util.Vector clear(java.util.Map t,
                                     int limit)
Clear map from states that don't have enough hits. This function reads the entries from the given map and checks if each entry has more than limit hits, if not the state is removed. Runtime O(n), this is expensive!
Parameters:
t - Map.
limit - Limit for hits.
Returns:
Vector of all states that have enough hits.

print

public void print(CheckDigitAlgorithm[] algorithms)
Print state. The name of the algorithm and the length of the serial number analysed are obtained through the parameters.
Parameters:
algorithms - Array of check digit algorithms
num - Length of the serial number.
See Also:
CheckDigitAlgorithm

load

public static State load(java.lang.String filename)
                  throws java.io.IOException,
                         java.lang.ClassNotFoundException
Load a state from a file. The file is compressed using the gzip compression provided by Java.
Parameters:
filename - Name of file.
Returns:
The loaded state.
Throws:
java.io.IOException - if anything goes wrong
java.lang.ClassNotFoundException - if the loaded file is not a correct class

save

public void save(java.lang.String filename)
          throws java.io.IOException
Save a state to a file. The file is compressed using the gzip compression provided by Java.
Parameters:
filename - Name of file.
Throws:
java.io.IOException - if anything goes wrong

loadVector

public static java.util.Vector loadVector(java.lang.String filename)
                                   throws java.io.IOException,
                                          java.lang.ClassNotFoundException
Load vector of states from a file. The file is compressed using the gzip compression provided by Java.
Parameters:
filename - Name of file.
Returns:
The loaded vector.
Throws:
java.io.IOException - if anything goes wrong.
java.lang.ClassNotFoundException - if the loaded file is not a correct class

saveVector

public static void saveVector(java.lang.String filename,
                              java.util.Vector v)
                       throws java.io.IOException
Save a vector of states to a file. The file is compressed using the gzip compression provided by Java.
Parameters:
filename - Name of file.
v - Vector of states.
Throws:
java.io.IOException - if anything goes wrong.

check

public boolean check(int[] number,
                     CheckDigitAlgorithm[] a)
Checks a serial number against the data from this state.
Parameters:
number - Serial number
a - Check Digit algorithm to use
Returns:
true or false