1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package net.wotonomy.control;
20
21 import java.util.Iterator;
22 import java.util.List;
23 import java.util.ListIterator;
24
25 import net.wotonomy.foundation.NSArray;
26
27 /***
28 * A class that extends NSArray to intercept any accessor calls
29 * in order to defer loading until the last possible moment.<br><br>
30 *
31 * Because ArrayFault inherits from NSArray which implements
32 * List which implements Collection, data objects may declare
33 * their relationships to be of type NSArray, List, or Collection.<br><br>
34 *
35 * This class should be returned by implementations of
36 * EOObjectStore.arrayFaultForSourceGlobalID().
37 */
38 public class ArrayFault extends NSArray
39 {
40 private EOEditingContext editingContext;
41 private EOGlobalID sourceID;
42 private String relationshipKey;
43 private boolean fetched;
44
45 public ArrayFault(
46 EOGlobalID aSourceID,
47 String aRelationshipKey,
48 EOEditingContext aContext )
49 {
50 super();
51 editingContext = aContext;
52 sourceID = aSourceID;
53 relationshipKey = aRelationshipKey;
54 fetched = false;
55 }
56
57 public boolean isFetched()
58 {
59 return fetched;
60 }
61
62 protected void fireFault()
63 {
64 if ( !fetched )
65 {
66
67
68 fetched = true;
69 super.protectedAddAll(
70 editingContext.parentObjectStore().objectsForSourceGlobalID(
71 sourceID,
72 relationshipKey,
73 editingContext ) );
74
75 }
76 }
77
78 public Object clone()
79 {
80 fireFault();
81 return super.clone();
82 }
83
84 public boolean contains(Object elem)
85 {
86 fireFault();
87 return super.contains( elem );
88 }
89
90 public boolean equals(Object o)
91 {
92 fireFault();
93 return super.equals( o );
94 }
95
96 public Object get(int index)
97 {
98 fireFault();
99 return super.get( index );
100 }
101
102 /***
103 * Overridden to return the identity hash.
104 * This somewhat violates the List contract,
105 * but otherwise calling hash code would
106 * fire the fault. Bottom line: don't use
107 * array faults as keys in hash maps.
108 */
109 public int hashCode()
110 {
111 return System.identityHashCode( this );
112 }
113
114 public int indexOf(Object o)
115 {
116 fireFault();
117 return super.indexOf( o );
118 }
119
120 public boolean isEmpty()
121 {
122 fireFault();
123 return super.isEmpty();
124 }
125
126 public Iterator iterator()
127 {
128 fireFault();
129 return super.iterator();
130 }
131
132 public int lastIndexOf(Object o)
133 {
134 fireFault();
135 return super.lastIndexOf( o );
136 }
137
138 public ListIterator listIterator()
139 {
140 fireFault();
141 return super.listIterator();
142 }
143
144 public ListIterator listIterator(int index)
145 {
146 fireFault();
147 return super.listIterator( index );
148 }
149
150 public int size()
151 {
152 fireFault();
153 return super.size();
154 }
155
156 public List subList(int fromIndex, int toIndex)
157 {
158 fireFault();
159 return super.subList( fromIndex, toIndex );
160 }
161
162 public Object[] toArray()
163 {
164 fireFault();
165 return super.toArray();
166 }
167
168 public Object[] toArray(Object[] a)
169 {
170 fireFault();
171 return super.toArray( a );
172 }
173
174 /***
175 * Overridden to display information about
176 * the fault only if not fetched.
177 * Calls to super if fetched.
178 */
179 public String toString()
180 {
181 if ( isFetched() )
182 {
183 return super.toString();
184 }
185 return "[ArrayFault@"+Integer.toHexString( System.identityHashCode( this ) )+":"+sourceID+":"+relationshipKey+"]";
186 }
187 }
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219