summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/apkzlib/utils/CachedSupplier.java
blob: b8c9a53765e4dd5ca93779f2284fd6b0492f491d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/*
 * Copyright (C) 2016 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.apkzlib.utils;

import com.android.annotations.NonNull;

import java.util.function.Supplier;

/**
 * Supplier that will cache a computed value and always supply the same value. It can be used to
 * lazily compute data. For example:
 * <pre>
 * CachedSupplier&lt;Integer&gt; value = new CachedSupplier&lt;&gt;(() -> {
 *     Integer result;
 *     // Do some expensive computation.
 *     return result;
 * });
 *
 * if (a) {
 *     // We need the result of the expensive computation.
 *     Integer r = value.get();
 * }
 *
 * if (b) {
 *     // We also need the result of the expensive computation.
 *     Integer r = value.get();
 * }
 *
 * // If neither a nor b are true, we avoid doing the computation at all.
 * </pre>
 */
public class CachedSupplier<T> {

    /**
     * The cached data, {@code null} if computation resulted in {@code null}.
     */
    private T cached;

    /**
     * Is the current data in {@link #cached} valid?
     */
    private boolean valid;

    /**
     * Actual supplier of data, if computation is needed.
     */
    @NonNull
    private final Supplier<T> supplier;

    /**
     * Creates a new supplier.
     */
    public CachedSupplier(@NonNull Supplier<T> supplier) {
        valid = false;
        this.supplier = supplier;
    }


    /**
     * Obtains the value.
     *
     * @return the value, either cached (if one exists) or computed
     */
    public synchronized T get() {
        if (!valid) {
            cached = supplier.get();
            valid = true;
        }

        return cached;
    }

    /**
     * Resets the cache forcing a {@code get()} on the supplier next time {@link #get()} is invoked.
     */
    public synchronized void reset() {
        cached = null;
        valid = false;
    }

    /**
     * In some cases, we may be able to precompute the cache value (or load it from somewhere we
     * had previously stored it). This method allows the cache value to be loaded.
     *
     * <p>If this method is invoked, then an invocation of {@link #get()} will not trigger an
     * invocation of the supplier provided in the constructor.
     *
     * @param t the new cache contents; will replace any currently cache content, if one exists
     */
    public synchronized void precomputed(T t) {
        cached = t;
        valid = true;
    }

    /**
     * Checks if the contents of the cache are valid.
     *
     * @return are there valid contents in the cache?
     */
    public synchronized boolean isValid() {
        return valid;
    }
}