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 @ 40559
History | View | Annotate | Download (5.84 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
package org.gvsig.fmap.dal.feature.paging.impl; |
25 |
|
26 |
import org.gvsig.tools.paging.DefaultPagingCalculator; |
27 |
|
28 |
/**
|
29 |
* Performs the calculations needed to be able to do pagination with two Sets of
|
30 |
* data which must be viewed as a single set of data. The first set is supposed
|
31 |
* to be a subset of the second one, so the size of the first set must be
|
32 |
* subtracted from the second one.
|
33 |
*
|
34 |
* @author gvSIG team
|
35 |
*/
|
36 |
public class OneSubsetOneSetPagingCalculator extends DefaultPagingCalculator { |
37 |
|
38 |
private final Sizeable firstSet; |
39 |
private final Sizeable secondSet; |
40 |
|
41 |
/**
|
42 |
* @see AbstractPagingCalculator#AbstractPagingCalculator(int).
|
43 |
* @param firstSet
|
44 |
* the first set of elements to calculate pagination for
|
45 |
* @param secondSet
|
46 |
* the second set of elements to calculate pagination for
|
47 |
*/
|
48 |
public OneSubsetOneSetPagingCalculator(Sizeable firstSet,
|
49 |
Sizeable secondSet, int maxPageSize) {
|
50 |
super(firstSet, maxPageSize);
|
51 |
this.firstSet = firstSet;
|
52 |
this.secondSet = secondSet;
|
53 |
} |
54 |
|
55 |
/**
|
56 |
* @see AbstractPagingCalculator#AbstractPagingCalculator(int, long).
|
57 |
* @param firstSet
|
58 |
* the first set of elements to calculate pagination for
|
59 |
* @param secondSet
|
60 |
* the second set of elements to calculate pagination for
|
61 |
*/
|
62 |
public OneSubsetOneSetPagingCalculator(Sizeable firstSet,
|
63 |
Sizeable secondSet, int maxPageSize, long currentPage) { |
64 |
super(firstSet, maxPageSize, currentPage);
|
65 |
this.firstSet = firstSet;
|
66 |
this.secondSet = secondSet;
|
67 |
} |
68 |
|
69 |
public long getTotalSize() { |
70 |
return secondSet.getSize();
|
71 |
} |
72 |
|
73 |
/**
|
74 |
* Returns the size of the first set.
|
75 |
*
|
76 |
* @return the first set size
|
77 |
*/
|
78 |
public long getFirstSetSize() { |
79 |
return firstSet.getSize();
|
80 |
} |
81 |
|
82 |
/**
|
83 |
* Returns the size of the second set.
|
84 |
*
|
85 |
* @return the second set size
|
86 |
*/
|
87 |
public long getSecondSetSize() { |
88 |
return secondSet.getSize() - getFirstSetSize();
|
89 |
} |
90 |
|
91 |
/**
|
92 |
* Returns the index of the first element in the page which is available
|
93 |
* into the first set, if the current page has elements on it.
|
94 |
*
|
95 |
* @return the index of the first element into the first set
|
96 |
*/
|
97 |
public long getFirstSetInitialIndex() { |
98 |
if (hasCurrentPageAnyValuesInFirstSet()) {
|
99 |
return calculateFirstSetInitialIndex();
|
100 |
} else {
|
101 |
return -1; |
102 |
} |
103 |
} |
104 |
|
105 |
private long calculateFirstSetInitialIndex() { |
106 |
return getCurrentPage() * getMaxPageSize();
|
107 |
} |
108 |
|
109 |
/**
|
110 |
* Returns the index of the first element in the page which is available
|
111 |
* into the second set, if the current page has elements on it.
|
112 |
*
|
113 |
* @return the index of the first element into the second set
|
114 |
*/
|
115 |
public long getSecondSetInitialIndex() { |
116 |
if (hasCurrentPageAnyValuesInSecondSet()) {
|
117 |
if (hasCurrentPageAnyValuesInFirstSet()) {
|
118 |
return 0; |
119 |
} else {
|
120 |
return getInitialIndex() - getFirstSetSize();
|
121 |
} |
122 |
} else {
|
123 |
return -1; |
124 |
} |
125 |
} |
126 |
|
127 |
/**
|
128 |
* Returns the number of elements of the current page into the first set.
|
129 |
*
|
130 |
* @return the number of elements of the current page into the first set
|
131 |
*/
|
132 |
public long getFirstSetHowMany() { |
133 |
if (hasCurrentPageAnyValuesInFirstSet()) {
|
134 |
return (hasCurrentPageAllValuesInFirstSet()) ? getCurrentPageSize()
|
135 |
: (getFirstSetSize() - calculateFirstSetInitialIndex()); |
136 |
} else {
|
137 |
return 0; |
138 |
} |
139 |
} |
140 |
|
141 |
/**
|
142 |
* Returns the number of elements of the current page into the second set.
|
143 |
*
|
144 |
* @return the number of elements of the current page into the second set
|
145 |
*/
|
146 |
public long getSecondSetHowMany() { |
147 |
if (hasCurrentPageAnyValuesInSecondSet()) {
|
148 |
return getLastIndex() - getFirstSetSize()
|
149 |
- getSecondSetInitialIndex() + 1;
|
150 |
} else {
|
151 |
return 0; |
152 |
} |
153 |
} |
154 |
|
155 |
/**
|
156 |
* Returns if the current page has all its values into the first set.
|
157 |
*
|
158 |
* @return if the current page has all its values into the first set
|
159 |
*/
|
160 |
protected boolean hasCurrentPageAllValuesInFirstSet() { |
161 |
return calculateFirstSetInitialIndex() + getCurrentPageSize() <= getFirstSetSize();
|
162 |
} |
163 |
|
164 |
/**
|
165 |
* Returns if the current page has any of its values into the first set.
|
166 |
*
|
167 |
* @return if the current page has any of its values into the first set
|
168 |
*/
|
169 |
public boolean hasCurrentPageAnyValuesInFirstSet() { |
170 |
return calculateFirstSetInitialIndex() < getFirstSetSize();
|
171 |
} |
172 |
|
173 |
/**
|
174 |
* Returns if the current page has any of its values into the second set.
|
175 |
*
|
176 |
* @return if the current page has any of its values into the second set
|
177 |
*/
|
178 |
public boolean hasCurrentPageAnyValuesInSecondSet() { |
179 |
return getLastIndex() >= getFirstSetSize();
|
180 |
} |
181 |
} |