MATSIM
AbstractModalQSimModule.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 
26 import jakarta.inject.Provider;
27 
31 
32 import com.google.inject.Key;
33 import com.google.inject.TypeLiteral;
34 import com.google.inject.binder.LinkedBindingBuilder;
35 
39 public abstract class AbstractModalQSimModule<M extends Annotation> extends AbstractQSimModule {
40  private final String mode;
42 
43  protected AbstractModalQSimModule(String mode, ModalAnnotationCreator<M> modalAnnotationCreator) {
44  this.mode = mode;
45  this.modalAnnotationCreator = modalAnnotationCreator;
46  }
47 
48  protected final String getMode() {
49  return mode;
50  }
51 
52  protected final M getModalAnnotation() {
53  return modalAnnotationCreator.mode(mode);
54  }
55 
56  protected final <T> Key<T> modalKey(Class<T> type) {
57  return modalAnnotationCreator.key(type, mode);
58  }
59 
60  protected final <T> Key<T> modalKey(TypeLiteral<T> typeLiteral) {
61  return modalAnnotationCreator.key(typeLiteral, mode);
62  }
63 
64  protected final <T> LinkedBindingBuilder<T> bindModal(Class<T> type) {
65  return bind(modalKey(type));
66  }
67 
68  protected final <T> LinkedBindingBuilder<T> bindModal(TypeLiteral<T> typeLiteral) {
69  return bind(modalKey(typeLiteral));
70  }
71 
90  @Deprecated
91  protected final LinkedBindingBuilder<MobsimListener> addMobsimListenerBinding() {
92  throw new UnsupportedOperationException(
93  "Very likely you wanted to call addQSimComponentBinding() or addModalQSimComponentBinding()");
94  }
95 
96  protected final LinkedBindingBuilder<QSimComponent> addModalQSimComponentBinding() {
98  }
99 
100  protected final <T extends QSimComponent> void addModalComponent(Class<T> componentClass, Key<? extends T> key) {
101  bindModal(componentClass).to(key).asEagerSingleton();
102  addModalQSimComponentBinding().to(modalKey(componentClass));
103  }
104 
105  protected final <T extends QSimComponent> void addModalComponent(Class<T> componentClass,
106  Provider<? extends T> componentProvider) {
107  bindModal(componentClass).toProvider(componentProvider).asEagerSingleton();
108  addModalQSimComponentBinding().to(modalKey(componentClass));
109  }
110 
111  protected final <T extends QSimComponent> void addModalComponent(Class<T> componentClass,
112  Class<? extends T> implementation) {
113  bindModal(componentClass).to(implementation).asEagerSingleton();
114  addModalQSimComponentBinding().to(modalKey(componentClass));
115  }
116 
117  protected final <T extends QSimComponent> void addModalComponent(Class<T> componentClass) {
118  addModalComponent(componentClass, componentClass);
119  }
120 
121  protected final <T> Provider<T> modalProvider(Function<ModalProviders.InstanceGetter<M>, T> delegate) {
122  return ModalProviders.createProvider(mode, modalAnnotationCreator, delegate);
123  }
124 }
final ModalAnnotationCreator< M > modalAnnotationCreator
static< M extends Annotation, T > Provider< T > createProvider(String mode, ModalAnnotationCreator< M > modalAnnotationCreator, Function< InstanceGetter< M >, T > delegate)
final< T > LinkedBindingBuilder< T > bindModal(Class< T > type)
AbstractModalQSimModule(String mode, ModalAnnotationCreator< M > modalAnnotationCreator)
final< T extends QSimComponent > void addModalComponent(Class< T > componentClass, Key<? extends T > key)
final< T extends QSimComponent > void addModalComponent(Class< T > componentClass, Provider<? extends T > componentProvider)
final LinkedBindingBuilder< QSimComponent > addQSimComponentBinding(Annotation annotation)
final< T > Key< T > modalKey(Class< T > type)
default< T > Key< T > key(Class< T > type, String mode)
final< T > Provider< T > modalProvider(Function< ModalProviders.InstanceGetter< M >, T > delegate)
final< T > LinkedBindingBuilder< T > bindModal(TypeLiteral< T > typeLiteral)
final< T extends QSimComponent > void addModalComponent(Class< T > componentClass)
final< T > Key< T > modalKey(TypeLiteral< T > typeLiteral)
final< T extends QSimComponent > void addModalComponent(Class< T > componentClass, Class<? extends T > implementation)
final LinkedBindingBuilder< MobsimListener > addMobsimListenerBinding()
final LinkedBindingBuilder< QSimComponent > addModalQSimComponentBinding()