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.kernel.dao.search;
016    
017    /**
018     * @author Roberto D??az
019     */
020    public class SearchPaginationUtil {
021    
022            public static int[] calculateStartAndEnd(int cur, int delta) {
023                    int start = 0;
024    
025                    if (cur > 0) {
026                            start = (cur - 1) * delta;
027                    }
028    
029                    int end = start + delta;
030    
031                    return new int[] {start, end};
032            }
033    
034            public static int[] calculateStartAndEnd(int start, int end, int total) {
035                    if (total <= 0) {
036                            return new int[] {0, 0};
037                    }
038    
039                    int[] startAndEnd = {start, end};
040    
041                    int delta = end - start;
042    
043                    if (delta < 0) {
044                            return new int[] {0, 0};
045                    }
046    
047                    while ((start > 0) && (start >= total)) {
048                            int cur = start / delta;
049    
050                            startAndEnd = calculateStartAndEnd(cur, delta);
051    
052                            start = startAndEnd[0];
053                    }
054    
055                    end = startAndEnd[1];
056    
057                    if (end > total) {
058                            startAndEnd[1] = total;
059                    }
060    
061                    return startAndEnd;
062            }
063    
064    }