KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnode > Made > TIR > Impl > EnumDefImpl


1 /* $Id: EnumDefImpl.java,v 1.2 2004/05/20 14:23:52 bures Exp $ */
2 package SOFA.SOFAnode.Made.TIR.Impl;
3 import java.io.DataInputStream JavaDoc;
4 import java.io.DataOutputStream JavaDoc;
5 import java.io.FileInputStream JavaDoc;
6 import java.io.FileOutputStream JavaDoc;
7 import java.io.IOException JavaDoc;
8 import java.rmi.RemoteException JavaDoc;
9
10 import SOFA.SOFAnode.Made.TIR.Container;
11 import SOFA.SOFAnode.Made.TIR.DefinitionKind;
12 import SOFA.SOFAnode.Made.TIR.EnumDef;
13 import SOFA.SOFAnode.Made.TIR.EnumMember;
14 import SOFA.SOFAnode.Made.TIR.Identification;
15 import SOFA.SOFAnode.Made.TIR.Repository;
16 import SOFA.SOFAnode.Made.TIR.StateKind;
17 import SOFA.SOFAnode.Made.TIR.TIRExceptCommit;
18 import SOFA.SOFAnode.Made.TIR.TIRExceptCreate;
19 import SOFA.SOFAnode.Made.TIR.TIRExceptLock;
20 import SOFA.SOFAnode.Made.TIR.TIRObject;
21
22 public class EnumDefImpl extends ContainedImpl implements EnumDef {
23   DefinitionKindImpl defKindImpl;
24   StateKindImpl stKindImpl;
25   int numOfMembs;
26   LiItem firstMemb;
27   LiItem lastMemb;
28
29   EnumDefImpl normal;
30   boolean hasCont;
31   long workId;
32
33   public EnumDefImpl(Identification ident, Container in, Repository inrep, long workId) throws RemoteException JavaDoc {
34     super(ident,in,inrep);
35     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Enum);
36     stKindImpl = new StateKindImpl(StateKind.sk_work);
37     numOfMembs = 0;
38     firstMemb = lastMemb = null;
39     normal = null;
40     hasCont = false;
41     this.workId = workId;
42   }
43  
44   public EnumDefImpl(Container in, Repository inrep) throws RemoteException JavaDoc {
45     super(null,in,inrep);
46     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Enum);
47     stKindImpl = new StateKindImpl(StateKind.sk_normal);
48     numOfMembs = 0;
49     firstMemb = lastMemb = null;
50     normal = null;
51     hasCont = false;
52     this.workId = -1L;
53   }
54
55   public EnumDefImpl(EnumDefImpl a, Container in, Repository inrep, long workId) throws RemoteException JavaDoc {
56     super(a.id,in,inrep);
57     defKindImpl = new DefinitionKindImpl(DefinitionKind.dk_Enum);
58     stKindImpl = new StateKindImpl(StateKind.sk_work);
59     numOfMembs = 0;
60     firstMemb = lastMemb = null;
61     normal = a;
62     hasCont = false;
63     this.workId = workId;
64   }
65
66 /* from interface TIRObject */
67   public DefinitionKind get_def_kind() throws RemoteException JavaDoc {
68     return (DefinitionKind) defKindImpl;
69   }
70
71   /* from interface TIRObject */
72   public StateKind get_state() throws RemoteException JavaDoc {
73     return (StateKind) stKindImpl;
74   }
75
76   public void tag(String JavaDoc t) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
77     if (stKindImpl.value()!=StateKind.sk_work)
78       throw new TIRExceptCreate("you can call create method on work object");
79     super.tag(t);
80   }
81
82   private void addListItem(TIRObject what) {
83     LiItem item = new LiItem(what);
84     if (firstMemb == null) {
85       firstMemb = lastMemb = item;
86     } else {
87       LiItem akt = firstMemb;
88       while (akt.next != null) akt = akt.next;
89       akt.next = item;
90       item.prev = akt;
91       lastMemb = item;
92     }
93     numOfMembs++;
94   }
95
96   private void fromNormObj() throws RemoteException JavaDoc, TIRExceptLock {
97     if (normal==null)
98       return;
99     if (((WorkRepositoryImpl)rep).lock.isLocked())
100       throw new TIRExceptLock("Repository is locked");
101     int i;
102     LiItem akt;
103     akt = normal.firstMemb;
104     for(i=0;i<normal.numOfMembs;i++) {
105       addListItem(new EnumMemberImpl((EnumMemberImpl)akt.obj));
106       akt = akt.next;
107     }
108     hasCont = true;
109   }
110
111   public EnumMember[] members() throws RemoteException JavaDoc, TIRExceptLock {
112     if (stKindImpl.value()==StateKind.sk_normal && !hasCont)
113       fromNormObj();
114     EnumMember[] ret = new EnumMember [numOfMembs];
115     LiItem akt = firstMemb;
116     for(int i=0;i<numOfMembs;i++) {
117       ret[i] = (EnumMember) akt.obj;
118       akt = akt.next;
119     }
120     return ret;
121   }
122
123   public EnumMember create_member(String JavaDoc name) throws RemoteException JavaDoc, TIRExceptCreate, TIRExceptLock {
124     if (!isNew())
125       throw new TIRExceptCreate("you can call create method on work object");
126     if (name!=null && name.compareTo("")!=0) {
127       EnumMember[] a = members();
128       boolean found = false;
129       for (int i=0;i<a.length;i++) {
130         if (a[i].name().compareTo(name)==0) {
131           found = true;
132           break;
133         }
134       }
135       if (found)
136         throw new TIRExceptCreate("Member with this name exists in enum.");
137       EnumMember ret = new EnumMemberImpl(new String JavaDoc(name));
138       addListItem(ret);
139       return ret;
140     } else
141       return null;
142   }
143
144   public void save(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
145     try {
146       boolean closeFile = false;
147       if (st.curOutFile==null) { // saving in module
148
st.curOutFile = new DataOutputStream JavaDoc(new FileOutputStream JavaDoc(st.currentFile));
149     closeFile = true;
150       } else { // saving in structure (or union)
151
;
152       }
153       ((IdentificationImpl)id).save(st.curOutFile); // saving identification
154
st.curOutFile.writeInt(numOfMembs);
155       LiItem akt = firstMemb;
156       for(int i=0;i<numOfMembs;i++) { // saving all members
157
((TIRImplObject)(akt.obj)).save(st);
158     akt = akt.next;
159       }
160       if (closeFile) { // close file
161
st.curOutFile.close();
162     st.curOutFile = null;
163       }
164     } catch (IOException JavaDoc e) {
165       throw new TIRExceptStorage("Can't write in file "+st.currentFile+".");
166     }
167   }
168
169   public void load(Storage st) throws RemoteException JavaDoc, TIRExceptStorage {
170     try {
171       boolean closeFile = false;
172       if (st.curInFile==null) {
173         st.curInFile = new DataInputStream JavaDoc(new FileInputStream JavaDoc(st.currentFile));
174         closeFile = true;
175       } else {
176         ;
177       }
178       id = new IdentificationImpl();
179       ((IdentificationImpl)id).load(st.curInFile);
180       int num = st.curInFile.readInt();
181       EnumMemberImpl memb;
182       for (int i=0;i<num;i++) {
183         memb = new EnumMemberImpl();
184     memb.load(st);
185     addListItem(memb);
186       }
187       if (closeFile) { // close file
188
st.curInFile.close();
189     st.curInFile = null;
190       }
191     } catch (IOException JavaDoc e) {
192       throw new TIRExceptStorage("Can't read in file "+st.currentFile+".");
193     }
194   }
195
196   public void postLoad(RepositoryImpl r) throws RemoteException JavaDoc, TIRExceptStorage {
197     ;
198   }
199
200   public boolean isNew() {
201     return ((stKindImpl.value()==StateKind.sk_work) && normal==null);
202   }
203
204   public void canCommit() throws RemoteException JavaDoc, TIRExceptCommit {;}
205
206   public void doCommit(Container in, Repository rep) throws RemoteException JavaDoc {
207     if (stKindImpl.value()==StateKind.sk_normal)
208       return;
209     if (isNew()) {
210       stKindImpl.toNormal();
211       LiItem akt = firstMemb;
212       for (int i=0;i<numOfMembs;i++) {
213         ((TIRImplObject)akt.obj).doCommit(in,rep);
214         akt = akt.next;
215       }
216       parent = in;
217       this.rep = rep;
218     } else {
219       normal = null;
220     }
221   }
222
223   public void doAbort(long workId) throws RemoteException JavaDoc {}
224 }
225
Popular Tags