001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.dao.orm.jpa;
016    
017    import com.liferay.portal.kernel.dao.orm.ORMException;
018    import com.liferay.portal.kernel.dao.orm.ScrollableResults;
019    
020    import java.util.List;
021    
022    /**
023     * @author Prashant Dighe
024     * @author Brian Wing Shun Chan
025     */
026    public class ScrollableResultsImpl implements ScrollableResults {
027    
028            public ScrollableResultsImpl(List<?> results) {
029                    _results = results;
030                    _last = _results.size();
031            }
032    
033            public boolean first() throws ORMException {
034                    if (_results.isEmpty()) {
035                            return false;
036                    }
037    
038                    _current = 1;
039    
040                    return true;
041            }
042    
043            public Object[] get() throws ORMException {
044                    Object[] result = null;
045    
046                    Object object = _results.get(_current - 1);
047    
048                    if (object instanceof Object[]) {
049                            result = (Object[])object;
050                    }
051                    else {
052                            result = new Object[] {object};
053                    }
054    
055                    return result;
056            }
057    
058            public Object get(int i) throws ORMException {
059                    Object result = null;
060    
061                    Object object = _results.get(_current - 1);
062    
063                    if (object instanceof Object[]) {
064                            result = ((Object[])object)[i];
065                    }
066                    else {
067                            result = object;
068                    }
069    
070                    return result;
071            }
072    
073            public boolean last() throws ORMException {
074                    if (_results.isEmpty()) {
075                            return false;
076                    }
077    
078                    _current = _last;
079    
080                    return true;
081            }
082    
083            public boolean next() throws ORMException {
084                    if (_current == _last) {
085                            return false;
086                    }
087    
088                    _current++;
089    
090                    return true;
091            }
092    
093            public boolean previous() throws ORMException {
094                    if (_current == 1) {
095                            return false;
096                    }
097    
098                    _current--;
099    
100                    return true;
101            }
102    
103            public boolean scroll(int i) throws ORMException {
104                    if (_current + i < 1 || _current + i > _last ) {
105                            return false;
106                    }
107    
108                    _current += i;
109    
110                    return true;
111            }
112    
113            private int _current = 0;
114            private int _last = 0;
115            private List<?> _results;
116    
117    }