Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / feature / paging / impl / AbstractPagingCalculator.java @ 33378

History | View | Annotate | Download (3.31 KB)

1
/* gvSIG. Geographic Information System of the Valencian Government
2
 *
3
 * Copyright (C) 2007-2008 Infrastructures and Transports Department
4
 * of the Valencian Government (CIT)
5
 * 
6
 * This program is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU General Public License
8
 * as published by the Free Software Foundation; either version 2
9
 * of the License, or (at your option) any later version.
10
 * 
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 * 
16
 * You should have received a copy of the GNU General Public License
17
 * along with this program; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
19
 * MA  02110-1301, USA.
20
 * 
21
 */
22

    
23
package org.gvsig.fmap.dal.feature.paging.impl;
24

    
25
import org.slf4j.Logger;
26
import org.slf4j.LoggerFactory;
27

    
28
/**
29
 * Abstract base implementation.
30
 * 
31
 * @author 2010- C?sar Ordi?ana - gvSIG team
32
 */
33
public abstract class AbstractPagingCalculator implements PagingCalculator {
34

    
35
        private static final Logger LOG =
36
                        LoggerFactory.getLogger(AbstractPagingCalculator.class);
37

    
38
        private int maxPageSize;
39
        private long currentPage;
40

    
41
        /**
42
         * Creates a new {@link AbstractPagingCalculator}, setting the current page
43
         * to the first one.
44
         * 
45
         * @param maxPageSize
46
         *            the maximum page size
47
         */
48
        public AbstractPagingCalculator(int maxPageSize) {
49
                this(maxPageSize, 0);
50
        }
51

    
52
        /**
53
         * Creates a new {@link AbstractPagingCalculator} .
54
         * 
55
         * @param maxPageSize
56
         *            the maximum page size
57
         * @param currentPage
58
         *            the current page
59
         */
60
        public AbstractPagingCalculator(int maxPageSize, long currentPage) {
61
                this.maxPageSize = maxPageSize;
62
                this.currentPage = currentPage;
63
        }
64

    
65
        public int getMaxPageSize() {
66
                return maxPageSize;
67
        }
68

    
69
        public void setMaxPageSize(int maxPageSize) {
70
                if (maxPageSize < 0) {
71
                        throw new IndexOutOfBoundsException(
72
                                        "Error, unable to set the max page size to a "
73
                                                        + "negative value: " + maxPageSize);
74
                }
75
                this.maxPageSize = maxPageSize;
76
        }
77

    
78
        public long getCurrentPage() {
79
                return currentPage;
80
        }
81

    
82
        public void setCurrentPage(long currentPage) {
83
                if (LOG.isDebugEnabled()) {
84
                        LOG.debug("Setting current page to: {}", Long.toString(currentPage));
85
                }
86

    
87
                if (currentPage < 0) {
88
                        throw new IndexOutOfBoundsException(
89
                                        "Error, unable to set current page to a "
90
                                                        + "negative value: " + currentPage);
91
                }
92
                if (currentPage >= getNumPages()) {
93
                        throw new IndexOutOfBoundsException(
94
                                        "Error, unable to set current page to the page num. "
95
                                                        + currentPage + ", as we have only "
96
                                                        + getNumPages() + " pages of data");
97
                }
98

    
99
                this.currentPage = currentPage;
100
        }
101

    
102
        public long getInitialIndex() {
103
                return getCurrentPage() * getMaxPageSize();
104
        }
105

    
106
        protected long getLastIndex() {
107
                return getInitialIndex() + getCurrentPageSize() - 1;
108
        }
109

    
110
        public long getNumPages() {
111
                return ((long) Math.floor(getTotalSize() / (long) getMaxPageSize())) + 1;
112
        }
113

    
114
        public int getCurrentPageSize() {
115
                long currentPage = getCurrentPage();
116
                if (currentPage < (getNumPages() - 1)) {
117
                        return getMaxPageSize();
118
                } else {
119
                        return (int) (getTotalSize() - (currentPage * getMaxPageSize()));
120
                }
121
        }
122
}