1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
|
23 | |
|
24 | |
|
25 | |
|
26 | |
package com.sun.javafx.runtime.sequence; |
27 | |
|
28 | |
import java.util.BitSet; |
29 | |
import java.util.List; |
30 | |
import java.util.Iterator; |
31 | |
import java.util.NoSuchElementException; |
32 | |
|
33 | |
import com.sun.javafx.runtime.Util; |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 387088 | class ArraySequence<T> extends AbstractSequence<T> implements Sequence<T> { |
43 | |
|
44 | |
private final T[] array; |
45 | |
|
46 | |
|
47 | |
public ArraySequence(Class<T> clazz, T... values) { |
48 | 1441 | super(clazz); |
49 | 1441 | this.array = Util.<T>newObjectArray(values.length); |
50 | 1441 | System.arraycopy(values, 0, array, 0, values.length); |
51 | 1441 | checkForNulls(); |
52 | 1441 | } |
53 | |
|
54 | |
public ArraySequence(Class<T> clazz, T[] values, int size) { |
55 | 4334 | super(clazz); |
56 | 4334 | this.array = Util.<T>newObjectArray(size); |
57 | 4334 | System.arraycopy(values, 0, array, 0, size); |
58 | 4334 | checkForNulls(); |
59 | 4334 | } |
60 | |
|
61 | |
@SuppressWarnings("unchecked") |
62 | |
public ArraySequence(Class<T> clazz, List<? extends T> values) { |
63 | 0 | super(clazz); |
64 | 0 | this.array = (T[]) values.toArray(); |
65 | 0 | checkForNulls(); |
66 | 0 | } |
67 | |
|
68 | |
public ArraySequence(Class<T> clazz, Sequence<? extends T>... sequences) { |
69 | 2380 | super(clazz); |
70 | 2380 | int size = 0; |
71 | 8816 | for (Sequence<? extends T> seq : sequences) |
72 | 6436 | size += seq.size(); |
73 | 2380 | this.array = Util.<T>newObjectArray(size); |
74 | 2380 | int next = 0; |
75 | 8816 | for (Sequence<? extends T> seq : sequences) { |
76 | 6436 | seq.toArray(array, next); |
77 | 6436 | next += seq.size(); |
78 | |
} |
79 | 2380 | checkForNulls(); |
80 | 2380 | } |
81 | |
|
82 | |
private void checkForNulls() { |
83 | 254692 | for (T v : array) |
84 | 246537 | if (v == null) |
85 | 0 | throw new IllegalArgumentException("cannot create sequence with null values"); |
86 | 8155 | } |
87 | |
|
88 | |
@Override |
89 | |
public int size() { |
90 | 153829 | return array.length; |
91 | |
} |
92 | |
|
93 | |
@Override |
94 | |
public T get(int position) { |
95 | 709549 | if (position < 0 || position >= array.length) |
96 | 327 | throw new IndexOutOfBoundsException(Integer.toString(position)); |
97 | |
else |
98 | 709222 | return array[position]; |
99 | |
} |
100 | |
|
101 | |
|
102 | |
|
103 | |
@Override |
104 | |
public BitSet getBits(SequencePredicate<? super T> predicate) { |
105 | 727 | BitSet bits = new BitSet(array.length); |
106 | 1988 | for (int i = 0; i < array.length; i++) |
107 | 1261 | if (predicate.matches(this, i, array[i])) |
108 | 502 | bits.set(i); |
109 | 727 | return bits; |
110 | |
} |
111 | |
|
112 | |
@Override |
113 | |
public void toArray(Object[] dest, int destOffset) { |
114 | 2676 | System.arraycopy(array, 0, dest, destOffset, array.length); |
115 | 2676 | } |
116 | |
|
117 | |
@Override |
118 | |
public Iterator<T> iterator() { |
119 | 8937 | return new Iterator<T>() { |
120 | |
int index; |
121 | |
|
122 | |
public boolean hasNext() { |
123 | 231431 | return index < array.length; |
124 | |
} |
125 | |
|
126 | |
public T next() { |
127 | 155657 | if (hasNext()) |
128 | 155657 | return array[index++]; |
129 | |
else |
130 | 0 | throw new NoSuchElementException(); |
131 | |
} |
132 | |
|
133 | |
public void remove() { |
134 | 0 | throw new UnsupportedOperationException(); |
135 | |
} |
136 | |
}; |
137 | |
} |
138 | |
} |