001package arez.spy;
002
003import java.util.Map;
004import java.util.Objects;
005import javax.annotation.Nonnull;
006import javax.annotation.Nullable;
007
008/**
009 * Notification when Action completes.
010 */
011public final class ActionCompleteEvent
012  implements SerializableEvent
013{
014  @Nonnull
015  private final String _name;
016  /**
017   * Is the action a "tracking" action.
018   */
019  private final boolean _tracked;
020  @Nonnull
021  private final Object[] _parameters;
022  /**
023   * True if the action returns a result during normal completion.
024   */
025  private final boolean _returnsResult;
026  @Nullable
027  private final Object _result;
028  @Nullable
029  private final Throwable _throwable;
030  private final int _duration;
031
032  public ActionCompleteEvent( @Nonnull final String name,
033                              final boolean tracked,
034                              @Nonnull final Object[] parameters,
035                              final boolean returnsResult,
036                              @Nullable final Object result,
037                              @Nullable final Throwable throwable,
038                              final int duration )
039  {
040    assert duration >= 0;
041    assert null == throwable || null == result;
042    _name = Objects.requireNonNull( name );
043    _tracked = tracked;
044    _parameters = Objects.requireNonNull( parameters );
045    _returnsResult = returnsResult;
046    _result = result;
047    _throwable = throwable;
048    _duration = duration;
049  }
050
051  @Nonnull
052  public String getName()
053  {
054    return _name;
055  }
056
057  public boolean isTracked()
058  {
059    return _tracked;
060  }
061
062  @Nonnull
063  public Object[] getParameters()
064  {
065    return _parameters;
066  }
067
068  public boolean returnsResult()
069  {
070    return _returnsResult;
071  }
072
073  @Nullable
074  public Object getResult()
075  {
076    return _result;
077  }
078
079  @Nullable
080  public Throwable getThrowable()
081  {
082    return _throwable;
083  }
084
085  public int getDuration()
086  {
087    return _duration;
088  }
089
090  @Override
091  public void toMap( @Nonnull final Map<String, Object> map )
092  {
093    map.put( "type", "ActionComplete" );
094    map.put( "name", getName() );
095    map.put( "tracked", isTracked() );
096    map.put( "duration", getDuration() );
097    final Throwable throwable = getThrowable();
098    final String message =
099      null == throwable ? null : null == throwable.getMessage() ? throwable.toString() : throwable.getMessage();
100    map.put( "errorMessage", message );
101    map.put( "parameters", getParameters() );
102    map.put( "returnsResult", returnsResult() );
103    map.put( "result", getResult() );
104  }
105}