Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / DefaultDynObjectPagingHelper.java @ 1860

History | View | Annotate | Download (6.16 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 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
 * 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.tools.dynobject.impl;
25

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

    
29
import org.gvsig.tools.dispose.impl.AbstractDisposable;
30
import org.gvsig.tools.dynobject.DynObject;
31
import org.gvsig.tools.dynobject.DynObjectPagingHelper;
32
import org.gvsig.tools.dynobject.DynObjectSet;
33
import org.gvsig.tools.exception.BaseException;
34
import org.gvsig.tools.paging.DefaultPagingCalculator;
35
import org.gvsig.tools.paging.PagingCalculator;
36
import org.gvsig.tools.visitor.VisitCanceledException;
37
import org.gvsig.tools.visitor.Visitor;
38

    
39
/**
40
 * Default {@link DynObjectPagingHelper} implementation.
41
 * 
42
 * @author gvSIG Team
43
 * @version $Id$
44
 */
45
public class DefaultDynObjectPagingHelper extends AbstractDisposable implements
46
    DynObjectPagingHelper {
47

    
48
    private static final Logger LOG = LoggerFactory
49
        .getLogger(DefaultDynObjectPagingHelper.class);
50

    
51
    private DynObjectSet set;
52
    private DynObject[] values;
53
    private PagingCalculator calculator;
54

    
55
    public DefaultDynObjectPagingHelper() {
56
        // Nothing to do
57
    }
58

    
59
    public DefaultDynObjectPagingHelper(DynObjectSet set) throws BaseException {
60
        this(set, DEFAULT_PAGE_SIZE);
61
    }
62

    
63
    public DefaultDynObjectPagingHelper(DynObjectSet set, int pageSize)
64
        throws BaseException {
65
        setDynObjectSet(set, pageSize);
66
    }
67

    
68
    protected void setDynObjectSet(final DynObjectSet set, int pageSize)
69
        throws BaseException {
70
        this.set = set;
71
        setDefaultCalculator(new Sizeable() {
72

    
73
            public long getSize() {
74
                try {
75
                    return set.getSize();
76
                } catch (BaseException e) {
77
                    LOG.error("Error getting the size of the DynObjectSet: "
78
                        + set, e);
79
                    return 0l;
80
                }
81
            }
82
        }, pageSize);
83
    }
84

    
85
    public DynObject getDynObjectAt(long index) throws BaseException {
86
        // Check if we have currently loaded the viewed page data,
87
        // or we need to load a new one
88
        long pageForIndex = (long) Math.floor(index / getMaxPageSize());
89

    
90
        if (pageForIndex != getCurrentPage()) {
91
            setCurrentPage(pageForIndex);
92
        }
93

    
94
        long positionForIndex = index - (getCurrentPage() * getMaxPageSize());
95

    
96
        return values[(int) positionForIndex];
97
    }
98

    
99
    public DynObject[] getCurrentPageDynObjects() {
100
        return values;
101
    }
102

    
103
    public DynObjectSet getDynObjectSet() {
104
        return set;
105
    }
106

    
107
    public void reloadCurrentPage() throws BaseException {
108
        if (getCurrentPage() > -1) {
109
            loadCurrentPageData();
110
        }
111
    }
112

    
113
    protected void loadCurrentPageData() throws BaseException {
114
        final int currentPageSize = getCurrentPageSize();
115
        final DynObject[] values = new DynObject[currentPageSize];
116

    
117
        long t1 = 0;
118
        if (LOG.isTraceEnabled()) {
119
            t1 = System.currentTimeMillis();
120
        }
121

    
122
        long initialIndex = getInitialIndex();
123
        final int pageSize = getCurrentPageSize();
124

    
125
        if (LOG.isDebugEnabled()) {
126
            LOG.debug("Loading {} DynObjects starting at position {}",
127
                new Integer(pageSize), new Long(initialIndex));
128
        }
129

    
130
        set.accept(new Visitor() {
131

    
132
            private int i = 0;
133

    
134
            public void visit(Object obj) throws VisitCanceledException,
135
                BaseException {
136
                if (i >= pageSize) {
137
                    throw new VisitCanceledException();
138
                }
139
                values[i] = (DynObject) obj;
140
                i++;
141
            }
142
        }, initialIndex);
143

    
144
        if (LOG.isTraceEnabled()) {
145
            long t2 = System.currentTimeMillis();
146
            LOG.trace("Time to load {} DynObjects: {} ms", new Integer(
147
                currentPageSize), new Long(t2 - t1));
148
        }
149

    
150
        this.values = values;
151
    }
152

    
153
    public void reload() throws BaseException {
154
        reloadCurrentPage();
155
    }
156

    
157
    protected void doDispose() throws BaseException {
158
        set.dispose();
159
    }
160

    
161
    protected PagingCalculator getCalculator() {
162
        return calculator;
163
    }
164

    
165
    protected void setCalculator(PagingCalculator calculator)
166
        throws BaseException {
167
        this.calculator = calculator;
168
        loadCurrentPageData();
169
    }
170

    
171
    protected void setDefaultCalculator(Sizeable sizeable, int pageSize)
172
        throws BaseException {
173
        setCalculator(new DefaultPagingCalculator(sizeable, pageSize));
174
    }
175

    
176
    public int getMaxPageSize() {
177
        return calculator == null ? DEFAULT_PAGE_SIZE : calculator
178
            .getMaxPageSize();
179
    }
180

    
181
    public void setMaxPageSize(int pageSize) throws BaseException {
182
        calculator.setMaxPageSize(pageSize);
183
        reloadCurrentPage();
184
    }
185

    
186
    public long getCurrentPage() {
187
        return calculator.getCurrentPage();
188
    }
189

    
190
    public void setCurrentPage(long page) throws BaseException {
191
        calculator.setCurrentPage(page);
192
        loadCurrentPageData();
193
    }
194

    
195
    public long getInitialIndex() {
196
        return calculator.getInitialIndex();
197
    }
198

    
199
    public long getNumPages() {
200
        return calculator.getNumPages();
201
    }
202

    
203
    public long getTotalSize() {
204
        return calculator.getTotalSize();
205
    }
206

    
207
    public int getCurrentPageSize() {
208
        return calculator.getCurrentPageSize();
209
    }
210

    
211
}