中国开发网: 论坛: 程序员情感CBD: 贴子 384356
老玉米
贴一个我写的用于j2ee程序的基础类,欢迎拍砖
public abstract class Record {
private int ID;
private RecordState State;

public Record()
{
ID= -1;
State = RecordState.StateAdd;
}

public Record(int id) throws DataValidationException
{
if (id <0)
throw new DataValidationException("id[" + id +"] of class Member < 0");
ID = id;
State = RecordState.StateBrowse;
}

public RecordState getState() {
return State;
}

public void setState(RecordState newState) throws StateException
{
if (State == newState)
throw new StateException(this,State,newState);

if (State == RecordState.StateBrowse){
//permit transfer from StateBrowse to StateAdd or StateCancel
if (newState != RecordState.StateUpdate)
throw new StateException(this,State,newState);
}
else if (State == RecordState.StateAdd
|| State == RecordState.StateUpdate
|| State == RecordState.StateDelete){
if (newState != RecordState.StateCancel && newState != RecordState.StateBrowse)
throw new StateException(this,State,newState);
}
else if (State == RecordState.StateCancel){
throw new StateException(this,State,newState);
}

State = newState;
}

public final int getID() throws StateException
{
if (State == RecordState.StateAdd)
throw new StateException(this,getState());
return ID;
}

public final void setID(int id) throws DataValidationException
{
if (id <0)
throw new DataValidationException("id[" + id +"] of class Member < 0");
if (ID >= 0)
throw new DataValidationException("Canot reset ID[" + ID + "]");
ID = id;
}

public String toString()
{
int id;
try{
id = getID();
}
catch (StateException e){
id = -1;
}
return getClass().getName() + "["+ id + "]";
}
}

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录