1/*
2 * Copyright (C) 2010 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 */
16package com.android.quicksearchbox.util;
17
18/**
19 * {@link NowOrLater} class that converts from one type to another.
20 */
21public abstract class NowOrLaterWrapper<A, B> implements NowOrLater<B> {
22
23    private final NowOrLater<A> mWrapped;
24
25    public NowOrLaterWrapper(NowOrLater<A> wrapped) {
26        mWrapped = wrapped;
27    }
28
29    public void getLater(final Consumer<? super B> consumer) {
30        mWrapped.getLater(new Consumer<A>(){
31            public boolean consume(A value) {
32                return consumer.consume(get(value));
33            }});
34    }
35
36    public B getNow() {
37        return get(mWrapped.getNow());
38    }
39
40    public boolean haveNow() {
41        return mWrapped.haveNow();
42    }
43
44    /**
45     * Perform the appropriate conversion. This will be called once for every call to
46     * {@link #getLater(Consumer)} or {@link #getNow()}. The thread that it's called on will depend
47     * on the behaviour of the wrapped object and the caller.
48     */
49    public abstract B get(A value);
50
51}
52