Java Code Examples for com.google.common.collect.ClassToInstanceMap#putInstance()

The following examples show how to use com.google.common.collect.ClassToInstanceMap#putInstance() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BuildModelContext.java    From ok-gradle with Apache License 2.0 5 votes vote down vote up
@NotNull
public <T extends BuildModelNotification> T getNotificationForType(@NotNull GradleDslFile file,
                                                                   @NotNull NotificationTypeReference<T> type) {
  ClassToInstanceMap<BuildModelNotification> notificationMap =
    myNotifications.computeIfAbsent(file, (f) -> MutableClassToInstanceMap.create());
  if (notificationMap.containsKey(type.getClazz())) {
    return notificationMap.getInstance(type.getClazz());
  }
  else {
    T notification = type.getConstructor().produce();
    notificationMap.putInstance(type.getClazz(), notification);
    return notification;
  }
}
 
Example 2
Source File: ClassToInstanceMapUnitTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenClassToInstanceMap_whenPutCalled_returnPreviousElementUpperBound() {
    ClassToInstanceMap<Action> map = MutableClassToInstanceMap.create();
    map.put(Save.class, new Save());
    // Put again to get previous value returned
    Action action = map.put(Save.class, new Save());
    assertTrue(action instanceof Save);

    // Use putInstance to avoid casting
    Save save = map.putInstance(Save.class, new Save());
}
 
Example 3
Source File: StaticEnvironment.java    From immutables with Apache License 2.0 5 votes vote down vote up
static <T extends Completable> T getInstance(Class<T> type, Supplier<T> supplier) {
  ClassToInstanceMap<Completable> components = state().components;
  @Nullable T instance = components.getInstance(type);
  if (instance == null) {
    instance = supplier.get();
    components.putInstance(type, instance);
  }
  return instance;
}