Skip to content

Commit beca0e8

Browse files
committed
A bunch of operators.
1 parent a1c95eb commit beca0e8

31 files changed

+1645
-56
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
17+
package ix;
18+
19+
enum EqualityHelper implements Pred2<Object, Object> {
20+
INSTANCE;
21+
22+
@Override
23+
public boolean test(Object t, Object u) {
24+
return (t == u) || (t != null && t.equals(u));
25+
}
26+
}

src/main/java/ix/GroupedIx.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
117
package ix;
218

319
/**

src/main/java/ix/Ix.java

Lines changed: 33 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -201,108 +201,87 @@ public final Ix<Boolean> all(Pred<? super T> predicate) {
201201
}
202202

203203
public final Ix<Boolean> hasElements() {
204-
// TODO implement
205-
throw new UnsupportedOperationException();
204+
return new IxHasElements<T>(this);
206205
}
207206

208207
public final Ix<T> ignoreElements() {
209-
// TODO implement
210-
throw new UnsupportedOperationException();
208+
return new IxIgnoreElements<T>(this);
211209
}
212210

213211
public final Ix<T> max(Comparator<? super T> comparator) {
214-
// TODO implement
215-
throw new UnsupportedOperationException();
212+
return new IxMinMax<T>(this, comparator, -1);
216213
}
217214

215+
@SuppressWarnings({ "rawtypes", "unchecked" })
218216
public final Ix<T> max() {
219-
// TODO implement
220-
throw new UnsupportedOperationException();
217+
return max((Comparator)SelfComparator.INSTANCE);
221218
}
222219

223220
public final Ix<T> min(Comparator<? super T> comparator) {
224-
// TODO implement
225-
throw new UnsupportedOperationException();
221+
return new IxMinMax<T>(this, comparator, 1);
226222
}
227223

224+
@SuppressWarnings({ "rawtypes", "unchecked" })
228225
public final Ix<T> min() {
229-
// TODO implement
230-
throw new UnsupportedOperationException();
226+
return min((Comparator)SelfComparator.INSTANCE);
231227
}
232228

233229
public final Ix<Boolean> contains(Object o) {
234-
// TODO implement
235-
throw new UnsupportedOperationException();
230+
return new IxContains<T>(this, o);
236231
}
237232

238233
public final Ix<Integer> count() {
239-
// TODO implement
240-
throw new UnsupportedOperationException();
234+
return new IxCount<T>(this);
241235
}
242236

243237
public final Ix<Long> countLong() {
244-
// TODO implement
245-
throw new UnsupportedOperationException();
238+
return new IxCountLong<T>(this);
246239
}
247240

248241
public final Ix<T> distinct() {
249-
// TODO implement
250-
throw new UnsupportedOperationException();
251-
}
252-
253-
public final Ix<T> distinct(Pred2<? super T, ? super T> comparer) {
254-
// TODO implement
255-
throw new UnsupportedOperationException();
242+
return distinct(IdentityHelper.instance());
256243
}
257244

258245
public final <K> Ix<T> distinct(Func1<? super T, K> keySelector) {
259-
// TODO implement
260-
throw new UnsupportedOperationException();
246+
return new IxDistinct<T, K>(this, keySelector);
261247
}
262248

263249
public final Ix<T> distinctUntilChanged() {
264-
// TODO implement
265-
throw new UnsupportedOperationException();
250+
return distinctUntilChanged(IdentityHelper.instance());
266251
}
267252

268253
public final Ix<T> distinctUntilChanged(Pred2<? super T, ? super T> comparer) {
269-
// TODO implement
270-
throw new UnsupportedOperationException();
254+
return new IxDistinctUntilChanged<T, T>(this, IdentityHelper.<T>instance(), comparer);
271255
}
272256

273257
public final <K> Ix<T> distinctUntilChanged(Func1<? super T, K> keySelector) {
274-
// TODO implement
275-
throw new UnsupportedOperationException();
258+
return new IxDistinctUntilChanged<T, K>(this, keySelector, EqualityHelper.INSTANCE);
276259
}
277260

278261
public final Ix<T> doOnNext(Action1<? super T> action) {
279-
// TODO implement
280-
throw new UnsupportedOperationException();
262+
return new IxDoOn<T>(this, action, IxEmptyAction.instance0());
281263
}
282264

283-
public final Ix<T> doOnCompleted(Action1<? super T> action) {
284-
// TODO implement
285-
throw new UnsupportedOperationException();
265+
public final Ix<T> doOnCompleted(Action0 action) {
266+
return new IxDoOn<T>(this, IxEmptyAction.instance1(), action);
286267
}
287268

288-
public final Ix<T> startWith(T... value) {
289-
// TODO implement
290-
throw new UnsupportedOperationException();
269+
@SuppressWarnings("unchecked")
270+
public final Ix<T> startWith(T... values) {
271+
return concatArray(fromArray(values), this);
291272
}
292273

293-
public final Ix<T> endWith(T... value) {
294-
// TODO implement
295-
throw new UnsupportedOperationException();
274+
@SuppressWarnings("unchecked")
275+
public final Ix<T> endWith(T... values) {
276+
return concatArray(this, fromArray(values));
296277
}
297278

298279
public final Ix<String> join() {
299-
// TODO implement
300-
throw new UnsupportedOperationException();
280+
return join(", ");
301281
}
302282

303283
public final Ix<String> join(CharSequence separator) {
304-
// TODO implement
305-
throw new UnsupportedOperationException();
284+
return new IxJoin<T>(this, separator);
306285
}
307286

308287
public final <R> Ix<R> map(Func1<? super T, ? extends R> mapper) {
@@ -515,18 +494,17 @@ public final Ix<Ix<T>> window(int size, int skip) {
515494
throw new UnsupportedOperationException();
516495
}
517496

518-
public final Ix<T> concatWith(Iterator<? extends T> other) {
519-
// TODO implement
520-
throw new UnsupportedOperationException();
497+
@SuppressWarnings("unchecked")
498+
public final Ix<T> concatWith(Iterable<? extends T> other) {
499+
return concatArray(this, other);
521500
}
522501

523-
public final Ix<T> mergeWith(Iterator<? extends T> other) {
502+
public final Ix<T> mergeWith(Iterable<? extends T> other) {
524503
return concatWith(other);
525504
}
526505

527-
public final <U, R> Ix<R> zipWith(Iterator<U> other, Func2<? super T, ? super U, ? extends R> zipper) {
528-
// TODO implement
529-
throw new UnsupportedOperationException();
506+
public final <U, R> Ix<R> zipWith(Iterable<U> other, Func2<? super T, ? super U, ? extends R> zipper) {
507+
return zip(this, other, zipper);
530508
}
531509

532510
public final Ix<T> orderBy() {

src/main/java/ix/IxCompose.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,19 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
117
package ix;
218

319
import java.util.Iterator;

src/main/java/ix/IxContains.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
17+
package ix;
18+
19+
import java.util.Iterator;
20+
21+
final class IxContains<T> extends IxSource<T, Boolean> {
22+
23+
final Object o;
24+
25+
public IxContains(Iterable<T> source, Object o) {
26+
super(source);
27+
this.o = o;
28+
}
29+
30+
@Override
31+
public Iterator<Boolean> iterator() {
32+
return new ContainsIterator<T>(source.iterator(), o);
33+
}
34+
35+
static final class ContainsIterator<T> extends IxSourceIterator<T, Boolean> {
36+
37+
final Object o;
38+
39+
public ContainsIterator(Iterator<T> it, Object o) {
40+
super(it);
41+
this.o = o;
42+
}
43+
44+
@Override
45+
protected boolean moveNext() {
46+
Iterator<T> it = this.it;
47+
Object o = this.o;
48+
49+
while (it.hasNext()) {
50+
T v = it.next();
51+
if (o == v || (o != null && o.equals(v))) {
52+
value = true;
53+
hasValue = true;
54+
done = true;
55+
return true;
56+
}
57+
}
58+
59+
value = false;
60+
hasValue = true;
61+
done = true;
62+
return true;
63+
}
64+
}
65+
66+
}

src/main/java/ix/IxCount.java

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2011-2016 David Karnok
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
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+
17+
package ix;
18+
19+
import java.util.Iterator;
20+
21+
final class IxCount<T> extends IxSource<T, Integer> {
22+
23+
public IxCount(Iterable<T> source) {
24+
super(source);
25+
}
26+
27+
@Override
28+
public Iterator<Integer> iterator() {
29+
return new CountIterator<T>(source.iterator());
30+
}
31+
32+
static final class CountIterator<T> extends IxSourceIterator<T, Integer> {
33+
34+
public CountIterator(Iterator<T> it) {
35+
super(it);
36+
}
37+
38+
@Override
39+
protected boolean moveNext() {
40+
int c = 0;
41+
42+
Iterator<T> it = this.it;
43+
44+
while (it.hasNext()) {
45+
it.next();
46+
c++;
47+
}
48+
49+
value = c;
50+
hasValue = true;
51+
done = true;
52+
return true;
53+
}
54+
}
55+
}

0 commit comments

Comments
 (0)