FilteredCallback.java revision 81308a22b0f64c6667f6c23adee9da520415bcb6
1/*
2 * Copyright (C) 2015 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
17package com.android.camera.async;
18
19import com.android.camera.util.Callback;
20
21import java.util.Objects;
22
23import javax.annotation.Nonnull;
24import javax.annotation.Nullable;
25import javax.annotation.ParametersAreNonnullByDefault;
26
27/**
28 * Wraps a callback by filtering out duplicate invocations.
29 */
30@ParametersAreNonnullByDefault
31public final class FilteredCallback<T> implements Callback<T> {
32    private final Callback<T> mCallback;
33    @Nullable
34    private T mLastValue;
35
36    public FilteredCallback(Callback<T> callback) {
37        mCallback = callback;
38        mLastValue = null;
39    }
40
41    @Override
42    public void onCallback(@Nonnull T result) {
43        if (Objects.equals(mLastValue, result)) {
44            return;
45        }
46        mLastValue = result;
47        mCallback.onCallback(result);
48    }
49}
50