|
1 | 1 | /**
|
2 | 2 | * Copyright 2014 Netflix, Inc.
|
3 |
| - * |
| 3 | + * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
6 | 6 | * You may obtain a copy of the License at
|
7 |
| - * |
| 7 | + * |
8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0
|
9 |
| - * |
| 9 | + * |
10 | 10 | * Unless required by applicable law or agreed to in writing, software
|
11 | 11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
12 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
21 | 21 | import java.util.LinkedList;
|
22 | 22 | import java.util.List;
|
23 | 23 | import java.util.concurrent.TimeUnit;
|
24 |
| -import java.util.concurrent.locks.ReentrantLock; |
25 | 24 |
|
26 | 25 | import rx.Observable;
|
27 | 26 | import rx.Observable.OnSubscribeFunc;
|
| 27 | +import rx.Observable.Operator; |
28 | 28 | import rx.Observer;
|
29 | 29 | import rx.Scheduler;
|
30 | 30 | import rx.Subscriber;
|
|
34 | 34 | /**
|
35 | 35 | * Bypasses a specified number of elements at the end of an observable sequence.
|
36 | 36 | */
|
37 |
| -public class OperationSkipLast { |
| 37 | +public class OperationSkipLast<T> implements Operator<T, T> { |
38 | 38 |
|
39 |
| - /** |
40 |
| - * Bypasses a specified number of elements at the end of an observable |
41 |
| - * sequence. |
42 |
| - * <p> |
43 |
| - * This operator accumulates a queue with a length enough to store the first |
44 |
| - * count elements. As more elements are received, elements are taken from |
45 |
| - * the front of the queue and produced on the result sequence. This causes |
46 |
| - * elements to be delayed. |
47 |
| - * |
48 |
| - * @param source |
49 |
| - * the source sequence. |
50 |
| - * @param count |
51 |
| - * number of elements to bypass at the end of the source |
52 |
| - * sequence. |
53 |
| - * @return An observable sequence containing the source sequence elements |
54 |
| - * except for the bypassed ones at the end. |
55 |
| - * |
56 |
| - * @throws IndexOutOfBoundsException |
57 |
| - * count is less than zero. |
58 |
| - */ |
59 |
| - public static <T> OnSubscribeFunc<T> skipLast( |
60 |
| - Observable<? extends T> source, int count) { |
61 |
| - return new SkipLast<T>(source, count); |
62 |
| - } |
| 39 | + private final int count; |
63 | 40 |
|
64 |
| - private static class SkipLast<T> implements OnSubscribeFunc<T> { |
65 |
| - private final int count; |
66 |
| - private final Observable<? extends T> source; |
67 |
| - |
68 |
| - private SkipLast(Observable<? extends T> source, int count) { |
69 |
| - this.count = count; |
70 |
| - this.source = source; |
| 41 | + public OperationSkipLast(int count) { |
| 42 | + if (count < 0) { |
| 43 | + throw new IndexOutOfBoundsException("count could not be negative"); |
71 | 44 | }
|
| 45 | + this.count = count; |
| 46 | + } |
72 | 47 |
|
73 |
| - public Subscription onSubscribe(final Observer<? super T> observer) { |
74 |
| - if (count < 0) { |
75 |
| - throw new IndexOutOfBoundsException( |
76 |
| - "count could not be negative"); |
77 |
| - } |
78 |
| - final SafeObservableSubscription subscription = new SafeObservableSubscription(); |
79 |
| - return subscription.wrap(source.unsafeSubscribe(new Subscriber<T>() { |
80 |
| - |
81 |
| - private final ReentrantLock lock = new ReentrantLock(); |
| 48 | + @Override |
| 49 | + public Subscriber<? super T> call(final Subscriber<? super T> subscriber) { |
| 50 | + return new Subscriber<T>(subscriber) { |
| 51 | + /** |
| 52 | + * Store the last count elements until now. |
| 53 | + */ |
| 54 | + private final Deque<T> deque = new LinkedList<T>(); |
82 | 55 |
|
83 |
| - /** |
84 |
| - * Store the last count elements until now. |
85 |
| - */ |
86 |
| - private final Deque<T> deque = new LinkedList<T>(); |
| 56 | + @Override |
| 57 | + public void onCompleted() { |
| 58 | + subscriber.onCompleted(); |
| 59 | + } |
87 | 60 |
|
88 |
| - @Override |
89 |
| - public void onCompleted() { |
90 |
| - observer.onCompleted(); |
91 |
| - } |
| 61 | + @Override |
| 62 | + public void onError(Throwable e) { |
| 63 | + subscriber.onError(e); |
| 64 | + } |
92 | 65 |
|
93 |
| - @Override |
94 |
| - public void onError(Throwable e) { |
95 |
| - observer.onError(e); |
| 66 | + @Override |
| 67 | + public void onNext(T value) { |
| 68 | + if (count == 0) { |
| 69 | + // If count == 0, we do not need to put value into deque |
| 70 | + // and remove it at once. We can emit the value |
| 71 | + // directly. |
| 72 | + subscriber.onNext(value); |
| 73 | + return; |
96 | 74 | }
|
97 |
| - |
98 |
| - @Override |
99 |
| - public void onNext(T value) { |
100 |
| - if (count == 0) { |
101 |
| - // If count == 0, we do not need to put value into deque |
102 |
| - // and remove it at once. We can emit the value |
103 |
| - // directly. |
104 |
| - try { |
105 |
| - observer.onNext(value); |
106 |
| - } catch (Throwable ex) { |
107 |
| - observer.onError(ex); |
108 |
| - subscription.unsubscribe(); |
109 |
| - } |
110 |
| - return; |
111 |
| - } |
112 |
| - lock.lock(); |
113 |
| - try { |
114 |
| - deque.offerLast(value); |
115 |
| - if (deque.size() > count) { |
116 |
| - // Now deque has count + 1 elements, so the first |
117 |
| - // element in the deque definitely does not belong |
118 |
| - // to the last count elements of the source |
119 |
| - // sequence. We can emit it now. |
120 |
| - observer.onNext(deque.removeFirst()); |
121 |
| - } |
122 |
| - } catch (Throwable ex) { |
123 |
| - observer.onError(ex); |
124 |
| - subscription.unsubscribe(); |
125 |
| - } finally { |
126 |
| - lock.unlock(); |
127 |
| - } |
| 75 | + deque.offerLast(value); |
| 76 | + if (deque.size() > count) { |
| 77 | + // Now deque has count + 1 elements, so the first |
| 78 | + // element in the deque definitely does not belong |
| 79 | + // to the last count elements of the source |
| 80 | + // sequence. We can emit it now. |
| 81 | + subscriber.onNext(deque.removeFirst()); |
128 | 82 | }
|
| 83 | + } |
129 | 84 |
|
130 |
| - })); |
131 |
| - } |
| 85 | + }; |
132 | 86 | }
|
133 | 87 |
|
134 | 88 | /**
|
135 | 89 | * Skip delivering values in the time window before the values.
|
136 |
| - * |
| 90 | + * |
137 | 91 | * @param <T>
|
138 | 92 | * the result value type
|
139 | 93 | */
|
|
0 commit comments