Debian Codegraph @

libs/WindowManager/Jetpack/src/androidx/window/util/AcceptOnceConsumer.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2022 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 androidx.window.util;
18
19 import android.annotation.NonNull;
20
21 import java.util.function.Consumer;
22
23 /**
24 * A base class that works with {@link BaseDataProducer} to add/remove a consumer that should
25 * only be used once when {@link BaseDataProducer#notifyDataChanged} is called.
26 * @param <T> The type of data this producer returns through {@link DataProducer#getData}.
27 */
28 public class AcceptOnceConsumer<T> implements Consumer<T> {
29 private final Consumer<T> mCallback;
30 private final AcceptOnceProducerCallback<T> mProducer;
31
32 public AcceptOnceConsumer(@NonNull AcceptOnceProducerCallback<T> producer,
33 @NonNull Consumer<T> callback) {
34 mProducer = producer;
35 mCallback = callback;
36 }
37
38 @Override
39 public void accept(@NonNull T t) {
40 mCallback.accept(t);
41 mProducer.onConsumerReadyToBeRemoved(this);
42 }
43
44 /**
45 * Interface to allow the {@link AcceptOnceConsumer} to notify the client that created it,
46 * when it is ready to be removed. This allows the client to remove the consumer object
47 * when it deems it is safe to do so.
48 * @param <T> The type of data this callback accepts through {@link #onConsumerReadyToBeRemoved}
49 */
50 public interface AcceptOnceProducerCallback<T> {
51
52 /**
53 * Notifies that the given {@code callback} is ready to be removed
54 */
55 void onConsumerReadyToBeRemoved(Consumer<T> callback);
56 }
57 }