package org.crosswire.xml;
/**
* Copyright (c) 2001 CrossWire Bible Society.
* Distributable under the terms of the GNU GPL V2.
*/
import java.io.Serializable;
/**
* Overview:
* The status object holds the information to indicate whether
* or not a task has been completed. Status is made up 31
* indexes, plus one at the start indicating the overall state. Each index represents a
* specific task, and the mapping of the index and the task is defined at
* the configuration of Dispatcher
* The status for each task is either 0 or 1 where 0 means not yet
* processed and 1 means has been processed. Status
is
* primarily used by ManagerService
,
* Dropbox
, and Dispatcher
. In a typical
* processing flow, Dropbox
seeds
* an initial status value. Then, Dispatcher
* will set its associated index to '1' if that the task has been processed
* successfully.
*
*
**/
public class Status implements Serializable, Cloneable {
/**
* The overall state.
* READY - Ready to be processed.
* CANCELED - Canceled.
* DONE - Has been processed by all the processors.
*
**/
public static final char MASK_BIT_ON = '1';
public static final char MASK_BIT_OFF = '0';
public static final char MASK_BIT_NA = '_';
public static final char STATE_PENDING = 'P';
public static final char STATE_READY = 'R';
public static final char STATE_DONE = 'D';
public static final char STATE_CANCELED = 'C';
public static final char STATE_NA = '_';
private static final int size = 31;
private char[] status = new char[size];
/**
* The default constructor. The status 'R000000000000000000000000000000'
* will be constructed.
*
**/
public Status() {
setStatus(new String() + STATE_READY);
}
public Object clone() {
return new Status(this.toString());
}
/**
* Constructor that accept a string as an input.
*
**/
public Status(String statusString) {
setStatus(statusString);
}
public void setBit(int bit) {
setMask((long)Math.pow(2,bit));
}
public void setBit(int bit, char value) {
setMaskVal((long)Math.pow(2,bit), value);
}
public void clearBit(int bit) {
clearMask((long)Math.pow(2,bit));
}
public void setMaskVal(long mask, char value) {
int count = 1;
for(int i=0; ((i0)); i++) {
if ((mask&((long)Math.pow(2,i))) > 0) {
this.status[count] = value;
mask -= Math.pow(2,i);
}
count++;
}
}
/**
* Set status indexes. The valid input is an integer number, and
* the integer is converted to a binary representation where the
* least significant bit (LSB) refer to index 1 of the status, the
* second LSB refer to index 2, and so on. By using this method,
* all the bits that have the value '1', its corresponding index
* will be set to '1'. Let say the input integer value is 6 and the
* status is 'R000010000000000000000000000000'. Since The binary of
* 6 is '011', result would be 'R011010000000000000000000000000'.
*
**/
public void setMask(long mask) {
setMaskVal(mask, MASK_BIT_ON);
}
/**
* Clear status indexes. The valid input is an integer number, and
* the integer is converted to a binary representation where the
* least significant bit (LSB) refer to index 1 of the status, the
* second LSB refer to index 2, and so on. By using this method,
* all the bits that have the value '1', its corresponding index
* will be set to '0'. Let say the input integer value is 6 and the
* status is 'R111110000000000000000000000000'. Since The binary of
* 6 is '011', result would be 'R100110000000000000000000000000'.
*
**/
public void clearMask(long mask) {
setMaskVal(mask, MASK_BIT_OFF);
}
public boolean isBitSet(int bit) {
return isMaskSet((long)Math.pow(2, bit));
}
public boolean isBitClear(int bit) {
return isMaskClear((long)Math.pow(2, bit));
}
private boolean checkMaskVal(long mask, char value) {
boolean result = true;
int count = 1;
for(int i=0; ((i0)); i++) {
if ((mask&((long)Math.pow(2,i))) > 0) {
if (this.status[count] != value)
return false;
mask -= Math.pow(2,i);
}
count++;
}
return true;
}
/**
* Test status indexes. The valid input is an integer number, and
* the integer is converted to a binary representation where the
* least significant bit (LSB) refer to index 1 of the status, the
* second LSB refer to index 2, and so on. By using this method,
* all the bits that have the value '1', test its corresponding index
* to see if it is set to '1'. Let say the input integer value is 6
* and the status is 'R111110000000000000000000000000'. Since The binary of
* 6 is '011', result would be true.
*
**/
public boolean isMaskSet(long mask) {
return checkMaskVal(mask, '1');
}
/**
* Test status indexes. The valid input is an integer number, and
* the integer is converted to a binary representation where the
* least significant bit (LSB) refer to index 1 of the status, the
* second LSB refer to index 2, and so on. By using this method,
* all the bits that have the value '1', test its corresponding index
* to see if it is set to '0'. Let say the input integer value is 6
* and the status is 'R111110000000000000000000000000'. Since The binary of
* 6 is '011', result would be false.
*
**/
public boolean isMaskClear(long mask) {
return checkMaskVal(mask, '0');
}
/**
* Get the overall state/status of this Status object.
*
**/
public char getState() {
return status[0];
}
/**
* Set the overall state/status of this Status object.
*
**/
public String setState(char state) {
status[0] = state;
return toString();
}
/**
* Get this Status object in string.
*
**/
public String toString() {
return new String(status);
}
/**
* Return the decimal value of this Status object. The first index is
* ignored and the index #1 represents the least significant bit(LSB),
* and the index #2 represents the second LSB, and so on. For example,
* if status equal 'R111000000000000000000000000000', the result will
* be 7.
*
**/
public long getLong() {
long result = 0;
for(int i=1; i 0) : false;
String retVal = "";
char[] smChars = sm.toCharArray();
// Set the object state
if (anyFilter) {
setState(smChars[0]);
}
else {
setState (STATE_NA);
}
for (int i = 1; i < size; i++) {
if (anyFilter) {
// Check if there is a character in the smChars array to copy
// into the maskBits
if (i < smLength) {
status[i] = smChars[i];
}
else {
// No so set it to NA
status[i] = MASK_BIT_NA;
}
}
else {
status[i] = MASK_BIT_NA;
}
// maskBits[i] = (anyFilter) ? (i < sm.length()) ? sm.toCharArray()[i] :
// Status.MASK_BIT_NA :
// Status.MASK_BIT_NA;
}
}
}