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 @ 6323

History | View | Annotate | Download (4.29 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 pageManager
34
     */
35
    public AbstractPaginatedBand(int rows, int columns, NoData noData, BandPageManager pageManager) {
36
        this.rows = rows;
37
        this.columns = columns;
38
        this.loaded=false;
39
        calculateRowsPerPage();
40

    
41
        if (noData == null) {
42
            this.noData = BufferLocator.getBufferManager().createNoData(null, null);
43
        } else {
44
            this.noData = noData;
45
        }
46
        this.pageManager = pageManager;
47
    }
48

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

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

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

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

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

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

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

    
126
    protected abstract int getDataSize();
127

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

    
136

    
137
    @Override
138
    public boolean isPaginated() {
139
        return true;
140
    }
141

    
142
    @Override
143
    public BandInfo getBandInfo() {
144
        return this.pageManager == null ? null : this.pageManager.getBandInfo();
145
    }
146

    
147
}