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

History | View | Annotate | Download (6.69 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.observer.Observer;
32
import org.gvsig.tools.visitor.Visitor;
33

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

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

    
46
    private final DynObjectSet[] innerSets;
47

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
170
    public void addObserver(Observer o) {
171
        throw new UnsupportedOperationException("This DynObjectSet "
172
            + "implementation does not support edition, so it doesn't "
173
            + "provide notifications");
174
    }
175

    
176
    public void deleteObserver(Observer o) {
177
        throw new UnsupportedOperationException("This DynObjectSet "
178
            + "implementation does not support edition, so it doesn't "
179
            + "provide notifications");
180
    }
181

    
182
    public void deleteObservers() {
183
        throw new UnsupportedOperationException("This DynObjectSet "
184
            + "implementation does not support edition, so it doesn't "
185
            + "provide notifications");
186
    }
187

    
188
    public boolean isDeleteEnabled() {
189
        return false;
190
    }
191

    
192
    public void delete(DynObject dynObject) throws BaseException {
193
        throw new UnsupportedOperationException("This DynObjectSet "
194
            + "implementation does not support DynObject deletion");
195
    }
196

    
197
    public boolean isUpdateEnabled() {
198
        return false;
199
    }
200

    
201
    public void update(DynObject dynObject) throws BaseException {
202
        throw new UnsupportedOperationException("This DynObjectSet "
203
            + "implementation does not support DynObject update");
204
    }
205

    
206
}