summaryrefslogtreecommitdiffstats
path: root/sangria-listbinder/src/main/java/com/tavianator/sangria/listbinder/ListBinder.java
blob: 003e25e88e5d7514a63f88d75aa368300b271574 (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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/****************************************************************************
 * 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.listbinder;

import java.lang.annotation.Annotation;
import java.util.*;
import javax.inject.Inject;
import javax.inject.Provider;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.ListMultimap;
import com.google.inject.AbstractModule;
import com.google.inject.Binder;
import com.google.inject.CreationException;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import com.google.inject.binder.LinkedBindingBuilder;
import com.google.inject.multibindings.Multibinder;
import com.google.inject.spi.Message;
import com.google.inject.util.Types;

import com.tavianator.sangria.core.PotentialAnnotation;
import com.tavianator.sangria.core.PrettyTypes;
import com.tavianator.sangria.core.Priority;
import com.tavianator.sangria.core.TypeLiterals;
import com.tavianator.sangria.core.UniqueAnnotations;

/**
 * A multi-binder with guaranteed order.
 *
 * <p>
 * {@link ListBinder} is much like {@link Multibinder}, except it provides a guaranteed iteration order, and binds a
 * {@link List} instead of a {@link Set}. For example:
 * </p>
 *
 * <pre>
 * ListBinder&lt;String&gt; listBinder = ListBinder.build(binder(), String.class)
 *         .withDefaultPriority();
 * listBinder.addBinding().toInstance("a");
 * listBinder.addBinding().toInstance("b");
 * </pre>
 *
 * <p>
 * This will create a binding for a {@code List<String>}, which contains {@code "a"} followed by {@code "b"}. It also
 * creates a binding for {@code List<Provider<String>>} &mdash; this may be useful in more advanced cases to allow list
 * elements to be lazily loaded.
 * </p>
 *
 * <p>To add an annotation to the list binding, simply write this:</p>
 *
 * <pre>
 * ListBinder&lt;String&gt; listBinder = ListBinder.build(binder(), String.class)
 *         .annotatedWith(Names.named("name"))
 *         .withDefaultPriority();
 * </pre>
 *
 * <p>
 * and the created binding will be {@code @Named("name") List<String>} instead.
 * </p>
 *
 * <p>
 * For large lists, it may be helpful to split up their specification across different modules. This is accomplished by
 * specifying <em>priorities</em> for the {@link ListBinder}s when they are created. For example:
 * </p>
 *
 * <pre>
 * // In some module
 * ListBinder&lt;String&gt; listBinder1 = ListBinder.build(binder(), String.class)
 *         .withPriority(0);
 * listBinder1.addBinding().toInstance("a");
 * listBinder1.addBinding().toInstance("b");
 *
 * // ... some other module
 * ListBinder&lt;String&gt; listBinder2 = ListBinder.build(binder(), String.class)
 *         .withPriority(1);
 * listBinder2.addBinding().toInstance("c");
 * listBinder2.addBinding().toInstance("d");
 * </pre>
 *
 * <p>
 * The generated list will contain {@code "a"}, {@code "b"}, {@code "c"}, {@code "d"}, in order. This happens because
 * the first {@link ListBinder} had a smaller priority, so its entries come first. For more information about the
 * priority system, see {@link Priority}.
 * </p>
 *
 * @param <T> The type of the list element.
 * @author Tavian Barnes (tavianator@tavianator.com)
 * @version 1.1
 * @since 1.1
 */
public class ListBinder<T> {
    private static final Class<?>[] SKIPPED_SOURCES = {
            ListBinder.class,
            BuilderImpl.class,
    };

    private final Binder binder;
    private final Multibinder<ListElement<T>> multibinder;
    private final Multibinder<ListBinderErrors<T>> errorMultibinder;
    private final TypeLiteral<T> entryType;
    private final Key<List<T>> listKey;
    private final Key<List<Provider<T>>> listOfProvidersKey;
    private final Key<Set<ListElement<T>>> setKey;
    private final Key<Set<ListBinderErrors<T>>> errorSetKey;
    private final PotentialAnnotation potentialAnnotation;
    private final Priority initialPriority;
    private Priority priority;

    private ListBinder(
            Binder binder,
            TypeLiteral<T> entryType,
            PotentialAnnotation potentialAnnotation,
            Priority initialPriority) {
        this.binder = binder;
        this.entryType = entryType;

        TypeLiteral<ListElement<T>> elementType = listElementOf(entryType);
        TypeLiteral<ListBinderErrors<T>> errorsType = listBinderErrorsOf(entryType);
        this.listKey = potentialAnnotation.getKey(TypeLiterals.listOf(entryType));
        this.listOfProvidersKey = potentialAnnotation.getKey(TypeLiterals.listOf(TypeLiterals.providerOf(entryType)));
        this.setKey = potentialAnnotation.getKey(TypeLiterals.setOf(elementType));
        this.errorSetKey = potentialAnnotation.getKey(TypeLiterals.setOf(errorsType));
        this.multibinder = potentialAnnotation.accept(new MultibinderMaker<>(binder, elementType));
        this.errorMultibinder = potentialAnnotation.accept(new MultibinderMaker<>(binder, errorsType));

        this.potentialAnnotation = potentialAnnotation;
        this.priority = this.initialPriority = initialPriority;
    }

    @SuppressWarnings("unchecked")
    private static <T> TypeLiteral<ListElement<T>> listElementOf(TypeLiteral<T> type) {
        return (TypeLiteral<ListElement<T>>)TypeLiteral.get(Types.newParameterizedType(ListElement.class, type.getType()));
    }

    @SuppressWarnings("unchecked")
    private static <T> TypeLiteral<ListBinderErrors<T>> listBinderErrorsOf(TypeLiteral<T> type) {
        return (TypeLiteral<ListBinderErrors<T>>)TypeLiteral.get(Types.newParameterizedType(ListBinderErrors.class, type.getType()));
    }

    /**
     * {@link PotentialAnnotation.Visitor} that makes {@link Multibinder}s with the given annotation.
     */
    private static class MultibinderMaker<T> implements PotentialAnnotation.Visitor<Multibinder<T>> {
        private final Binder binder;
        private final TypeLiteral<T> type;

        MultibinderMaker(Binder binder, TypeLiteral<T> type) {
            this.binder = binder;
            this.type = type;
        }

        @Override
        public Multibinder<T> visitNoAnnotation() {
            return Multibinder.newSetBinder(binder, type);
        }

        @Override
        public Multibinder<T> visitAnnotationType(Class<? extends Annotation> annotationType) {
            return Multibinder.newSetBinder(binder, type, annotationType);
        }

        @Override
        public Multibinder<T> visitAnnotationInstance(Annotation annotation) {
            return Multibinder.newSetBinder(binder, type, annotation);
        }
    }

    /**
     * Start building a {@link ListBinder}.
     *
     * @param binder The current binder, usually {@link AbstractModule#binder()}.
     * @param type   The type of the list element.
     * @param <T>    The type of the list element.
     * @return A fluent builder.
     */
    public static <T> AnnotatedListBinderBuilder<T> build(Binder binder, Class<T> type) {
        return build(binder, TypeLiteral.get(type));
    }

    /**
     * Start building a {@link ListBinder}.
     *
     * @param binder The current binder, usually {@link AbstractModule#binder()}.
     * @param type   The type of the list element.
     * @param <T>    The type of the list element.
     * @return A fluent builder.
     */
    public static <T> AnnotatedListBinderBuilder<T> build(Binder binder, TypeLiteral<T> type) {
        return new BuilderImpl<>(binder.skipSources(SKIPPED_SOURCES), type, PotentialAnnotation.none());
    }

    private static class BuilderImpl<T> implements AnnotatedListBinderBuilder<T> {
        private final Binder binder;
        private final TypeLiteral<T> entryType;
        private final PotentialAnnotation potentialAnnotation;

        BuilderImpl(Binder binder, TypeLiteral<T> type, PotentialAnnotation potentialAnnotation) {
            this.binder = binder;
            this.entryType = type;
            this.potentialAnnotation = potentialAnnotation;
        }

        @Override
        public ListBinderBuilder<T> annotatedWith(Class<? extends Annotation> annotationType) {
            return new BuilderImpl<>(binder, entryType, potentialAnnotation.annotatedWith(annotationType));
        }

        @Override
        public ListBinderBuilder<T> annotatedWith(Annotation annotation) {
            return new BuilderImpl<>(binder, entryType, potentialAnnotation.annotatedWith(annotation));
        }

        @Override
        public ListBinder<T> withDefaultPriority() {
            return create(Priority.getDefault());
        }

        @Override
        public ListBinder<T> withPriority(int weight, int... weights) {
            return create(Priority.create(weight, weights));
        }

        private ListBinder<T> create(Priority priority) {
            ListBinder<T> listBinder = new ListBinder<>(binder, entryType, potentialAnnotation, priority);

            // Add the delayed errors
            Message duplicateBindersError = new Message(PrettyTypes.format("Duplicate %s", listBinder));
            Message conflictingDefaultExplicitError;
            if (priority.isDefault()) {
                conflictingDefaultExplicitError = new Message(PrettyTypes.format("%s conflicts with ListBinder with explicit priority", listBinder));
            } else {
                conflictingDefaultExplicitError = new Message(PrettyTypes.format("%s conflicts with ListBinder with default priority", listBinder));
            }
            listBinder.errorMultibinder.addBinding().toInstance(new ListBinderErrors<T>(
                    priority,
                    duplicateBindersError,
                    conflictingDefaultExplicitError));

            // Set up the exposed bindings
            binder.bind(listBinder.listOfProvidersKey)
                    .toProvider(new ListOfProvidersProvider<>(listBinder));
            binder.bind(listBinder.listKey)
                    .toProvider(new ListOfProvidersAdapter<>(listBinder.listOfProvidersKey));

            return listBinder;
        }
    }

    /**
     * Provider implementation for {@code List&lt;Provider&lt;T&gt;&gt;}.
     */
    private static class ListOfProvidersProvider<T> implements Provider<List<Provider<T>>> {
        private final Key<Set<ListElement<T>>> setKey;
        private final Key<Set<ListBinderErrors<T>>> errorSetKey;
        private final Priority priority;
        private List<Provider<T>> providers;

        ListOfProvidersProvider(ListBinder<T> listBinder) {
            this.setKey = listBinder.setKey;
            this.errorSetKey = listBinder.errorSetKey;
            this.priority = listBinder.initialPriority;
        }

        @Inject
        void inject(Injector injector) {
            validate(injector);
            initialize(injector);
        }

        private void validate(Injector injector) {
            // Note that here we don't report all errors at once, correctness relies on Guice injecting even providers
            // that get de-duplicated. This way, all errors are attached to the right source.

            List<Message> messages = new ArrayList<>();

            // Get the errors into a multimap by priority
            Set<ListBinderErrors<T>> errorSet = injector.getInstance(errorSetKey);
            ListMultimap<Priority, ListBinderErrors<T>> errorMap = ArrayListMultimap.create();
            for (ListBinderErrors<T> errors : errorSet) {
                errorMap.put(errors.priority, errors);
            }

            // Check for duplicate priorities
            List<ListBinderErrors<T>> ourPriorityErrors = errorMap.get(priority);
            ListBinderErrors<T> ourErrors = ourPriorityErrors.get(0);
            if (ourPriorityErrors.size() > 1) {
                messages.add(ourErrors.duplicateBindersError);
            }

            // Check for default and non-default priorities
            if (errorMap.containsKey(Priority.getDefault()) && errorMap.keySet().size() > 1) {
                messages.add(ourErrors.conflictingDefaultExplicitError);
            }

            if (!messages.isEmpty()) {
                throw new CreationException(messages);
            }
        }

        private void initialize(final Injector injector) {
            Set<ListElement<T>> set = injector.getInstance(setKey);
            List<ListElement<T>> elements = new ArrayList<>(set);
            Collections.sort(elements);

            this.providers = FluentIterable.from(elements)
                    .transform(new Function<ListElement<T>, Provider<T>>() {
                        @Override
                        public Provider<T> apply(ListElement<T> input) {
                            return injector.getProvider(input.key);
                        }
                    })
                    .toList();
        }

        @Override
        public List<Provider<T>> get() {
            return providers;
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            } else if (!(obj instanceof ListOfProvidersProvider)) {
                return false;
            }

            ListOfProvidersProvider<?> other = (ListOfProvidersProvider<?>)obj;
            return setKey.equals(other.setKey);
        }

        @Override
        public int hashCode() {
            return setKey.hashCode();
        }
    }

    /**
     * Provider implementation for {@code List&lt;T&gt;}, in terms of {@code List&lt;Provider&lt;T&gt;&gt;}.
     */
    private static class ListOfProvidersAdapter<T> implements Provider<List<T>> {
        private final Key<List<Provider<T>>> providerListKey;
        private Provider<List<Provider<T>>> provider;

        ListOfProvidersAdapter(Key<List<Provider<T>>> providerListKey) {
            this.providerListKey = providerListKey;
        }

        @Inject
        void inject(final Injector injector) {
            this.provider = injector.getProvider(providerListKey);
        }

        @Override
        public List<T> get() {
            return FluentIterable.from(provider.get())
                    .transform(new Function<Provider<T>, T>() {
                        @Override
                        public T apply(Provider<T> input) {
                            return input.get();
                        }
                    })
                    .toList();
        }

        @Override
        public boolean equals(Object obj) {
            if (obj == this) {
                return true;
            } else if (!(obj instanceof ListOfProvidersAdapter)) {
                return false;
            }

            ListOfProvidersAdapter<?> other = (ListOfProvidersAdapter<?>)obj;
            return providerListKey.equals(other.providerListKey);
        }

        @Override
        public int hashCode() {
            return Objects.hashCode(providerListKey);
        }
    }

    /**
     * Add an entry to the list.
     *
     * <p>
     * The entry will be added in order for this {@link ListBinder} instance. Between different {@link ListBinder}s, the
     * order is determined by the {@link ListBinder}'s {@link Priority}.
     * </p>
     *
     * @return A fluent binding builder.
     */
    public LinkedBindingBuilder<T> addBinding() {
        Key<T> key = Key.get(entryType, UniqueAnnotations.create());
        multibinder.addBinding().toInstance(new ListElement<>(key, priority));
        priority = priority.next();
        return binder.bind(key);
    }

    @Override
    public String toString() {
        return PrettyTypes.format("ListBinder<%s>%s with %s",
                entryType,
                (potentialAnnotation.hasAnnotation() ? " annotated with " + potentialAnnotation : ""),
                initialPriority);
    }
}