1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.dao.orm.jpa;
16  
17  import com.liferay.portal.kernel.dao.orm.ORMException;
18  import com.liferay.portal.kernel.dao.orm.ScrollableResults;
19  
20  import java.util.List;
21  
22  /**
23   * <a href="ScrollableResultsImpl.java.html"><b><i>View Source</i></b></a>
24   *
25   * @author Prashant Dighe
26   * @author Brian Wing Shun Chan
27   */
28  public class ScrollableResultsImpl implements ScrollableResults {
29  
30      public ScrollableResultsImpl(List<?> results) {
31          _results = results;
32          _last = _results.size();
33      }
34  
35      public boolean first() throws ORMException {
36          if (_results.isEmpty()) {
37              return false;
38          }
39  
40          _current = 1;
41  
42          return true;
43      }
44  
45      public Object[] get() throws ORMException {
46          Object[] result = null;
47  
48          Object object = _results.get(_current - 1);
49  
50          if (object instanceof Object[]) {
51              result = (Object[])object;
52          }
53          else {
54              result = new Object[] {object};
55          }
56  
57          return result;
58      }
59  
60      public Object get(int i) throws ORMException {
61          Object result = null;
62  
63          Object object = _results.get(_current - 1);
64  
65          if (object instanceof Object[]) {
66              result = ((Object[])object)[i];
67          }
68          else {
69              result = object;
70          }
71  
72          return result;
73      }
74  
75      public boolean last() throws ORMException {
76          if (_results.isEmpty()) {
77              return false;
78          }
79  
80          _current = _last;
81  
82          return true;
83      }
84  
85      public boolean next() throws ORMException {
86          if (_current == _last) {
87              return false;
88          }
89  
90          _current++;
91  
92          return true;
93      }
94  
95      public boolean previous() throws ORMException {
96          if (_current == 1) {
97              return false;
98          }
99  
100         _current--;
101 
102         return true;
103     }
104 
105     public boolean scroll(int i) throws ORMException {
106         if (_current + i < 1 || _current + i > _last ) {
107             return false;
108         }
109 
110         _current += i;
111 
112         return true;
113     }
114 
115     private int _current = 0;
116     private int _last = 0;
117     private List<?> _results;
118 
119 }