This repository has been archived by the owner on Aug 31, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathProtectedModule.java
82 lines (70 loc) · 2.41 KB
/
ProtectedModule.java
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
package tc.oc.inject;
import javax.annotation.Nullable;
import com.google.inject.Binder;
import com.google.inject.Module;
/**
* A {@link Module} that uses a {@link ProtectedBinder}.
*
* This module must be installed through a {@link ProtectedBinder}, which must be created explicitly
* by calling {@link ProtectedBinder#newProtectedBinder(Binder)}. Attempting to install this module
* into a normal {@link Binder} fails with an error.
*
* Unlike {@link com.google.inject.PrivateModule}, installing this never creates a new {@link ProtectedBinder}
* automatically. This allows an entire tree of {@link ProtectedModule}s to share the same public binder.
*
* @see ProtectedBinder
*/
public abstract class ProtectedModule implements Module, ForwardingProtectedBinder {
protected void configure() {}
private final @Nullable Object moduleKey;
private @Nullable ProtectedBinder binder;
protected ProtectedModule(@Nullable Object moduleKey) {
this.moduleKey = moduleKey;
}
protected ProtectedModule() {
this(null);
}
@Override
public int hashCode() {
return moduleKey != null ? moduleKey.hashCode()
: super.hashCode();
}
@Override
public boolean equals(Object obj) {
if(moduleKey != null) {
return obj != null &&
getClass().equals(obj.getClass()) &&
moduleKey.equals(((ProtectedModule) obj).moduleKey);
} else {
return super.equals(obj);
}
}
@Override
public final ProtectedBinder forwardedBinder() {
return binder();
}
protected final ProtectedBinder binder() {
if(binder == null) {
throw new IllegalStateException("Binder is only usable during configuration");
}
return binder;
}
@Override
public final void configure(Binder binder) {
final ProtectedBinder old = this.binder;
this.binder = ProtectedBinderImpl.current(binder);
try {
if(this.binder != null) {
configure();
} else {
binder.skipSources(ProtectedModule.class).addError(
ProtectedModule.class.getSimpleName() +
" must be installed with a " +
ProtectedBinder.class.getSimpleName()
);
}
} finally {
this.binder = old;
}
}
}