summaryrefslogtreecommitdiffstats
path: root/sangria-lazy/src/main/java/com/tavianator/sangria/lazy/Lazy.java
blob: adf531c16a21c1ff4811088a082b6a41512d63c0 (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
/****************************************************************************
 * Sangria                                                                  *
 * Copyright (C) 2014 Tavian Barnes <tavianator@tavianator.com>             *
 *                                                                          *
 * 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.tavianator.sangria.lazy;

import javax.inject.Inject;
import javax.inject.Provider;

/**
 * A lazily-loaded dependency. Like a {@link Provider}, calling {@link #get()} will produce an instance of {@code T}.
 * Unlike a {@link Provider}, the same instance will be returned for every future call to {@link #get()}. Different
 * {@code Lazy} instances are independent and will return different instances from {@link #get()}.
 *
 * <p>
 * {@link Lazy} works automatically for unqualified bindings, as long as just-in-time bindings are enabled. For
 * qualified bindings, or if explicit bindings are requred, use {@link LazyBinder}:
 * </p>
 *
 * <pre>
 * // Either separately...
 * bind(Dependency.class)
 *         .annotatedWith(Names.named("name"))
 *         .to(RealDependency.class);
 *
 * LazyBinder.create(binder())
 *         .bind(Dependency.class)
 *         .annotatedWith(Names.named("name"));
 *
 * // ... or in one go
 * LazyBinder.create(binder())
 *         .bind(Dependency.class)
 *         .annotatedWith(Names.named("name"))
 *         .to(RealDependency.class);
 *
 * ...
 *
 * {@literal @}Inject {@literal @}Named("name") Lazy&lt;Dependency&gt; lazy;
 * </pre>
 *
 * @author Tavian Barnes (tavianator@tavianator.com)
 * @version 1.2
 * @since 1.2
 */
public final class Lazy<T> {
    private final Provider<T> provider;
    private volatile T instance = null;

    @Inject
    Lazy(Provider<T> provider) {
        this.provider = provider;
    }

    /**
     * @return A lazily-produced value of type {@code T}.
     */
    public T get() {
        // Double-checked locking
        if (instance == null) {
            synchronized (this) {
                if (instance == null) {
                    instance = provider.get();
                }
            }
        }
        return instance;
    }
}