01 /*
02 * Copyright 2008-2015 the original author or authors.
03 *
04 * Licensed under the Apache License, Version 2.0 (the "License");
05 * you may not use this file except in compliance with the License.
06 * You may obtain a copy of the License at
07 *
08 * http://www.apache.org/licenses/LICENSE-2.0
09 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package griffon.builder.pivot
17
18 /**
19 * @author Andres Almiray
20 */
21 final class FixedIterator implements Iterator {
22 private final iterable
23 private final boolean mutable
24 private int index = 0;
25
26 FixedIterator(iterable, boolean mutable = false) {
27 this.iterable = iterable
28 this.mutable = mutable
29 }
30
31 boolean hasNext() {
32 return index > -1 && index < size()
33 }
34
35 Object next() {
36 return iterable.get(index++)
37 }
38
39 void remove() {
40 if (!mutable) {
41 throw new UnsupportedOperationException("Immutable iterator!")
42 }
43 if (hasNext()) iterable.remove(index--)
44 }
45
46 private int size() {
47 MetaClass mc = iterable.metaClass
48 if (mc.respondsTo(iterable, 'getLength')) return iterable.getLength()
49 if (mc.respondsTo(iterable, 'getSize')) return iterable.getSize()
50 if (mc.respondsTo(iterable, 'getCount')) return iterable.getCount()
51 -1
52 }
53 }
|