Statistics
| Revision:

gvsig-tools / org.gvsig.tools / library / trunk / org.gvsig.tools / org.gvsig.tools.lib / src / main / java / org / gvsig / tools / dynobject / impl / MultiDynObjectSet.java @ 261

History | View | Annotate | Download (5.42 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.tools.dynobject.impl;
23

    
24
import org.slf4j.Logger;
25
import org.slf4j.LoggerFactory;
26

    
27
import org.gvsig.tools.dispose.DisposableIterator;
28
import org.gvsig.tools.dynobject.DynObject;
29
import org.gvsig.tools.dynobject.DynObjectSet;
30
import org.gvsig.tools.exception.BaseException;
31
import org.gvsig.tools.visitor.Visitor;
32

    
33
/**
34
 * Fa?ade for a list of {@link DynObjectSet}s as if they were an only contiguous
35
 * set of {@link DynObject}s.
36
 * 
37
 * @author gvSIG Team
38
 * @version $Id$
39
 */
40
public class MultiDynObjectSet implements DynObjectSet {
41

    
42
    private static final Logger LOG = LoggerFactory
43
        .getLogger(MultiDynObjectSet.class);
44

    
45
    private final DynObjectSet[] innerSets;
46

    
47
    /**
48
     * Creates a new {@link MultiDynObjectSet} with the list of
49
     * {@link DynObjectSet}s to fa?ade.
50
     */
51
    public MultiDynObjectSet(DynObjectSet[] innerSets) {
52
        this.innerSets = innerSets == null ? new DynObjectSet[0] : innerSets;
53
    }
54

    
55
    public void dispose() {
56
        for (int i = 0; i < innerSets.length; i++) {
57
            innerSets[i].dispose();
58
        }
59
    }
60

    
61
    public void accept(Visitor visitor, long firstValueIndex)
62
        throws BaseException {
63

    
64
        // Find the set where the global index points to
65
        int currentSet = 0;
66
        long currentIndex = 0;
67
        while (currentIndex + innerSets[currentSet].getSize() <= firstValueIndex) {
68
            currentIndex += innerSets[currentSet].getSize();
69
            currentSet++;
70
        }
71
        // Get the relative index into the current set
72
        long firstIndexFromCurrentSet = firstValueIndex - currentIndex;
73

    
74
        // Visit the current set beginning from the relative index
75
        innerSets[currentSet].accept(visitor, firstIndexFromCurrentSet);
76

    
77
        // Finally, visit the remaining sets fully.
78
        for (int i = currentSet; i < innerSets.length; i++) {
79
            innerSets[i].accept(visitor);
80
        }
81
    }
82

    
83
    public void accept(Visitor visitor) throws BaseException {
84
        accept(visitor, 0);
85
    }
86

    
87
    public long getSize() throws BaseException {
88
        long size = 0;
89
        for (int i = 0; i < innerSets.length; i++) {
90
            size += innerSets[i].getSize();
91
        }
92
        return size;
93
    }
94

    
95
    public DisposableIterator iterator(long index) throws BaseException {
96
        return new MultiDynObjectSetDisposableIterator(innerSets);
97
    }
98

    
99
    public DisposableIterator iterator() throws BaseException {
100
        return iterator(0);
101
    }
102

    
103
    public boolean isEmpty() throws BaseException {
104
        for (int i = 0; i < innerSets.length; i++) {
105
            if (!innerSets[i].isEmpty()) {
106
                return false;
107
            }
108
        }
109
        return true;
110
    }
111

    
112
    /**
113
     * @author gvSIG Team
114
     * @version $Id$
115
     * 
116
     */
117
    public static class MultiDynObjectSetDisposableIterator implements
118
        DisposableIterator {
119

    
120
        private DisposableIterator currentIterator = null;
121
        private int currentSet = 0;
122
        private final DynObjectSet[] innerSets;
123

    
124
        public MultiDynObjectSetDisposableIterator(DynObjectSet[] innerSets)
125
            throws BaseException {
126
            this.innerSets = innerSets;
127
            if (currentIterator == null && currentSet < innerSets.length) {
128
                currentIterator = innerSets[currentSet].iterator();
129
            }
130
        }
131

    
132
        public void dispose() {
133
            if (currentIterator != null) {
134
                currentIterator.dispose();
135
            }
136
        }
137

    
138
        private DisposableIterator getCurrentIterator() {
139
            while (!currentIterator.hasNext()
140
                && currentSet < innerSets.length - 1) {
141
                currentSet++;
142
                try {
143
                    currentIterator = innerSets[currentSet].iterator();
144
                } catch (BaseException e) {
145
                    LOG.error(
146
                        "Could no get the iterator of the internal DynObjectSet num. "
147
                            + currentSet + ": " + innerSets[currentSet], e);
148
                }
149
            }
150
            return currentIterator;
151
        }
152

    
153
        public boolean hasNext() {
154
            DisposableIterator currentIterator = getCurrentIterator();
155
            return currentIterator == null ? false : currentIterator.hasNext();
156
        }
157

    
158
        public Object next() {
159
            DisposableIterator currentIterator = getCurrentIterator();
160
            return currentIterator == null ? null : currentIterator.next();
161
        }
162

    
163
        public void remove() {
164
            throw new UnsupportedOperationException(
165
                "Unable to remove on composed DynObjectSet iterator");
166
        }
167
    }
168

    
169
}