001    /**
002     * Copyright (c) 2000-present 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.interval;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    
019    /**
020     * @author Jonathan McCann
021     * @author Sergio Gonz??lez
022     * @author Preston Crary
023     */
024    public class IntervalActionProcessor<T> {
025    
026            public static final int INTERVAL_DEFAULT = 100;
027    
028            public IntervalActionProcessor(int total) {
029                    if (total < 0) {
030                            throw new IllegalArgumentException(
031                                    "Total " + total + " is less than zero");
032                    }
033    
034                    _total = total;
035    
036                    _interval = INTERVAL_DEFAULT;
037            }
038    
039            public IntervalActionProcessor(int total, int interval) {
040                    if (total < 0) {
041                            throw new IllegalArgumentException(
042                                    "Total " + total + " is less than zero");
043                    }
044    
045                    if (interval <= 0) {
046                            throw new IllegalArgumentException(
047                                    "Interval " + interval + " is less than or equal to zero");
048                    }
049    
050                    _total = total;
051                    _interval = interval;
052            }
053    
054            public void incrementStart() {
055                    _start++;
056            }
057    
058            public void incrementStart(int increment) {
059                    if (increment < 0) {
060                            throw new IllegalArgumentException(
061                                    "Increment " + increment + " is less than zero");
062                    }
063    
064                    _start += increment;
065            }
066    
067            public T performIntervalActions() throws PortalException {
068                    if (_total == 0) {
069                            return null;
070                    }
071    
072                    int pages = _total / _interval;
073    
074                    for (int i = 0; i <= pages; i++) {
075                            int end = _start + _interval;
076    
077                            if (end > _total) {
078                                    end = _total;
079                            }
080    
081                            T result = performIntervalActions(_start, end);
082    
083                            if (result != null) {
084                                    return result;
085                            }
086                    }
087    
088                    return null;
089            }
090    
091            public void setPerformIntervalActionMethod(
092                    PerformIntervalActionMethod<T> performIntervalActionMethod) {
093    
094                    _performIntervalActionMethod = performIntervalActionMethod;
095            }
096    
097            public interface PerformIntervalActionMethod<T> {
098    
099                    public T performIntervalAction(int start, int end)
100                            throws PortalException;
101    
102            }
103    
104            protected T performIntervalActions(int start, int end)
105                    throws PortalException {
106    
107                    if (_performIntervalActionMethod != null) {
108                            return _performIntervalActionMethod.performIntervalAction(
109                                    start, end);
110                    }
111    
112                    return null;
113            }
114    
115            private final int _interval;
116            private PerformIntervalActionMethod<T> _performIntervalActionMethod;
117            private int _start;
118            private final int _total;
119    
120    }