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

cloud.prefab.context.PrefabContextSet 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 cloud.prefab.domain.Prefab;
import com.google.common.collect.ImmutableList;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentSkipListMap;

public class PrefabContextSet implements PrefabContextSetReadable {

  private final ConcurrentSkipListMap contextByNameMap = new ConcurrentSkipListMap<>();

  public PrefabContextSet addContext(PrefabContext prefabContext) {
    if (prefabContext != null) {
      contextByNameMap.put(prefabContext.getName().toLowerCase(), prefabContext);
    }
    return this;
  }

  public boolean isEmpty() {
    return contextByNameMap.isEmpty();
  }

  @Override
  public Optional getByName(String contextType) {
    return Optional.ofNullable(contextByNameMap.get(contextType.toLowerCase()));
  }

  @Override
  public Iterable getContexts() {
    return ImmutableList.copyOf(contextByNameMap.values());
  }

  public static PrefabContextSet from(PrefabContext... contexts) {
    PrefabContextSet set = new PrefabContextSet();
    for (PrefabContext context : contexts) {
      set.addContext(context);
    }
    return set;
  }

  /**
   * Converts the given `PrefabContextSetReadable` instance into a PrefabContextSet
   * If the argument is already a PrefabContextSet return it, othewise create a new PrefabContextSet and add the contents
   * of the PrefabContextSetReadable to it, then return the new set
   * @param prefabContextSetReadable instance to convert
   * @return a PrefabContextSet built as discussed above
   */
  public static PrefabContextSet convert(
    PrefabContextSetReadable prefabContextSetReadable
  ) {
    if (prefabContextSetReadable instanceof PrefabContextSet) {
      return (PrefabContextSet) prefabContextSetReadable;
    }
    PrefabContextSet prefabContextSet = new PrefabContextSet();
    for (PrefabContext context : prefabContextSetReadable.getContexts()) {
      prefabContextSet.addContext(context);
    }
    return prefabContextSet;
  }

  public Prefab.ContextSet toProto() {
    Prefab.ContextSet.Builder bldr = Prefab.ContextSet.newBuilder();
    getContexts()
      .forEach(prefabContext -> bldr.addContexts(prefabContext.toProtoContext()));
    return bldr.build();
  }

  public static PrefabContextSetReadable from(Prefab.ContextSet protoContextSet) {
    if (protoContextSet.getContextsList().isEmpty()) {
      return PrefabContextSet.EMPTY;
    }
    PrefabContextSet prefabContextSet = new PrefabContextSet();
    for (Prefab.Context contextProto : protoContextSet.getContextsList()) {
      prefabContextSet.addContext(PrefabContext.fromProto(contextProto));
    }
    return prefabContextSet;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) {
      return true;
    }
    if (o == null || getClass() != o.getClass()) {
      return false;
    }
    PrefabContextSet that = (PrefabContextSet) o;
    return Objects.equals(contextByNameMap, that.contextByNameMap);
  }

  @Override
  public int hashCode() {
    return Objects.hash(contextByNameMap);
  }

  @Override
  public String toString() {
    return com.google.common.base.MoreObjects
      .toStringHelper(this)
      .add("contextByNameMap", contextByNameMap)
      .toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy