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 Computation completes.
010 */
011public final class ComputeCompleteEvent
012  implements SerializableEvent
013{
014  @Nonnull
015  private final ComputableValueInfo _computableValue;
016  @Nullable
017  private final Object _result;
018  @Nullable
019  private final Throwable _throwable;
020  private final int _duration;
021
022  public ComputeCompleteEvent( @Nonnull final ComputableValueInfo computableValue,
023                               @Nullable final Object result,
024                               @Nullable final Throwable throwable,
025                               final int duration )
026  {
027    assert duration >= 0;
028    assert null == throwable || null == result;
029    _computableValue = Objects.requireNonNull( computableValue );
030    _result = result;
031    _throwable = throwable;
032    _duration = duration;
033  }
034
035  @Nonnull
036  public ComputableValueInfo getComputableValue()
037  {
038    return _computableValue;
039  }
040
041  public int getDuration()
042  {
043    return _duration;
044  }
045
046  @Nullable
047  public Object getResult()
048  {
049    return _result;
050  }
051
052  @Nullable
053  public Throwable getThrowable()
054  {
055    return _throwable;
056  }
057
058  @Override
059  public void toMap( @Nonnull final Map<String, Object> map )
060  {
061    map.put( "type", "ComputeComplete" );
062    map.put( "name", getComputableValue().getName() );
063    map.put( "duration", getDuration() );
064    final Throwable throwable = getThrowable();
065    final String message =
066      null == throwable ? null : null == throwable.getMessage() ? throwable.toString() : throwable.getMessage();
067    map.put( "errorMessage", message );
068    map.put( "result", getResult() );
069  }
070}