KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > thaiopensource > validate > nrl > ModeUsage


1 package com.thaiopensource.validate.nrl;
2
3 import com.thaiopensource.util.Equal;
4 import com.thaiopensource.validate.nrl.ContextMap;
5 import com.thaiopensource.validate.nrl.Mode;
6
7 import java.util.Vector;
8 import java.util.Enumeration;
9
10 class ModeUsage {
11   private final Mode mode;
12   private final Mode currentMode;
13   private ContextMap modeMap;
14   private int attributeProcessing = -1;
15
16   ModeUsage(Mode mode, Mode currentMode) {
17     this(mode, currentMode, null);
18   }
19
20   private ModeUsage(Mode mode, Mode currentMode, ContextMap modeMap) {
21     this.mode = mode;
22     this.currentMode = currentMode;
23     this.modeMap = modeMap;
24   }
25
26   ModeUsage changeCurrentMode(Mode currentMode) {
27     return new ModeUsage(mode, currentMode, modeMap);
28   }
29
30   public boolean equals(Object obj) {
31     if (!(obj instanceof ModeUsage))
32       return false;
33     ModeUsage other = (ModeUsage)obj;
34     return this.mode == other.mode && this.currentMode == other.currentMode && Equal.equal(this.modeMap, other.modeMap);
35   }
36
37   public int hashCode() {
38     int hc = mode.hashCode() ^ currentMode.hashCode();
39     if (modeMap != null)
40       hc ^= modeMap.hashCode();
41     return hc;
42   }
43
44   private Mode resolve(Mode mode) {
45     return mode == Mode.CURRENT ? currentMode : mode;
46   }
47
48   int getAttributeProcessing() {
49     if (attributeProcessing == -1) {
50       attributeProcessing = resolve(mode).getAttributeProcessing();
51       if (modeMap != null) {
52         for (Enumeration enum = modeMap.values();
53              enum.hasMoreElements()
54              && attributeProcessing != Mode.ATTRIBUTE_PROCESSING_FULL;)
55           attributeProcessing = Math.max(resolve((Mode)enum.nextElement()).getAttributeProcessing(),
56                                          attributeProcessing);
57       }
58     }
59     return attributeProcessing;
60   }
61
62   boolean isContextDependent() {
63     return modeMap != null;
64   }
65
66   Mode getMode(Vector context) {
67     if (modeMap != null) {
68       Mode m = (Mode)modeMap.get(context);
69       if (m != null)
70         return resolve(m);
71     }
72     return resolve(mode);
73   }
74
75   boolean addContext(boolean isRoot, Vector names, Mode mode) {
76     if (modeMap == null)
77       modeMap = new ContextMap();
78     return modeMap.put(isRoot, names, mode);
79   }
80 }
81
Popular Tags