Debian Codegraph @

src/test/java/io/reactivex/rxjava3/internal/util/CrashingIterable.java

raw
1 /*
2 * Copyright (c) 2016-present, RxJava Contributors.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
5 * compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License is
10 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See
11 * the License for the specific language governing permissions and limitations under the License.
12 */
13
14 package io.reactivex.rxjava3.internal.util;
15
16 import java.util.Iterator;
17
18 import io.reactivex.rxjava3.exceptions.TestException;
19
20 /**
21 * An Iterable and Iterator that crashes with TestException after the given number
22 * of method invocations on iterator(), hasNext() and next().
23 */
24 public final class CrashingIterable implements Iterable<Integer> {
25 int crashOnIterator;
26
27 final int crashOnHasNext;
28
29 final int crashOnNext;
30
31 public CrashingIterable(int crashOnIterator, int crashOnHasNext, int crashOnNext) {
32 this.crashOnIterator = crashOnIterator;
33 this.crashOnHasNext = crashOnHasNext;
34 this.crashOnNext = crashOnNext;
35 }
36
37 @Override
38 public Iterator<Integer> iterator() {
39 if (--crashOnIterator <= 0) {
40 throw new TestException("iterator()");
41 }
42 return new CrashingIterator(crashOnHasNext, crashOnNext);
43 }
44
45 static final class CrashingIterator implements Iterator<Integer> {
46 int crashOnHasNext;
47
48 int crashOnNext;
49
50 int count;
51
52 CrashingIterator(int crashOnHasNext, int crashOnNext) {
53 this.crashOnHasNext = crashOnHasNext;
54 this.crashOnNext = crashOnNext;
55 }
56
57 @Override
58 public boolean hasNext() {
59 if (--crashOnHasNext <= 0) {
60 throw new TestException("hasNext()");
61 }
62 return true;
63 }
64
65 @Override
66 public Integer next() {
67 if (--crashOnNext <= 0) {
68 throw new TestException("next()");
69 }
70 return count++;
71 }
72
73 @Override
74 public void remove() {
75 throw new UnsupportedOperationException();
76 }
77 }
78 }