001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of the Granite Data Services Platform.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or
008     *   modify it under the terms of the GNU Lesser General Public
009     *   License as published by the Free Software Foundation; either
010     *   version 2.1 of the License, or (at your option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful,
013     *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014     *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015     *   General Public License for more details.
016     *
017     *   You should have received a copy of the GNU Lesser General Public
018     *   License along with this library; if not, write to the Free Software
019     *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020     *   USA, or see <http://www.gnu.org/licenses/>.
021     */
022    package org.granite.spring.data;
023    
024    import org.springframework.data.domain.Pageable;
025    import org.springframework.data.domain.Sort;
026    
027    public class OffsetPageRequest implements Pageable {
028        
029        private int offset;
030        private int pageSize;
031        private Sort sort;
032        
033        public OffsetPageRequest(int offset, int pageSize, Sort sort) {
034            this.offset = offset;
035            this.pageSize = pageSize;
036            this.sort = sort;
037        }
038    
039        public int getOffset() {
040            return offset;
041        }
042    
043        public int getPageNumber() {
044            return pageSize > 0 ? offset / pageSize : 0;
045        }
046    
047        public int getPageSize() {
048            return pageSize;
049        }
050    
051        public Sort getSort() {
052            return sort;
053        }
054    
055            public boolean hasPrevious() {
056                    return offset > 0;
057            }
058    
059            public Pageable first() {
060                    return new OffsetPageRequest(0, pageSize, sort);
061            }
062    
063            public Pageable previousOrFirst() {
064                    if (offset > pageSize)
065                            return new OffsetPageRequest(offset-pageSize, pageSize, sort);
066                    else
067                            return new OffsetPageRequest(0, pageSize, sort);
068            }
069            
070            public Pageable next() {
071                    return new OffsetPageRequest(offset+pageSize, pageSize, sort);
072            }
073    }