Statistics
| Revision:

gvsig-raster / org.gvsig.raster / branches / org.gvsig.raster.2.4 / org.gvsig.raster / org.gvsig.raster.lib / org.gvsig.raster.lib.buffer / org.gvsig.raster.lib.buffer.impl / src / main / java / org / gvsig / raster / lib / buffer / impl / AbstractPaginatedBand.java @ 6303

History | View | Annotate | Download (4.33 KB)

1
package org.gvsig.raster.lib.buffer.impl;
2

    
3
import java.io.IOException;
4
import java.nio.Buffer;
5

    
6
import org.gvsig.raster.lib.buffer.api.Band;
7
import org.gvsig.raster.lib.buffer.api.BandInfo;
8
import org.gvsig.raster.lib.buffer.api.BandNotification;
9
import org.gvsig.raster.lib.buffer.api.BandPageManager;
10
import org.gvsig.raster.lib.buffer.api.BufferLocator;
11
import org.gvsig.raster.lib.buffer.api.NoData;
12
import org.gvsig.raster.lib.buffer.impl.exceptions.CopyFromBandException;
13

    
14
/**
15
 * @author fdiaz
16
 *
17
 */
18
public abstract class AbstractPaginatedBand extends AbstractBand implements Band{
19

    
20
    protected Buffer data;
21
    protected int firstRowOfPage;
22
    protected int rowsPerPage;
23
    protected BandPageManager pageManager;
24
    private boolean loaded;
25

    
26

    
27
    protected static final int MAX_PREFERED_SIZE = 10485760; // 10MB;
28

    
29
    /**
30
     * @param rows
31
     * @param columns
32
     * @param noData
33
     * @param rowsPerPage
34
     * @param pageManager
35
     */
36
    public AbstractPaginatedBand(int rows, int columns, NoData noData, BandPageManager pageManager) {
37
        this.rows = rows;
38
        this.columns = columns;
39
        this.loaded=false;
40
        calculateRowsPerPage();
41
        
42
        if (noData == null) {
43
            this.noData = BufferLocator.getBufferManager().createNoData(null, null);
44
        } else {
45
            this.noData = noData;
46
        }
47
        this.pageManager = pageManager;
48
    }
49

    
50
    private void calculateRowsPerPage(){
51
        rowsPerPage = MAX_PREFERED_SIZE/(this.columns*getDataSize());
52
        if(rowsPerPage<1){
53
            rowsPerPage = 1;
54
        }
55
    }
56

    
57
    /**
58
     * @param value
59
     * @return
60
     */
61
    protected Object nullValueToNoData(Object value) {
62
        if (value == null) {
63
            if (getNoData().isDefined()) {
64
                value = getNoData().getValue();
65
            } else {
66
                // Do nothing, no data value is undefined
67
                return null;
68
            }
69
        }
70
        return value;
71
    }
72

    
73
    @Override
74
    public void copyFrom(Band source) throws CopyFromBandException {
75
        doCopyFrom(source);
76
        notifyObservers(new DefaultBandNotification(BandNotification.COPY_FROM, new Object[]{source}));
77
    }
78

    
79
    protected void doCopyFrom(Band source) throws CopyFromBandException {
80
        if (this.getColumns() != source.getColumns() || this.getRows() != source.getRows()
81
            || this.getDataType()!=source.getDataType() ) {
82
            throw new CopyFromBandException(source, this);
83
        }
84
        Object rowBuffer = this.createRowBuffer();
85
        for(int row = 0; row<=this.rows; row++){
86
            source.fetchRow(row, rowBuffer);
87
            this.putRow(row, rowBuffer);
88
        }
89
    }
90

    
91
    protected void loadPage(int row) {
92
        if (loaded && row >= firstRowOfPage
93
            && row < firstRowOfPage + rowsPerPage) {
94
            return;
95
        }
96
        loaded=false;
97

    
98
        saveCurrentPage();
99
        int currentPage = row / rowsPerPage; //Divisi?n entera
100
        firstRowOfPage = currentPage * rowsPerPage;
101
        try {
102
            int rowsInPage = rowsPerPage;
103
            if(firstRowOfPage + rowsPerPage > this.rows){
104
                rowsInPage = this.rows - firstRowOfPage;
105
            }
106
            this.pageManager.load(data, firstRowOfPage, rowsInPage, this.getDataType());
107
            loaded=true;
108
        } catch (IOException e) {
109
            throw new RuntimeException("Can't save current page", e);
110
        }
111
    }
112

    
113
    protected void saveCurrentPage() {
114
        try {
115
            int rowsInPage = rowsPerPage;
116
            if(firstRowOfPage + rowsPerPage > this.rows){
117
                rowsInPage = this.rows - firstRowOfPage;
118
            }
119
            this.pageManager.save(data, firstRowOfPage, rowsInPage, this.getDataType());
120
        } catch (UnsupportedOperationException e) {
121
            // Do nothing, operation not supported
122
        } catch (IOException e) {
123
            throw new RuntimeException("Can't save current page", e);
124
        }
125
    }
126

    
127
    protected abstract int getDataSize();
128

    
129
    @Override
130
    public boolean isReadOnly() {
131
        if(this.pageManager==null){
132
            return false;
133
        }
134
        return !this.pageManager.isSupportedSave();
135
    }
136

    
137

    
138
    @Override
139
    public boolean isPaginated() {
140
        return true;
141
    }
142
    
143
    @Override
144
    public BandInfo getBandInfo() {
145
        return this.pageManager == null ? null : this.pageManager.getBandInfo();
146
    }
147

    
148
}