/
Sources.java
204 lines (170 loc) · 6.78 KB
/
Sources.java
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/*
* Copyright (c) 2013 Nigel Duffy
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.nickelproject.nickel.dataflow;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.collect.Iterators;
public final class Sources {
private Sources() {
// Prevents construction
}
public static <S> Source<S> limit(final Source<S> pSource, final int pLimit) {
return from(Iterables.limit(pSource, pLimit));
}
public static <S> Source<S> singleton(final S object) {
return from(Collections.singleton(object));
}
public static <T> Source<T> concat(final Source<T>... sources) {
return concat(from(sources));
}
public static <T> Source<T> concat(final Source<Source<T>> sources) {
return new ConcatSource<T>(sources);
}
public static <T> Source<T> filter(final Source<T> source, final Predicate<T> predicate) {
return new FilteredSource<T>(source, predicate);
}
public static <S, T> Source<T> transform(final Source<? extends S> source, final Function<S, T> function) {
return new TransformedSource<S, T>(source, function);
}
public static <S> Source<S> from(final Iterable<S> iterable) {
return new IterableSource<S>(iterable);
}
public static <S> Source<S> from(final S[] array) {
return from(Arrays.asList(array));
}
public static <S> Source<S> from(final List<S> list) {
return new IterableSource<S>(list, list.size());
}
private static class ConcatSource<S> implements Source<S> {
private static final long serialVersionUID = 1L;
private final Source<Source<S>> sources;
public ConcatSource(final Source<Source<S>> sources) {
this.sources = sources;
}
@Override
public int size() {
return Source.unknownSize;
}
@Override
public Iterator<S> iterator() {
return Iterators.concat(
Iterators.transform(sources.iterator(),
new Function<Source<S>, Iterator<S>>() {
@Override
public Iterator<S> apply(final Source<S> input) {
return input.iterator();
}
}));
}
@Override
public Source<Source<S>> partition(final int partitionSize) {
return sources;
}
}
private static class IterableSource<S> implements Source<S> {
private static final long serialVersionUID = 1L;
private final Iterable<S> iterable;
private final int size;
public IterableSource(final Iterable<S> iterable, final int size) {
this.iterable = iterable;
this.size = size;
}
public IterableSource(final Iterable<S> iterable) {
this(iterable, Source.unknownSize);
}
@Override
public int size() {
return size;
}
@Override
public Iterator<S> iterator() {
return iterable.iterator();
}
@Override
public Source<Source<S>> partition(final int partitionSize) {
return from(Iterables.transform(Iterables.partition(iterable, partitionSize),
new Function<Iterable<S>, Source<S>>() {
@Override
public Source<S> apply(final Iterable<S> partition) {
return from(partition);
}
}));
}
}
private static class TransformedSource<S, T> implements Source<T> {
private static final long serialVersionUID = 1L;
private final Source<? extends S> wrappedSource;
private final Function<S, T> transform;
public TransformedSource(final Source<? extends S> wrappedSource, final Function<S, T> transform) {
this.wrappedSource = wrappedSource;
this.transform = transform;
}
@Override
public int size() {
return wrappedSource.size();
}
@Override
public Iterator<T> iterator() {
return Iterators.transform(wrappedSource.iterator(), transform);
}
@Override
public Source<Source<T>> partition(final int partitionSize) {
final Function<Source<S>, Source<T>> sourceTransform = new Function<Source<S>, Source<T>>() {
@Override
public Source<T> apply(final Source<S> input) {
return transform(input, transform);
}
};
final Source<Source<S>> wrappedPartitions =
(Source<Source<S>>) wrappedSource.partition(partitionSize);
return transform(wrappedPartitions, sourceTransform);
}
}
private static class FilteredSource<T> implements Source<T> {
private static final long serialVersionUID = 1L;
private final Source<T> wrappedSource;
private final Predicate<T> predicate;
public FilteredSource(final Source<T> wrappedSource, final Predicate<T> predicate) {
this.wrappedSource = wrappedSource;
this.predicate = predicate;
}
@Override
public int size() {
return Source.unknownSize;
}
@Override
public Iterator<T> iterator() {
return Iterators.filter(wrappedSource.iterator(), predicate);
}
@Override
public Source<Source<T>> partition(final int partitionSize) {
final Source<Source<T>> wrappedPartition =
(Source<Source<T>>) wrappedSource.partition(partitionSize);
return transform(wrappedPartition,
new Function<Source<T>, Source<T>>() {
@Override
public Source<T> apply(final Source<T> input) {
return filter(input, predicate);
}
});
}
}
}