View Javadoc

1   /*
2   Wotonomy: OpenStep design patterns for pure Java applications.
3   Copyright (C) 2001 Intersect Software Corporation
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10  This library is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  Lesser General Public License for more details.
14  
15  You should have received a copy of the GNU Lesser General Public
16  License along with this library; if not, see http://www.gnu.org
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  //new net.wotonomy.ui.swing.util.StackTraceInspector();
67  //System.out.println( "ArrayFault.fireFault: before:" + this );
68              fetched = true;
69              super.protectedAddAll( 
70                  editingContext.parentObjectStore().objectsForSourceGlobalID( 
71                      sourceID,
72                      relationshipKey,
73                      editingContext ) );
74  //System.out.println( "ArrayFault.fireFault: after:" + this );
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  * $Log$
191  * Revision 1.2  2006/02/16 16:47:14  cgruber
192  * Move some classes in to "internal" packages and re-work imports, etc.
193  *
194  * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
195  *
196  * Revision 1.1  2006/02/16 13:19:57  cgruber
197  * Check in all sources in eclipse-friendly maven-enabled packages.
198  *
199  * Revision 1.5  2003/12/18 15:37:38  mpowers
200  * Changes to retain ability to work with objects that don't necessarily
201  * implement EOEnterpriseObject.  I would still like to preserve this case
202  * for general usage, however the access package is free to assume that
203  * those objects will be EOs and cast appropriately.
204  *
205  * Revision 1.4  2003/08/19 01:53:12  chochos
206  * EOObjectStore had some incompatible return types (Object instead of EOEnterpriseObject, in fault methods mostly). It's internally consistent but I hope it doesn't break anything based on this, even though fault methods mostly throw exceptions for now.
207  *
208  * Revision 1.3  2002/10/24 18:17:37  mpowers
209  * ArrayFaults are now read-only.
210  *
211  * Revision 1.2  2001/05/06 22:22:55  mpowers
212  * Debugging.
213  *
214  * Revision 1.1  2001/05/05 23:05:42  mpowers
215  * Implemented Array Faults.
216  *
217  *
218  */
219