package org.realityforge.replicant.client.transport;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * A record of a request sent to the server that we need to keep track of.
 */
public class RequestEntry
{
  private final String _requestID;
  private final String _requestKey;
  @Nullable
  private final String _cacheKey;
  private final boolean _bulkLoad;
  private Boolean _normalCompletion;
  private boolean _expectingResults;
  private boolean _resultsArrived;
  private Runnable _runnable;

  public RequestEntry( @Nonnull final String requestID,
                       @Nonnull final String requestKey,
                       @Nullable final String cacheKey,
                       final boolean bulkLoad )
  {
    _requestID = requestID;
    _requestKey = requestKey;
    _cacheKey = cacheKey;
    _bulkLoad = bulkLoad;
  }

  @Nonnull
  public String getRequestID()
  {
    return _requestID;
  }

  @Nonnull
  public String getRequestKey()
  {
    return _requestKey;
  }

  @Nullable
  public String getCacheKey()
  {
    return _cacheKey;
  }

  public boolean isBulkLoad()
  {
    return _bulkLoad;
  }

  public boolean haveResultsArrived()
  {
    return _resultsArrived;
  }

  public void setNormalCompletionAction( @Nullable final Runnable runnable )
  {
    _normalCompletion = true;
    _runnable = runnable;
  }

  public void setNonNormalCompletionAction( @Nullable final Runnable runnable )
  {
    _normalCompletion = false;
    _runnable = runnable;
  }

  public boolean isCompletionDataPresent()
  {
    return null != _normalCompletion;
  }

  public boolean isNormalCompletion()
  {
    if ( !isCompletionDataPresent() )
    {
      throw new IllegalStateException( "Completion data not yet specified" );
    }
    return _normalCompletion;
  }

  public boolean isExpectingResults()
  {
    return _expectingResults;
  }

  @Nullable
  public Runnable getRunnable()
  {
    return _runnable;
  }

  public void setExpectingResults( final boolean expectingResults )
  {
    _expectingResults = expectingResults;
  }

  public void markResultsAsArrived()
  {
    _resultsArrived = true;
  }

  @Override
  public String toString()
  {
    return "Request(" + _requestKey + ")[ID=" + _requestID + ",Cache=" + _cacheKey + ",Bulk=" + _bulkLoad + "]";
  }
}

@@ -2244,16 +2244,51 @@
 uestID +
+%0A           ( ( null != _cacheKey ?
  %22,Cache
@@ -2306,20 +2306,18 @@
 Key 
-+ %22,Bulk=%22
+: %22%22 )
  +
+ (
  _bu
@@ -2322,16 +2322,40 @@
 bulkLoad
+ ? %22,BulkLoad!%22 : %22%22 ) )
  + %22%5D%22;%0A

Improve toString to aid debugging