2
2
3
3
import static com .google .common .base .Preconditions .checkArgument ;
4
4
5
- import java .util .Arrays ;
6
- import java .util .EnumSet ;
7
5
import java .util .Set ;
8
6
import java .util .concurrent .ThreadFactory ;
9
7
10
8
import javax .inject .Singleton ;
11
9
10
+ import com .google .common .collect .ImmutableSet ;
12
11
import com .google .common .util .concurrent .ThreadFactoryBuilder ;
13
12
import com .google .inject .AbstractModule ;
14
13
import com .google .inject .Binder ;
15
14
import com .google .inject .Module ;
16
15
import com .google .inject .Provides ;
17
16
18
- import de .skuzzle .inject .async .methods .AsyncModule ;
19
17
import de .skuzzle .inject .async .methods .annotation .Async ;
20
- import de .skuzzle .inject .async .schedule .ScheduleModule ;
21
18
import de .skuzzle .inject .async .schedule .annotation .Scheduled ;
22
19
23
20
/**
35
32
* </pre>
36
33
* <p>
37
34
* You may choose to only enable scheduling OR async methods in case you do not need both.
38
- * See {@link #enableFeaturesFor(Binder, Feature ...)} and
39
- * {@link #createModuleWithFeatures(Feature ...)}.
35
+ * See {@link #enableFeaturesFor(Binder, DefaultFeatures ...)} and
36
+ * {@link #createModuleWithFeatures(DefaultFeatures ...)}.
40
37
* <p>
41
38
* Please see the JavaDoc of the {@link Async} and {@link Scheduled} annotation for
42
39
* further usage information.
@@ -58,15 +55,17 @@ private GuiceAsync() {
58
55
* @param binder The binder to register with.
59
56
*/
60
57
public static void enableFor (Binder binder ) {
61
- enableFeaturesFor (binder , Feature .ASYNC , Feature .SCHEDULE );
58
+ enableFeaturesFor (binder , DefaultFeatures .ASYNC , DefaultFeatures .SCHEDULE );
62
59
}
63
60
64
61
/**
65
- * Enable support for the given {@link Feature features}. Allows to separately enable
66
- * support for async or scheduled.
62
+ * Enable support for the given {@link DefaultFeatures features}. Allows to separately
63
+ * enable support for async or scheduled.
67
64
*
68
65
* @param binder The binder to register with.
69
66
* @param features The features to enable.
67
+ * @since 2.0.0
68
+ * @see DefaultFeatures
70
69
*/
71
70
public static void enableFeaturesFor (Binder binder , Feature ... features ) {
72
71
checkArgument (binder != null , "binder must not be null" );
@@ -81,52 +80,44 @@ public static void enableFeaturesFor(Binder binder, Feature... features) {
81
80
* @since 0.2.0
82
81
*/
83
82
public static Module createModule () {
84
- return createModuleWithFeatures (Feature .ASYNC , Feature .SCHEDULE );
83
+ return createModuleWithFeatures (DefaultFeatures .ASYNC , DefaultFeatures .SCHEDULE );
85
84
}
86
85
87
86
/**
88
- * Creates a module taht can be used to enable the given features.
87
+ * Creates a module that can be used to enable the given features.
89
88
*
90
89
* @param features The features to enable.
91
90
* @return The module.
91
+ * @since 2.0.0
92
92
*/
93
93
public static Module createModuleWithFeatures (Feature ... features ) {
94
94
final GuiceAsync principal = new GuiceAsync ();
95
- final EnumSet <Feature > featureSet = EnumSet .copyOf (Arrays . asList ( features ) );
95
+ final Set <Feature > featureSet = ImmutableSet .copyOf (features );
96
96
return new GuiceAsyncModule (principal , featureSet );
97
97
}
98
98
99
99
private static final class GuiceAsyncModule extends AbstractModule {
100
100
101
101
private final GuiceAsync principal ;
102
- private final Set <Feature > features ;
102
+ private final Set <Feature > enabledFeatures ;
103
103
104
104
public GuiceAsyncModule (GuiceAsync principal , Set <Feature > features ) {
105
105
checkArgument (!features .isEmpty (), "Set of features must not be empty" );
106
106
this .principal = principal ;
107
- this .features = features ;
108
- }
109
-
110
- private boolean hasFeature (Feature feature ) {
111
- return features .contains (feature );
107
+ this .enabledFeatures = features ;
112
108
}
113
109
114
110
@ Override
115
111
protected void configure () {
116
- if (hasFeature (Feature .ASYNC )) {
117
- install (new AsyncModule (principal ));
118
- }
119
- if (hasFeature (Feature .SCHEDULE )) {
120
- install (new ScheduleModule (principal ));
121
- }
112
+ enabledFeatures .forEach (feature -> feature .installModuleTo (binder (), principal ));
122
113
bind (GuiceAsyncService .class ).to (GuiceAsyncServiceImpl .class ).in (Singleton .class );
123
114
}
124
115
125
116
@ Provides
126
117
@ Singleton
127
118
@ DefaultBinding
128
119
Set <Feature > provideFeatures () {
129
- return features ;
120
+ return enabledFeatures ;
130
121
}
131
122
132
123
@ Provides
0 commit comments