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

ai.stapi.graphsystem.genericGraphEventFactory.GenericGraphEventFactory Maven / Gradle / Ivy

package ai.stapi.graphsystem.genericGraphEventFactory;

import ai.stapi.graphsystem.messaging.event.AggregateGraphUpdatedEvent;
import ai.stapi.graphsystem.genericGraphEventFactory.exception.GenericGraphEventFactoryException;
import ai.stapi.graphsystem.genericGraphEventFactory.specific.SpecificGraphEventFactory;
import ai.stapi.graph.graphElementForRemoval.GraphElementForRemoval;
import ai.stapi.graph.Graph;
import ai.stapi.graphsystem.messaging.event.DynamicGraphUpdatedEvent;
import ai.stapi.identity.UniqueIdentifier;
import java.util.List;
import org.jetbrains.annotations.NotNull;

public class GenericGraphEventFactory {

  private final List eventFactories;

  public GenericGraphEventFactory(
      List eventFactories
  ) {
    this.eventFactories = eventFactories;
  }

  public AggregateGraphUpdatedEvent createEvent(
      Class> eventType,
      String eventName,
      UniqueIdentifier identity,
      Graph graph,
      List elementsForRemoval
  ) {
    if (DynamicGraphUpdatedEvent.class.equals(eventType)) {
      return new DynamicGraphUpdatedEvent(
          eventName,
          identity,
          graph,
          elementsForRemoval
      );
    }
    var supportingFactory = this.getSupportingSpecificFactory(eventType);
    return supportingFactory.createEvent(identity, graph, elementsForRemoval);
  }

  public AggregateGraphUpdatedEvent createEvent(
      Class> eventType,
      UniqueIdentifier identity,
      Graph graph,
      List elementsForRemoval
  ) {
    return this.createEvent(
        eventType,
        eventType.getSimpleName(),
        identity,
        graph,
        elementsForRemoval
    );
  }

  @NotNull
  private SpecificGraphEventFactory getSupportingSpecificFactory(
      Class> event
  ) {
    var supportingFactories = this.eventFactories.stream()
        .filter(specificObjectGraphMapper -> specificObjectGraphMapper.supports(event))
        .toList();
    if (supportingFactories.isEmpty()) {
      throw GenericGraphEventFactoryException.becauseNoSupportingSpecificFactoriesForGivenEvent(event);
    }
    if (supportingFactories.size() > 1) {
      throw GenericGraphEventFactoryException.becauseMoreThanOneSpecificFactoriesForGivenCommand(event);
    }
    return supportingFactories.get(0);
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy