Statistics
| Revision:

svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.impl / src / main / java / org / gvsig / fmap / dal / feature / paging / impl / OneSubsetOneSetPagingCalculator.java @ 40435

History | View | Annotate | Download (5.79 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
package org.gvsig.fmap.dal.feature.paging.impl;
23

    
24
import org.gvsig.tools.paging.DefaultPagingCalculator;
25

    
26
/**
27
 * Performs the calculations needed to be able to do pagination with two Sets of
28
 * data which must be viewed as a single set of data. The first set is supposed
29
 * to be a subset of the second one, so the size of the first set must be
30
 * subtracted from the second one.
31
 * 
32
 * @author gvSIG team
33
 */
34
public class OneSubsetOneSetPagingCalculator extends DefaultPagingCalculator {
35

    
36
    private final Sizeable firstSet;
37
    private final Sizeable secondSet;
38

    
39
    /**
40
     * @see AbstractPagingCalculator#AbstractPagingCalculator(int).
41
     * @param firstSet
42
     *            the first set of elements to calculate pagination for
43
     * @param secondSet
44
     *            the second set of elements to calculate pagination for
45
     */
46
    public OneSubsetOneSetPagingCalculator(Sizeable firstSet,
47
        Sizeable secondSet, int maxPageSize) {
48
        super(firstSet, maxPageSize);
49
        this.firstSet = firstSet;
50
        this.secondSet = secondSet;
51
    }
52

    
53
    /**
54
     * @see AbstractPagingCalculator#AbstractPagingCalculator(int, long).
55
     * @param firstSet
56
     *            the first set of elements to calculate pagination for
57
     * @param secondSet
58
     *            the second set of elements to calculate pagination for
59
     */
60
    public OneSubsetOneSetPagingCalculator(Sizeable firstSet,
61
        Sizeable secondSet, int maxPageSize, long currentPage) {
62
        super(firstSet, maxPageSize, currentPage);
63
        this.firstSet = firstSet;
64
        this.secondSet = secondSet;
65
    }
66

    
67
    public long getTotalSize() {
68
        return secondSet.getSize();
69
    }
70

    
71
    /**
72
     * Returns the size of the first set.
73
     * 
74
     * @return the first set size
75
     */
76
    public long getFirstSetSize() {
77
        return firstSet.getSize();
78
    }
79

    
80
    /**
81
     * Returns the size of the second set.
82
     * 
83
     * @return the second set size
84
     */
85
    public long getSecondSetSize() {
86
        return secondSet.getSize() - getFirstSetSize();
87
    }
88

    
89
    /**
90
     * Returns the index of the first element in the page which is available
91
     * into the first set, if the current page has elements on it.
92
     * 
93
     * @return the index of the first element into the first set
94
     */
95
    public long getFirstSetInitialIndex() {
96
        if (hasCurrentPageAnyValuesInFirstSet()) {
97
            return calculateFirstSetInitialIndex();
98
        } else {
99
            return -1;
100
        }
101
    }
102

    
103
    private long calculateFirstSetInitialIndex() {
104
        return getCurrentPage() * getMaxPageSize();
105
    }
106

    
107
    /**
108
     * Returns the index of the first element in the page which is available
109
     * into the second set, if the current page has elements on it.
110
     * 
111
     * @return the index of the first element into the second set
112
     */
113
    public long getSecondSetInitialIndex() {
114
        if (hasCurrentPageAnyValuesInSecondSet()) {
115
            if (hasCurrentPageAnyValuesInFirstSet()) {
116
                return 0;
117
            } else {
118
                return getInitialIndex() - getFirstSetSize();
119
            }
120
        } else {
121
            return -1;
122
        }
123
    }
124

    
125
    /**
126
     * Returns the number of elements of the current page into the first set.
127
     * 
128
     * @return the number of elements of the current page into the first set
129
     */
130
    public long getFirstSetHowMany() {
131
        if (hasCurrentPageAnyValuesInFirstSet()) {
132
            return (hasCurrentPageAllValuesInFirstSet()) ? getCurrentPageSize()
133
                : (getFirstSetSize() - calculateFirstSetInitialIndex());
134
        } else {
135
            return 0;
136
        }
137
    }
138

    
139
    /**
140
     * Returns the number of elements of the current page into the second set.
141
     * 
142
     * @return the number of elements of the current page into the second set
143
     */
144
    public long getSecondSetHowMany() {
145
        if (hasCurrentPageAnyValuesInSecondSet()) {
146
            return getLastIndex() - getFirstSetSize()
147
                - getSecondSetInitialIndex() + 1;
148
        } else {
149
            return 0;
150
        }
151
    }
152

    
153
    /**
154
     * Returns if the current page has all its values into the first set.
155
     * 
156
     * @return if the current page has all its values into the first set
157
     */
158
    protected boolean hasCurrentPageAllValuesInFirstSet() {
159
        return calculateFirstSetInitialIndex() + getCurrentPageSize() <= getFirstSetSize();
160
    }
161

    
162
    /**
163
     * Returns if the current page has any of its values into the first set.
164
     * 
165
     * @return if the current page has any of its values into the first set
166
     */
167
    public boolean hasCurrentPageAnyValuesInFirstSet() {
168
        return calculateFirstSetInitialIndex() < getFirstSetSize();
169
    }
170

    
171
    /**
172
     * Returns if the current page has any of its values into the second set.
173
     * 
174
     * @return if the current page has any of its values into the second set
175
     */
176
    public boolean hasCurrentPageAnyValuesInSecondSet() {
177
        return getLastIndex() >= getFirstSetSize();
178
    }
179
}