001package arez.testng;
002
003import arez.Observer;
004import arez.ObserverError;
005import java.util.ArrayList;
006import java.util.List;
007import javax.annotation.Nonnull;
008import javax.annotation.Nullable;
009
010public final class ObserverErrorCollector
011{
012  @Nonnull
013  private final List<String> _observerErrors = new ArrayList<>();
014  private final boolean _printExceptionStackTrace;
015
016  public ObserverErrorCollector()
017  {
018    this( false );
019  }
020
021  public ObserverErrorCollector( final boolean printExceptionStackTrace )
022  {
023    _printExceptionStackTrace = printExceptionStackTrace;
024  }
025
026  public void clear()
027  {
028    _observerErrors.clear();
029  }
030
031  @Nonnull
032  public List<String> getObserverErrors()
033  {
034    return _observerErrors;
035  }
036
037  void onObserverError( @Nonnull final Observer observer,
038                        @Nonnull final ObserverError error,
039                        @Nullable final Throwable throwable )
040  {
041    _observerErrors.add( "Observer: " + observer.getName() + " Error: " + error + " " + throwable );
042    if ( _printExceptionStackTrace && null != throwable )
043    {
044      throwable.printStackTrace();
045    }
046  }
047}