summaryrefslogtreecommitdiff
path: root/projects/net.wotonomy.persistence/src/main/java/net/wotonomy/control/ArrayFault.java
blob: 59e135b47baaf4b57b34ad512c241776b6f556ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
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
/*
Wotonomy: OpenStep design patterns for pure Java applications.
Copyright (C) 2001 Intersect Software Corporation

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, see http://www.gnu.org
*/

package net.wotonomy.control;

import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;

import net.wotonomy.foundation.NSArray;

/**
* A class that extends NSArray to intercept any accessor calls 
* in order to defer loading until the last possible moment.<br><br>
*
* Because ArrayFault inherits from NSArray which implements
* List which implements Collection, data objects may declare 
* their relationships to be of type NSArray, List, or Collection.<br><br>
*
* This class should be returned by implementations of
* EOObjectStore.arrayFaultForSourceGlobalID().
*/
public class ArrayFault extends NSArray
{    
    private EOEditingContext editingContext;
    private EOGlobalID sourceID;
    private String relationshipKey;
    private boolean fetched;
    
    public ArrayFault( 
        EOGlobalID aSourceID, 
        String aRelationshipKey,
        EOEditingContext aContext )
    {
        super();
        editingContext = aContext;
        sourceID = aSourceID;
        relationshipKey = aRelationshipKey;
        fetched = false;
    }
    
    public boolean isFetched()
    {
        return fetched;
    }
    
    protected void fireFault()
    {        
        if ( !fetched )
        {
//new net.wotonomy.ui.swing.util.StackTraceInspector();
//System.out.println( "ArrayFault.fireFault: before:" + this );
            fetched = true;
            super.protectedAddAll( 
                editingContext.parentObjectStore().objectsForSourceGlobalID( 
                    sourceID,
                    relationshipKey,
                    editingContext ) );
//System.out.println( "ArrayFault.fireFault: after:" + this );
        }
    }
    
    public Object clone()
    {
        fireFault();
        return super.clone();
    }
    
    public boolean contains(Object elem)
    {
        fireFault();
        return super.contains( elem );
    }
    
    public boolean equals(Object o)    
    {
        fireFault();
        return super.equals( o );
    }
    
    public Object get(int index)
    {
        fireFault();
        return super.get( index );
    }

    /**
    * Overridden to return the identity hash.
    * This somewhat violates the List contract,
    * but otherwise calling hash code would
    * fire the fault.  Bottom line: don't use
    * array faults as keys in hash maps.
    */
    public int hashCode()
    {
        return System.identityHashCode( this );
    }

    public int indexOf(Object o)
    {
        fireFault();
        return super.indexOf( o );
    }

    public boolean isEmpty()
    {
        fireFault();
        return super.isEmpty();
    }

    public Iterator iterator()
    {
        fireFault();
        return super.iterator();
    }

    public int lastIndexOf(Object o)
    {
        fireFault();
        return super.lastIndexOf( o );
    }

    public ListIterator listIterator()
    {
        fireFault();
        return super.listIterator();
    }

    public ListIterator listIterator(int index)
    {
        fireFault();
        return super.listIterator( index );
    }

    public int size()
    {
        fireFault();
        return super.size();
    }
    
    public List subList(int fromIndex, int toIndex)
    {
        fireFault();
        return super.subList( fromIndex, toIndex );
    }
    
    public Object[] toArray()
    {
        fireFault();
        return super.toArray();
    }

    public Object[] toArray(Object[] a)    
    {
        fireFault();
        return super.toArray( a );
    }
    
    /**
    * Overridden to display information about
    * the fault only if not fetched.
    * Calls to super if fetched.
    */
    public String toString()
    {
        if ( isFetched() )
        {
            return super.toString();
        }
        return "[ArrayFault@"+Integer.toHexString( System.identityHashCode( this ) )+":"+sourceID+":"+relationshipKey+"]";
    }
}

/*
 * $Log$
 * Revision 1.2  2006/02/16 16:47:14  cgruber
 * Move some classes in to "internal" packages and re-work imports, etc.
 *
 * Also use UnsupportedOperationExceptions where appropriate, instead of WotonomyExceptions.
 *
 * Revision 1.1  2006/02/16 13:19:57  cgruber
 * Check in all sources in eclipse-friendly maven-enabled packages.
 *
 * Revision 1.5  2003/12/18 15:37:38  mpowers
 * Changes to retain ability to work with objects that don't necessarily
 * implement EOEnterpriseObject.  I would still like to preserve this case
 * for general usage, however the access package is free to assume that
 * those objects will be EOs and cast appropriately.
 *
 * Revision 1.4  2003/08/19 01:53:12  chochos
 * 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.
 *
 * Revision 1.3  2002/10/24 18:17:37  mpowers
 * ArrayFaults are now read-only.
 *
 * Revision 1.2  2001/05/06 22:22:55  mpowers
 * Debugging.
 *
 * Revision 1.1  2001/05/05 23:05:42  mpowers
 * Implemented Array Faults.
 *
 *
 */