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

com.github.grzesiek_galezowski.test_environment.buffer.implementation.SearchCommand Maven / Gradle / Ivy

package com.github.grzesiek_galezowski.test_environment.buffer.implementation;

import com.github.grzesiek_galezowski.test_environment.buffer.interfaces.BufferObserver;
import com.github.grzesiek_galezowski.test_environment.buffer.exceptions.ExceptionRaisedByConditionException;
import lombok.val;
import org.assertj.core.api.Condition;

import java.util.List;

public class SearchCommand {
  private final SearchResult searchResult;
  private BufferObserver observer;
  private List receivedObjects;
  private Condition condition;

  public SearchCommand(
      final BufferObserver observer,
      final List receivedObjects,
      final Condition expected,
      final SearchResult searchResult) {
    this.observer = observer;
    this.receivedObjects = receivedObjects;
    this.condition = expected;
    this.searchResult = searchResult;
  }

  public void performSearch() {
    observer.searchingStartedWithin(receivedObjects, condition);

    for (T receivedObject : this.receivedObjects) {
      determineMatchOf(receivedObject);
    }

    searchResult.conclude();
  }

  private void determineMatchOf(final T receivedObject) {
    String previousDescription = condition.description().toString();
    try {
      observer.tryingToMatch(receivedObject, condition);

      val isMatch = this.condition.matches(receivedObject);
      val matchDescription = this.condition.description().toString();

      searchResult.add(receivedObject, isMatch, matchDescription);
    } catch (Throwable t) {
      val exception
          = new ExceptionRaisedByConditionException(condition, t);
      observer.searchingFinishedWith(exception, receivedObject);
      throw exception;
    } finally {
      condition.describedAs(previousDescription);
    }
  }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy