001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.openjpa.kernel;
020:
021: import java.io.ObjectStreamException;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.ListIterator;
026:
027: import org.apache.openjpa.lib.rop.ResultList;
028: import org.apache.openjpa.util.RuntimeExceptionTranslator;
029:
030: /**
031: * Delegating result list that can also perform exception translation
032: * for use in facades.
033: *
034: * @since 0.4.0
035: * @author Marc Prud'hommeaux
036: * @nojavadoc
037: */
038: public class DelegatingResultList implements ResultList {
039:
040: private final ResultList _del;
041: private final RuntimeExceptionTranslator _trans;
042:
043: /**
044: * Constructor; supply delegate.
045: */
046: public DelegatingResultList(ResultList list) {
047: this (list, null);
048: }
049:
050: /**
051: * Constructor; supply delegate and exception translator.
052: */
053: public DelegatingResultList(ResultList list,
054: RuntimeExceptionTranslator trans) {
055: _del = list;
056: _trans = trans;
057: }
058:
059: /**
060: * Return the direct delegate.
061: */
062: public ResultList getDelegate() {
063: return _del;
064: }
065:
066: /**
067: * Return the native delegate.
068: */
069: public ResultList getInnermostDelegate() {
070: return _del instanceof DelegatingResultList ? ((DelegatingResultList) _del)
071: .getInnermostDelegate()
072: : _del;
073: }
074:
075: /**
076: * Writes delegate, which may in turn write a normal list.
077: */
078: public Object writeReplace() throws ObjectStreamException {
079: return _del;
080: }
081:
082: public int hashCode() {
083: try {
084: return getInnermostDelegate().hashCode();
085: } catch (RuntimeException re) {
086: throw translate(re);
087: }
088: }
089:
090: public boolean equals(Object other) {
091: if (other == this )
092: return true;
093: if (other instanceof DelegatingResultList)
094: other = ((DelegatingResultList) other)
095: .getInnermostDelegate();
096: try {
097: return getInnermostDelegate().equals(other);
098: } catch (RuntimeException re) {
099: throw translate(re);
100: }
101: }
102:
103: /**
104: * Translate the OpenJPA exception.
105: */
106: protected RuntimeException translate(RuntimeException re) {
107: return (_trans == null) ? re : _trans.translate(re);
108: }
109:
110: public boolean isProviderOpen() {
111: try {
112: return _del.isProviderOpen();
113: } catch (RuntimeException re) {
114: throw translate(re);
115: }
116: }
117:
118: public void close() {
119: try {
120: _del.close();
121: } catch (RuntimeException re) {
122: throw translate(re);
123: }
124: }
125:
126: public boolean isClosed() {
127: try {
128: return _del.isClosed();
129: } catch (RuntimeException re) {
130: throw translate(re);
131: }
132: }
133:
134: public int size() {
135: try {
136: return _del.size();
137: } catch (RuntimeException re) {
138: throw translate(re);
139: }
140: }
141:
142: public boolean isEmpty() {
143: try {
144: return _del.isEmpty();
145: } catch (RuntimeException re) {
146: throw translate(re);
147: }
148: }
149:
150: public boolean contains(Object o) {
151: try {
152: return _del.contains(o);
153: } catch (RuntimeException re) {
154: throw translate(re);
155: }
156: }
157:
158: public Iterator iterator() {
159: return listIterator();
160: }
161:
162: public Object[] toArray() {
163: try {
164: return _del.toArray();
165: } catch (RuntimeException re) {
166: throw translate(re);
167: }
168: }
169:
170: public Object[] toArray(Object[] a) {
171: try {
172: return _del.toArray(a);
173: } catch (RuntimeException re) {
174: throw translate(re);
175: }
176: }
177:
178: public boolean add(Object o) {
179: try {
180: return _del.add(o);
181: } catch (RuntimeException re) {
182: throw translate(re);
183: }
184: }
185:
186: public boolean remove(Object o) {
187: try {
188: return _del.remove(o);
189: } catch (RuntimeException re) {
190: throw translate(re);
191: }
192: }
193:
194: public boolean containsAll(Collection c) {
195: try {
196: return _del.containsAll(c);
197: } catch (RuntimeException re) {
198: throw translate(re);
199: }
200: }
201:
202: public boolean addAll(Collection c) {
203: try {
204: return _del.addAll(c);
205: } catch (RuntimeException re) {
206: throw translate(re);
207: }
208: }
209:
210: public boolean addAll(int index, Collection c) {
211: try {
212: return _del.addAll(index, c);
213: } catch (RuntimeException re) {
214: throw translate(re);
215: }
216: }
217:
218: public boolean removeAll(Collection c) {
219: try {
220: return _del.removeAll(c);
221: } catch (RuntimeException re) {
222: throw translate(re);
223: }
224: }
225:
226: public boolean retainAll(Collection c) {
227: try {
228: return _del.retainAll(c);
229: } catch (RuntimeException re) {
230: throw translate(re);
231: }
232: }
233:
234: public void clear() {
235: try {
236: _del.clear();
237: } catch (RuntimeException re) {
238: throw translate(re);
239: }
240: }
241:
242: public Object get(int index) {
243: try {
244: return _del.get(index);
245: } catch (RuntimeException re) {
246: throw translate(re);
247: }
248: }
249:
250: public Object set(int index, Object element) {
251: try {
252: return _del.set(index, element);
253: } catch (RuntimeException re) {
254: throw translate(re);
255: }
256: }
257:
258: public void add(int index, Object element) {
259: try {
260: _del.add(index, element);
261: } catch (RuntimeException re) {
262: throw translate(re);
263: }
264: }
265:
266: public Object remove(int index) {
267: try {
268: return _del.remove(index);
269: } catch (RuntimeException re) {
270: throw translate(re);
271: }
272: }
273:
274: public int indexOf(Object o) {
275: try {
276: return _del.indexOf(o);
277: } catch (RuntimeException re) {
278: throw translate(re);
279: }
280: }
281:
282: public int lastIndexOf(Object o) {
283: try {
284: return _del.lastIndexOf(o);
285: } catch (RuntimeException re) {
286: throw translate(re);
287: }
288: }
289:
290: public ListIterator listIterator() {
291: try {
292: return new DelegatingListIterator(_del.listIterator());
293: } catch (RuntimeException re) {
294: throw translate(re);
295: }
296: }
297:
298: public ListIterator listIterator(int index) {
299: try {
300: return new DelegatingListIterator(_del.listIterator(index));
301: } catch (RuntimeException re) {
302: throw translate(re);
303: }
304: }
305:
306: public List subList(int fromIndex, int toIndex) {
307: try {
308: return _del.subList(fromIndex, toIndex);
309: } catch (RuntimeException re) {
310: throw translate(re);
311: }
312: }
313:
314: /**
315: * Delegating iterator that also performs exception translation.
316: */
317: public class DelegatingListIterator implements ListIterator {
318:
319: private final ListIterator _del;
320:
321: /**
322: * Constructor; supply delegate.
323: */
324: public DelegatingListIterator(ListIterator it) {
325: _del = it;
326: }
327:
328: /**
329: * Return the direct delegate.
330: */
331: public ListIterator getDelegate() {
332: return _del;
333: }
334:
335: /**
336: * Return the native delegate.
337: */
338: public ListIterator getInnermostDelegate() {
339: return _del instanceof DelegatingListIterator ? ((DelegatingListIterator) _del)
340: .getInnermostDelegate()
341: : _del;
342: }
343:
344: public int hashCode() {
345: try {
346: return getInnermostDelegate().hashCode();
347: } catch (RuntimeException re) {
348: throw translate(re);
349: }
350: }
351:
352: public boolean equals(Object other) {
353: if (other == this )
354: return true;
355: if (other instanceof DelegatingListIterator)
356: other = ((DelegatingListIterator) other)
357: .getInnermostDelegate();
358: try {
359: return getInnermostDelegate().equals(other);
360: } catch (RuntimeException re) {
361: throw translate(re);
362: }
363: }
364:
365: public boolean hasNext() {
366: try {
367: return _del.hasNext();
368: } catch (RuntimeException re) {
369: throw translate(re);
370: }
371: }
372:
373: public Object next() {
374: try {
375: return _del.next();
376: } catch (RuntimeException re) {
377: throw translate(re);
378: }
379: }
380:
381: public boolean hasPrevious() {
382: try {
383: return _del.hasPrevious();
384: } catch (RuntimeException re) {
385: throw translate(re);
386: }
387: }
388:
389: public Object previous() {
390: try {
391: return _del.previous();
392: } catch (RuntimeException re) {
393: throw translate(re);
394: }
395: }
396:
397: public int nextIndex() {
398: try {
399: return _del.nextIndex();
400: } catch (RuntimeException re) {
401: throw translate(re);
402: }
403: }
404:
405: public int previousIndex() {
406: try {
407: return _del.previousIndex();
408: } catch (RuntimeException re) {
409: throw translate(re);
410: }
411: }
412:
413: public void remove() {
414: try {
415: _del.remove();
416: } catch (RuntimeException re) {
417: throw translate(re);
418: }
419: }
420:
421: public void set(Object o) {
422: try {
423: _del.set(o);
424: } catch (RuntimeException re) {
425: throw translate(re);
426: }
427: }
428:
429: public void add(Object o) {
430: try {
431: _del.add(o);
432: } catch (RuntimeException re) {
433: throw translate(re);
434: }
435: }
436: }
437: }
|