001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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    }