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 / operations / DefaultOperationList.java @ 8600

History | View | Annotate | Download (9.18 KB)

1
/* gvSIG. Desktop Geographic Information System.
2
 *
3
 * Copyright ? 2007-2017 gvSIG Association
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License
7
 * as published by the Free Software Foundation; either version 2
8
 * of the License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA  02110-1301, USA.
19
 *
20
 * For any additional information, do not hesitate to contact us
21
 * at info AT gvsig.com, or visit our website www.gvsig.com.
22
 */
23
package org.gvsig.raster.lib.buffer.impl.operations;
24

    
25
import java.util.ArrayList;
26
import java.util.Collection;
27
import java.util.Iterator;
28
import java.util.List;
29
import java.util.ListIterator;
30

    
31
import org.gvsig.raster.lib.buffer.api.Buffer;
32
import org.gvsig.raster.lib.buffer.api.BufferLocator;
33
import org.gvsig.raster.lib.buffer.api.exceptions.BufferOperationException;
34
import org.gvsig.raster.lib.buffer.api.operations.Operation;
35
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory;
36
import org.gvsig.raster.lib.buffer.api.operations.OperationFactory.Applicable;
37
import org.gvsig.raster.lib.buffer.api.operations.OperationList;
38
import org.gvsig.raster.lib.buffer.api.operations.OperationListEntry;
39
import org.gvsig.raster.lib.buffer.spi.OperationListManager;
40
import org.gvsig.tools.dynobject.DynObject;
41

    
42

    
43
/**
44
 * @author fdiaz
45
 *
46
 */
47
public class DefaultOperationList implements OperationList {
48

    
49
    private List<OperationListEntry> operations;
50

    
51
    /**
52
     *
53
     */
54
    public DefaultOperationList() {
55
        this.operations = new ArrayList<OperationListEntry>();
56
    }
57

    
58
    /* (non-Javadoc)
59
     * @see java.util.List#size()
60
     */
61
    @Override
62
    public int size() {
63
        return this.operations.size();
64
    }
65

    
66
    /* (non-Javadoc)
67
     * @see java.util.List#isEmpty()
68
     */
69
    @Override
70
    public boolean isEmpty() {
71
        return this.operations.isEmpty();
72
    }
73

    
74
    /* (non-Javadoc)
75
     * @see java.util.List#contains(java.lang.Object)
76
     */
77
    @Override
78
    public boolean contains(Object o) {
79
        return this.operations.contains(o);
80
    }
81

    
82
    /* (non-Javadoc)
83
     * @see java.util.List#iterator()
84
     */
85
    @Override
86
    public Iterator<OperationListEntry> iterator() {
87
        return this.operations.iterator();
88
    }
89

    
90
    /* (non-Javadoc)
91
     * @see java.util.List#toArray()
92
     */
93
    @Override
94
    public Object[] toArray() {
95
        return this.operations.toArray();
96
    }
97

    
98
    /* (non-Javadoc)
99
     * @see java.util.List#toArray(java.lang.Object[])
100
     */
101
    @Override
102
    public <T> T[] toArray(T[] a) {
103
        return this.operations.toArray(a);
104
    }
105

    
106
    /* (non-Javadoc)
107
     * @see java.util.List#add(java.lang.Object)
108
     */
109
    @Override
110
    public boolean add(OperationListEntry e) {
111
        return this.operations.add(e);
112
    }
113

    
114
    /* (non-Javadoc)
115
     * @see java.util.List#remove(java.lang.Object)
116
     */
117
    @Override
118
    public boolean remove(Object o) {
119
        return this.operations.remove(o);
120
    }
121

    
122
    /* (non-Javadoc)
123
     * @see java.util.List#containsAll(java.util.Collection)
124
     */
125
    @Override
126
    public boolean containsAll(Collection<?> c) {
127
        return this.operations.containsAll(c);
128
    }
129

    
130
    /* (non-Javadoc)
131
     * @see java.util.List#addAll(java.util.Collection)
132
     */
133
    @Override
134
    public boolean addAll(Collection<? extends OperationListEntry> c) {
135
        return this.operations.addAll(c);
136
    }
137

    
138
    /* (non-Javadoc)
139
     * @see java.util.List#addAll(int, java.util.Collection)
140
     */
141
    @Override
142
    public boolean addAll(int index, Collection<? extends OperationListEntry> c) {
143
        return this.operations.addAll(index, c);
144
    }
145

    
146
    /* (non-Javadoc)
147
     * @see java.util.List#removeAll(java.util.Collection)
148
     */
149
    @Override
150
    public boolean removeAll(Collection<?> c) {
151
        return this.operations.removeAll(c);
152
    }
153

    
154
    /* (non-Javadoc)
155
     * @see java.util.List#retainAll(java.util.Collection)
156
     */
157
    @Override
158
    public boolean retainAll(Collection<?> c) {
159
        return this.operations.retainAll(c);
160
    }
161

    
162
    /* (non-Javadoc)
163
     * @see java.util.List#clear()
164
     */
165
    @Override
166
    public void clear() {
167
        this.operations.clear();
168
    }
169

    
170
    /* (non-Javadoc)
171
     * @see java.util.List#get(int)
172
     */
173
    @Override
174
    public OperationListEntry get(int index) {
175
        return this.operations.get(index);
176
    }
177

    
178
    /* (non-Javadoc)
179
     * @see java.util.List#set(int, java.lang.Object)
180
     */
181
    @Override
182
    public OperationListEntry set(int index, OperationListEntry element) {
183
        return this.operations.set(index, element);
184
    }
185

    
186
    /* (non-Javadoc)
187
     * @see java.util.List#add(int, java.lang.Object)
188
     */
189
    @Override
190
    public void add(int index, OperationListEntry element) {
191
        this.operations.add(index, element);
192
    }
193

    
194
    /* (non-Javadoc)
195
     * @see java.util.List#remove(int)
196
     */
197
    @Override
198
    public OperationListEntry remove(int index) {
199
        return this.operations.remove(index);
200
    }
201

    
202
    /* (non-Javadoc)
203
     * @see java.util.List#indexOf(java.lang.Object)
204
     */
205
    @Override
206
    public int indexOf(Object o) {
207
        return this.operations.indexOf(o);
208
    }
209

    
210
    /* (non-Javadoc)
211
     * @see java.util.List#lastIndexOf(java.lang.Object)
212
     */
213
    @Override
214
    public int lastIndexOf(Object o) {
215
        return this.operations.lastIndexOf(o);
216
    }
217

    
218
    /* (non-Javadoc)
219
     * @see java.util.List#listIterator()
220
     */
221
    @Override
222
    public ListIterator<OperationListEntry> listIterator() {
223
        return this.operations.listIterator();
224
    }
225

    
226
    /* (non-Javadoc)
227
     * @see java.util.List#listIterator(int)
228
     */
229
    @Override
230
    public ListIterator<OperationListEntry> listIterator(int index) {
231
        return this.operations.listIterator(index);
232
    }
233

    
234
    /* (non-Javadoc)
235
     * @see java.util.List#subList(int, int)
236
     */
237
    @Override
238
    public List<OperationListEntry> subList(int fromIndex, int toIndex) {
239
        return this.operations.subList(fromIndex, toIndex);
240
    }
241

    
242
    /* (non-Javadoc)
243
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationList#execute(org.gvsig.raster.lib.buffer.api.Buffer)
244
     */
245
    @Override
246
    public Buffer execute(Buffer buffer) throws BufferOperationException {
247
        Buffer tmpBuffer = buffer;
248
        for (Iterator<OperationListEntry> iterator = operations.iterator(); iterator.hasNext();) {
249
            OperationListEntry operationListEntry = (OperationListEntry) iterator.next();
250
            Operation operation = operationListEntry.getOperation();
251
            DynObject parameters = operationListEntry.getParameters();
252
            OperationFactory factory = operation.getFactory();
253
            if(operationListEntry.isActive()){
254
                if(factory.isApplicable(tmpBuffer).equals(Applicable.NO)){
255
                    throw new IllegalArgumentException("The operation is not applicable to the input buffer");
256
                }
257
                tmpBuffer = operation.execute(tmpBuffer, parameters);
258
            }
259
        }
260
        return tmpBuffer;
261
    }
262

    
263
    /* (non-Javadoc)
264
     * @see org.gvsig.raster.lib.buffer.api.operations.OperationList#add(org.gvsig.raster.lib.buffer.api.operations.Operation, org.gvsig.tools.dynobject.DynObject)
265
     */
266
    @Override
267
    public void add(Operation operation, DynObject parameters) {
268
        OperationListManager operationListManager = ((OperationListManager)BufferLocator.getBufferManager());
269
        OperationListEntry entry = operationListManager.createOperationListEntry(operation, parameters);
270
        this.operations.add(entry);
271
    }
272

    
273
    @Override
274
    public void add(int index, Operation operation, DynObject parameters) {
275
        OperationListManager operationListManager = ((OperationListManager)BufferLocator.getBufferManager());
276
        OperationListEntry entry = operationListManager.createOperationListEntry(operation, parameters);
277
        this.operations.add(index, entry);
278
    }
279

    
280
    @Override
281
    public OperationListEntry getEntry(String name) {
282

    
283
        for (Iterator<OperationListEntry> iterator = operations.iterator(); iterator.hasNext();) {
284
            OperationListEntry operationListEntry = (OperationListEntry) iterator.next();
285
            Operation operation = operationListEntry.getOperation();
286
            if(operation.getFactory().getName().equals(name)){
287
                return operationListEntry;
288
            }
289
        }
290
        return null;
291
    }
292

    
293
    @Override
294
    public Object getThrownDynValue(String name) {
295
        ListIterator<OperationListEntry> li = operations.listIterator(operations.size());
296

    
297
         // Iterate in reverse.
298
         while(li.hasPrevious()) {
299
             OperationListEntry operationListEntry = (OperationListEntry) li.previous();
300
             if(operationListEntry.getParameters().hasDynValue(name)) {
301
                 return operationListEntry.getParameters().getDynValue(name);
302
             }
303
         }
304
        return null;
305
    }
306

    
307
}