1 2 package SOFA.SOFAnode.Made.TIR.Impl; 3 import java.io.IOException ; 4 import java.rmi.RemoteException ; 5 import java.rmi.server.UnicastRemoteObject ; 6 7 import SOFA.SOFAnode.Made.TIR.Container; 8 import SOFA.SOFAnode.Made.TIR.DefinitionKind; 9 import SOFA.SOFAnode.Made.TIR.ExprOperDef; 10 import SOFA.SOFAnode.Made.TIR.ExprOperKind; 11 import SOFA.SOFAnode.Made.TIR.Repository; 12 import SOFA.SOFAnode.Made.TIR.StateKind; 13 import SOFA.SOFAnode.Made.TIR.TIRExceptCalculate; 14 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit; 15 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate; 16 import SOFA.SOFAnode.Made.TIR.TIRObject; 17 import SOFA.SOFAnode.Made.TIR.ValueGen; 18 import SOFA.SOFAnode.Made.TIR.ValueGenElement; 19 20 public class ValueGenImpl extends UnicastRemoteObject implements ValueGen, TIRImplObject { 21 ExprOperKindImpl eok; 22 DefinitionKindImpl defKindImpl; 23 StateKindImpl stKindImpl; 24 int numOfItems; 25 LiItem firstEl; 26 LiItem lastEl; 27 28 public ValueGenImpl(int state) throws RemoteException { 29 eok = new ExprOperKindImpl(ExprOperKind.eok_valuegen); 30 defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_ValueGen); 31 stKindImpl = new StateKindImpl(state); 32 firstEl = null; 33 lastEl = null; 34 } 35 36 public ValueGenElement[] elements() throws RemoteException { 37 ValueGenElement[] ret = new ValueGenElement [numOfItems]; 38 LiItem akt = firstEl; 39 for(int i=0;i<numOfItems;i++) { 40 ret[i] = (ValueGenElement) akt.obj; 41 akt = akt.next; 42 } 43 return ret; 44 } 45 46 public ValueGenElement create_element(ExprOperDef expr1, ExprOperDef expr2) throws RemoteException , TIRExceptCreate { 47 if (stKindImpl.value() == StateKind.sk_normal) 48 throw new TIRExceptCreate("You can call create method on work object only"); 49 ValueGenElement ret = new ValueGenElementImpl(expr1, expr2); 50 addListItem(ret); 51 return ret; 52 } 53 54 55 public DefinitionKind get_def_kind() throws RemoteException { 56 return (DefinitionKind) defKindImpl; 57 } 58 59 60 public StateKind get_state() throws RemoteException { 61 return (StateKind) stKindImpl; 62 } 63 64 public ExprOperKind get_eok_kind() throws RemoteException { 65 return (ExprOperKind) eok; 66 } 67 68 public ExprOperDef calculate() throws RemoteException , TIRExceptCalculate { 69 throw new TIRExceptCalculate("Can't calculate value generator."); 70 } 71 72 public void addListItem(TIRObject what) { 73 LiItem item = new LiItem(what); 74 if (firstEl == null) { 75 firstEl = lastEl = item; 76 } else { 77 LiItem akt = firstEl; 78 while (akt.next != null) akt = akt.next; 79 akt.next = item; 80 item.prev = akt; 81 lastEl = item; 82 } 83 numOfItems++; 84 } 85 86 public void save(Storage st) throws RemoteException , TIRExceptStorage { 87 try { 88 st.curOutFile.writeInt(eok.value()); 89 st.curOutFile.writeInt(numOfItems); 90 LiItem akt = firstEl; 91 for(int i=0;i<numOfItems;i++) { 92 ((TIRImplObject) akt.obj).save(st); 93 akt = akt.next; 94 } 95 } catch (IOException e) { 96 throw new TIRExceptStorage("Can't write in directory "+st.current+"."); 97 } 98 } 99 100 public void load(Storage st) throws RemoteException , TIRExceptStorage { 101 try { 102 int num = st.curInFile.readInt(); 103 TIRImplObject obj; 104 for(int i=0;i<num;i++) { 105 obj = new ValueGenElementImpl(); 106 obj.load(st); 107 addListItem((TIRObject) obj); 108 } 109 } catch (IOException e) { 110 throw new TIRExceptStorage("Can't read in directory "+st.current+"."); 111 } 112 } 113 114 public void postLoad(RepositoryImpl r) throws RemoteException , TIRExceptStorage { 115 LiItem akt = firstEl; 116 for (int i =0;i<numOfItems;i++) { 117 ((TIRImplObject) akt.obj).postLoad(r); 118 akt = akt.next; 119 } 120 } 121 122 public boolean isNew() { 123 return (stKindImpl.value()==StateKind.sk_work); 124 } 125 126 public void canCommit() throws RemoteException , TIRExceptCommit {;} 127 128 public void doCommit(Container in, Repository rep) throws RemoteException { 129 if (stKindImpl.value()==StateKind.sk_work) { 130 stKindImpl.toNormal(); 131 LiItem akt = firstEl; 132 for (int i=0;i<numOfItems;i++) { 133 ((TIRImplObject)akt.obj).doCommit(in,rep); 134 akt = akt.next; 135 } 136 } 137 } 138 139 public void doAbort(long workId) throws RemoteException {} 140 } 141 | Popular Tags |