1 /*
2  * Copyright (C) 2023 The Android Open Source Project
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 android.bluetooth;
18 
19 import io.grpc.stub.StreamObserver;
20 
21 import java.util.Iterator;
22 import java.util.Spliterator;
23 import java.util.Spliterators;
24 import java.util.concurrent.BlockingQueue;
25 import java.util.concurrent.LinkedBlockingQueue;
26 import java.util.function.Consumer;
27 
28 public class StreamObserverSpliterator<T> implements Spliterator<T>, StreamObserver<T> {
29     private BlockingQueue<Object> mQueue = new LinkedBlockingQueue<>();
30     private static final Object COMPLETED_INDICATOR = new Object();
31 
32     /**
33      * Creates and returns an iterator over the elements contained in the internal blocking queue.
34      *
35      * <p>The iterator is based on this class's Spliterator implementation. As elements are consumed
36      * from the iterator, they are removed from the queue. The iterator continues to provide
37      * elements as long as new items are added to the queue via the onNext method or until the
38      * onCompleted method is called.
39      *
40      * <p>If the onError method was called previously and the corresponding Throwable is retrieved
41      * by the iterator, it will throw a RuntimeException wrapping the original Throwable.
42      *
43      * @return an iterator over the elements contained in the internal blocking queue
44      */
iterator()45     public Iterator<T> iterator() {
46         return Spliterators.iterator(this);
47     }
48 
49     @Override
characteristics()50     public int characteristics() {
51         return ORDERED | NONNULL;
52     }
53 
54     @Override
estimateSize()55     public long estimateSize() {
56         return Long.MAX_VALUE;
57     }
58 
59     @Override
tryAdvance(Consumer<? super T> action)60     public boolean tryAdvance(Consumer<? super T> action) {
61         try {
62             Object item = mQueue.take();
63             if (item == COMPLETED_INDICATOR) {
64                 return false;
65             }
66             if (item instanceof Throwable) {
67                 throw new RuntimeException((Throwable) item);
68             }
69             action.accept((T) item);
70             return true;
71         } catch (InterruptedException e) {
72             throw new RuntimeException(e);
73         }
74     }
75 
76     @Override
trySplit()77     public Spliterator<T> trySplit() {
78         return null;
79     }
80 
81     @Override
onNext(T value)82     public void onNext(T value) {
83         mQueue.add(value);
84     }
85 
86     @Override
onError(Throwable t)87     public void onError(Throwable t) {
88         mQueue.add(t);
89     }
90 
91     @Override
onCompleted()92     public void onCompleted() {
93         mQueue.add(COMPLETED_INDICATOR);
94     }
95 }
96