MATSIM
AbstractModalModule.java
Go to the documentation of this file.
1 /*
2  * *********************************************************************** *
3  * project: org.matsim.*
4  * *********************************************************************** *
5  * *
6  * copyright : (C) 2021 by the members listed in the COPYING, *
7  * LICENSE and WARRANTY file. *
8  * email : info at matsim dot org *
9  * *
10  * *********************************************************************** *
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * See also COPYING, LICENSE and WARRANTY file *
17  * *
18  * *********************************************************************** *
19  */
20 
21 package org.matsim.core.modal;
22 
23 import java.lang.annotation.Annotation;
24 import java.util.function.Function;
25 
27 
28 import com.google.inject.Key;
29 import com.google.inject.Provider;
30 import com.google.inject.TypeLiteral;
31 import com.google.inject.binder.LinkedBindingBuilder;
32 import com.google.inject.multibindings.MapBinder;
33 
37 public abstract class AbstractModalModule<M extends Annotation> extends AbstractModule {
38  private final String mode;
40 
41  protected AbstractModalModule(String mode, ModalAnnotationCreator<M> modalAnnotationCreator) {
42  this.mode = mode;
43  this.modalAnnotationCreator = modalAnnotationCreator;
44  }
45 
46  protected String getMode() {
47  return mode;
48  }
49 
50  protected <T> Key<T> modalKey(Class<T> type) {
51  return modalAnnotationCreator.key(type, mode);
52  }
53 
54  protected <T> Key<T> modalKey(TypeLiteral<T> typeLiteral) {
55  return modalAnnotationCreator.key(typeLiteral, mode);
56  }
57 
58  protected <T> LinkedBindingBuilder<T> bindModal(Class<T> type) {
59  return bind(modalKey(type));
60  }
61 
62  protected <T> LinkedBindingBuilder<T> bindModal(TypeLiteral<T> typeLiteral) {
63  return bind(modalKey(typeLiteral));
64  }
65 
66  protected <K, V> MapBinder<K, V> modalMapBinder(Class<K> keyType, Class<V> valueType) {
67  return MapBinder.newMapBinder(binder(), keyType, valueType, modalAnnotationCreator.mode(getMode()));
68  }
69 
70  protected <K, V> MapBinder<K, V> modalMapBinder(TypeLiteral<K> keyType, TypeLiteral<V> valueType) {
71  return MapBinder.newMapBinder(binder(), keyType, valueType, modalAnnotationCreator.mode(getMode()));
72  }
73 
74  protected <T> Provider<T> modalProvider(Function<ModalProviders.InstanceGetter<M>, T> delegate) {
75  return ModalProviders.createProvider(mode, modalAnnotationCreator, delegate);
76  }
77 }
final ModalAnnotationCreator< M > modalAnnotationCreator
static< M extends Annotation, T > Provider< T > createProvider(String mode, ModalAnnotationCreator< M > modalAnnotationCreator, Function< InstanceGetter< M >, T > delegate)
AbstractModalModule(String mode, ModalAnnotationCreator< M > modalAnnotationCreator)
default< T > Key< T > key(Class< T > type, String mode)