MATSIM
ModalProviders.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.Inject;
27 
28 import com.google.inject.Injector;
29 import com.google.inject.Key;
30 import com.google.inject.Provider;
31 import com.google.inject.TypeLiteral;
32 import com.google.inject.name.Names;
33 
37 public class ModalProviders {
38  public static <M extends Annotation, T> Provider<T> createProvider(String mode,
39  ModalAnnotationCreator<M> modalAnnotationCreator, Function<InstanceGetter<M>, T> delegate) {
40  return new Provider<>() {
41  @Inject
42  private Injector injector;
43 
44  @Override
45  public T get() {
46  return delegate.apply(new InstanceGetter<>(mode, modalAnnotationCreator, injector));
47  }
48  };
49  }
50 
51  public static final class InstanceGetter<M extends Annotation> {
52  private final String mode;
53  private final Injector injector;
55 
56  private InstanceGetter(String mode, ModalAnnotationCreator<M> modalAnnotationCreator, Injector injector) {
57  this.mode = mode;
58  this.injector = injector;
59  this.modalAnnotationCreator = modalAnnotationCreator;
60  }
61 
62  public <T> T get(Class<T> type) {
63  return injector.getInstance(type);
64  }
65 
66  public <T> T get(Key<T> key) {
67  return injector.getInstance(key);
68  }
69 
70  public <T> T get(TypeLiteral<T> typeLiteral) {
71  return injector.getInstance(Key.get(typeLiteral));
72  }
73 
74  public <T> T getModal(Class<T> type) {
75  return injector.getInstance(modalAnnotationCreator.key(type, mode));
76  }
77 
78  public <T> T getModal(TypeLiteral<T> typeLiteral) {
79  return injector.getInstance(modalAnnotationCreator.key(typeLiteral, mode));
80  }
81 
82  public <T> T getNamed(Class<T> type, String name) {
83  return injector.getInstance(Key.get(type, Names.named(name)));
84  }
85 
86  public <T> T getNamed(TypeLiteral<T> typeLiteral, String name) {
87  return injector.getInstance(Key.get(typeLiteral, Names.named(name)));
88  }
89  }
90 
91  public static abstract class AbstractProvider<M extends Annotation, T> implements Provider<T> {
92  private final String mode;
94 
95  @Inject
96  private Injector injector;
97 
98  protected AbstractProvider(String mode, ModalAnnotationCreator<M> modalAnnotationCreator) {
99  this.mode = mode;
100  this.modalAnnotationCreator = modalAnnotationCreator;
101  }
102 
103  protected <I> I getModalInstance(Class<I> type) {
104  return injector.getInstance(modalAnnotationCreator.key(type, mode));
105  }
106 
107  protected <I> I getModalInstance(TypeLiteral<I> typeLiteral) {
108  return injector.getInstance(modalAnnotationCreator.key(typeLiteral, mode));
109  }
110 
111  protected <I> Provider<I> getModalProvider(Class<I> type) {
112  return injector.getProvider(modalAnnotationCreator.key(type, mode));
113  }
114 
115  protected String getMode() {
116  return mode;
117  }
118  }
119 }
final ModalAnnotationCreator< M > modalAnnotationCreator
final ModalAnnotationCreator< M > modalAnnotationCreator
static< M extends Annotation, T > Provider< T > createProvider(String mode, ModalAnnotationCreator< M > modalAnnotationCreator, Function< InstanceGetter< M >, T > delegate)
InstanceGetter(String mode, ModalAnnotationCreator< M > modalAnnotationCreator, Injector injector)
AbstractProvider(String mode, ModalAnnotationCreator< M > modalAnnotationCreator)
default< T > Key< T > key(Class< T > type, String mode)