Debian Codegraph @

src/main/java/io/reactivex/rxjava3/internal/util/AppendOnlyLinkedArrayList.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 org.reactivestreams.Subscriber;
17
18 import io.reactivex.rxjava3.core.Observer;
19 import io.reactivex.rxjava3.functions.*;
20
21 /**
22 * A linked-array-list implementation that only supports appending and consumption.
23 *
24 * @param <T> the value type
25 */
26 public class AppendOnlyLinkedArrayList<T> {
27 final int capacity;
28 final Object[] head;
29 Object[] tail;
30 int offset;
31
32 /**
33 * Constructs an empty list with a per-link capacity.
34 * @param capacity the capacity of each link
35 */
36 public AppendOnlyLinkedArrayList(int capacity) {
37 this.capacity = capacity;
38 this.head = new Object[capacity + 1];
39 this.tail = head;
40 }
41
42 /**
43 * Append a non-null value to the list.
44 * <p>Don't add null to the list!
45 * @param value the value to append
46 */
47 public void add(T value) {
48 final int c = capacity;
49 int o = offset;
50 if (o == c) {
51 Object[] next = new Object[c + 1];
52 tail[c] = next;
53 tail = next;
54 o = 0;
55 }
56 tail[o] = value;
57 offset = o + 1;
58 }
59
60 /**
61 * Set a value as the first element of the list.
62 * @param value the value to set
63 */
64 public void setFirst(T value) {
65 head[0] = value;
66 }
67
68 /**
69 * Predicate interface suppressing the exception.
70 *
71 * @param <T> the value type
72 */
73 public interface NonThrowingPredicate<T> extends Predicate<T> {
74 @Override
75 boolean test(T t);
76 }
77
78 /**
79 * Loops over all elements of the array until a null element is encountered or
80 * the given predicate returns true.
81 * @param consumer the consumer of values that returns true if the forEach should terminate
82 */
83 @SuppressWarnings("unchecked")
84 public void forEachWhile(NonThrowingPredicate<? super T> consumer) {
85 Object[] a = head;
86 final int c = capacity;
87 while (a != null) {
88 for (int i = 0; i < c; i++) {
89 Object o = a[i];
90 if (o == null) {
91 break;
92 }
93 if (consumer.test((T)o)) {
94 return;
95 }
96 }
97 a = (Object[])a[c];
98 }
99 }
100
101 /**
102 * Interprets the contents as NotificationLite objects and calls
103 * the appropriate Subscriber method.
104 *
105 * @param <U> the target type
106 * @param subscriber the subscriber to emit the events to
107 * @return true if a terminal event has been reached
108 */
109 public <U> boolean accept(Subscriber<? super U> subscriber) {
110 Object[] a = head;
111 final int c = capacity;
112 while (a != null) {
113 for (int i = 0; i < c; i++) {
114 Object o = a[i];
115 if (o == null) {
116 break;
117 }
118
119 if (NotificationLite.acceptFull(o, subscriber)) {
120 return true;
121 }
122 }
123 a = (Object[])a[c];
124 }
125 return false;
126 }
127
128 /**
129 * Interprets the contents as NotificationLite objects and calls
130 * the appropriate Observer method.
131 *
132 * @param <U> the target type
133 * @param observer the observer to emit the events to
134 * @return true if a terminal event has been reached
135 */
136 public <U> boolean accept(Observer<? super U> observer) {
137 Object[] a = head;
138 final int c = capacity;
139 while (a != null) {
140 for (int i = 0; i < c; i++) {
141 Object o = a[i];
142 if (o == null) {
143 break;
144 }
145
146 if (NotificationLite.acceptFull(o, observer)) {
147 return true;
148 }
149 }
150 a = (Object[])a[c];
151 }
152 return false;
153 }
154
155 /**
156 * Loops over all elements of the array until a null element is encountered or
157 * the given predicate returns true.
158 * @param <S> the extra state type
159 * @param state the extra state passed into the consumer
160 * @param consumer the consumer of values that returns true if the forEach should terminate
161 * @throws Throwable if the predicate throws
162 */
163 @SuppressWarnings("unchecked")
164 public <S> void forEachWhile(S state, BiPredicate<? super S, ? super T> consumer) throws Throwable {
165 Object[] a = head;
166 final int c = capacity;
167 for (;;) {
168 for (int i = 0; i < c; i++) {
169 Object o = a[i];
170 if (o == null) {
171 return;
172 }
173 if (consumer.test(state, (T)o)) {
174 return;
175 }
176 }
177 a = (Object[])a[c];
178 }
179 }
180 }