import java.text.DecimalFormat; import java.io.*; import java.util.*; public class Key { private int value = 0; public static final int size = 8; public static final int versize = 2; public static final int abbsize = 2; public static final int rowsize = 4; private final int abbmask = 0x000000FF; private final int abbshift = 24; private final int vermask = 0x000000FF; private final int vershift = 16; private final int rowmask = 0x0000FFFF; private final int rowshift = 0; private Database TheSchema; private boolean Constructed; public Key(String strval, Database mySchema) { String buffer; int tmpint; Constructed = false; do { setTheSchema(mySchema); buffer = strval.substring(0,2); if(!setAbb(buffer)) break; buffer = strval.substring(2,4); tmpint = Integer.parseInt(buffer); if(!setVersion(tmpint)) break; buffer = strval.substring(4,8); tmpint = Integer.parseInt(buffer); if(!setRow(tmpint)) break; Constructed = true; } while(false); } public boolean Valid() { return Constructed; } public int getValue() { return value; } public int compare( Key otherkey ) { return ( value - otherkey.getValue() ); } public boolean setVersion(int r) { value &= ~(vermask << vershift); // Clear field value |= (r & vermask) << vershift; return true; } public int getVersion() { return (value >> vershift) & vermask; } public boolean setRow(int r) { value &= ~(rowmask << rowshift); value |= (r & rowmask) << rowshift; return true; } public int getRow() { return (value >> rowshift) & rowmask; } public boolean setAbb(String ab) { int i; i = TheSchema.FindTblIdx(ab); if( i < 0 ) return false; value &= ~(abbmask << abbshift); value |= (i & abbmask) << abbshift; return true; } public String getAbb() { int i = (value >> abbshift) & abbmask; return TheSchema.FindTblAbbr(i); } public Database getTheSchema() { return TheSchema; } public void setTheSchema(Database value) { TheSchema = value; } public String toString() { String retval; DecimalFormat twoDigits = new DecimalFormat( "00" ); DecimalFormat fourDigits = new DecimalFormat( "0000" ); retval = getAbb(); retval += twoDigits.format(getVersion()); retval += fourDigits.format(getRow()); return retval; } public static void main(String[] args) { Database GlobalSchema = new Database(); Key testkey = new Key( args[0], GlobalSchema ); if( testkey.Valid() ) System.out.println("Valid key : " + testkey); else System.out.println("Invalid test key : " + args[0]); } }