All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cloud.prefab.context.CompositeContextStore Maven / Gradle / Ivy

Go to download

API Client for https://prefab.cloud: rate limits, feature flags and semaphores as a service

There is a newer version: 0.3.23
Show newest version
package cloud.prefab.context;

import java.util.Arrays;
import java.util.List;
import java.util.Optional;

/**
 * Used to stack context stores when one ContextStore may not work for all cases
 * One example of this is in a micronaut code base that has both GRPC and HTTP entry points
 * Where a ContextStore backed by ServerRequestContext works for HTTP but for GRPC, ServerRequestContext is not available
 * Picks the first context store that returns true
 * NOOPs quietly if there are no valid stores
 */
public class CompositeContextStore implements ContextStore {

  private final List contextStores;

  public CompositeContextStore(ContextStore... contextStores) {
    this.contextStores = Arrays.asList(contextStores);
  }

  @Override
  public void addContext(PrefabContext prefabContext) {
    getValidStore().ifPresent(cs -> cs.addContext(prefabContext));
  }

  @Override
  public Optional setContext(
    PrefabContextSetReadable prefabContextSetReadable
  ) {
    return getValidStore().flatMap(cs -> cs.setContext(prefabContextSetReadable));
  }

  @Override
  public Optional clearContext() {
    return getValidStore().flatMap(ContextStore::clearContext);
  }

  @Override
  public Optional getContext() {
    return getValidStore().flatMap(ContextStore::getContext);
  }

  Optional getValidStore() {
    return contextStores.stream().filter(ContextStore::isAvailable).findFirst();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy