01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one
03: * or more contributor license agreements. See the NOTICE file
04: * distributed with this work for additional information
05: * regarding copyright ownership. The ASF licenses this file
06: * to you under the Apache License, Version 2.0 (the
07: * "License"); you may not use this file except in compliance
08: * with the License. You may obtain a copy of the License at
09: *
10: * http://www.apache.org/licenses/LICENSE-2.0
11: *
12: * Unless required by applicable law or agreed to in writing,
13: * software distributed under the License is distributed on an
14: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15: * KIND, either express or implied. See the License for the
16: * specific language governing permissions and limitations
17: * under the License.
18: */
19: package org.apache.openjpa.lib.rop;
20:
21: import java.util.List;
22:
23: import org.apache.commons.lang.exception.NestableRuntimeException;
24: import org.apache.openjpa.lib.util.Closeable;
25:
26: /**
27: * A result object provider wrapped around a normal list.
28: *
29: * @author Abe White
30: */
31: public class ListResultObjectProvider implements ResultObjectProvider {
32:
33: private final List _list;
34: private int _idx = -1;
35:
36: /**
37: * Constructor. Supply delegate.
38: */
39: public ListResultObjectProvider(List list) {
40: _list = list;
41: }
42:
43: public List getDelegate() {
44: return _list;
45: }
46:
47: public boolean supportsRandomAccess() {
48: return true;
49: }
50:
51: public void open() throws Exception {
52: }
53:
54: public Object getResultObject() throws Exception {
55: return _list.get(_idx);
56: }
57:
58: public boolean next() throws Exception {
59: return absolute(_idx + 1);
60: }
61:
62: public boolean absolute(int pos) throws Exception {
63: if (pos >= 0 && pos < _list.size()) {
64: _idx = pos;
65: return true;
66: }
67: return false;
68: }
69:
70: public int size() throws Exception {
71: return _list.size();
72: }
73:
74: public void reset() throws Exception {
75: _idx = -1;
76: }
77:
78: public void close() throws Exception {
79: if (_list instanceof Closeable)
80: try {
81: ((Closeable) _list).close();
82: } catch (Exception e) {
83: }
84: }
85:
86: public void handleCheckedException(Exception e) {
87: // shouldn't ever happen
88: throw new NestableRuntimeException(e);
89: }
90: }
|