001    /**
002     * Copyright (c) 2000-2013 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            @Override
034            public boolean first() throws ORMException {
035                    if (_results.isEmpty()) {
036                            return false;
037                    }
038    
039                    _current = 1;
040    
041                    return true;
042            }
043    
044            @Override
045            public Object[] get() throws ORMException {
046                    Object[] result = null;
047    
048                    Object object = _results.get(_current - 1);
049    
050                    if (object instanceof Object[]) {
051                            result = (Object[])object;
052                    }
053                    else {
054                            result = new Object[] {object};
055                    }
056    
057                    return result;
058            }
059    
060            @Override
061            public Object get(int i) throws ORMException {
062                    Object result = null;
063    
064                    Object object = _results.get(_current - 1);
065    
066                    if (object instanceof Object[]) {
067                            result = ((Object[])object)[i];
068                    }
069                    else {
070                            result = object;
071                    }
072    
073                    return result;
074            }
075    
076            @Override
077            public boolean last() throws ORMException {
078                    if (_results.isEmpty()) {
079                            return false;
080                    }
081    
082                    _current = _last;
083    
084                    return true;
085            }
086    
087            @Override
088            public boolean next() throws ORMException {
089                    if (_current == _last) {
090                            return false;
091                    }
092    
093                    _current++;
094    
095                    return true;
096            }
097    
098            @Override
099            public boolean previous() throws ORMException {
100                    if (_current == 1) {
101                            return false;
102                    }
103    
104                    _current--;
105    
106                    return true;
107            }
108    
109            @Override
110            public boolean scroll(int i) throws ORMException {
111                    if (((_current + i) < 1) || ((_current + i) > _last)) {
112                            return false;
113                    }
114    
115                    _current += i;
116    
117                    return true;
118            }
119    
120            private int _current = 0;
121            private int _last = 0;
122            private List<?> _results;
123    
124    }