private Operation optimizeGets() {
    // make sure there are at least two get operations in a row before
    // attempting to optimize them.
    Operation optimizedOp = writeQ.remove();
    if (writeQ.peek() instanceof GetOperation) {
      OptimizedGetImpl og = new OptimizedGetImpl((GetOperation) optimizedOp);
      optimizedOp = og;

      while (writeQ.peek() instanceof GetOperation) {
        GetOperation o = (GetOperation) writeQ.remove();
        if (!o.isCancelled()) {
          og.addOperation(o);
        }
      }

      // Initialize the new mega get
      optimizedOp.initialize();
      assert optimizedOp.getState() == OperationState.WRITING;
      ProxyCallback pcb = (ProxyCallback) og.getCallback();
      getLogger()
          .debug(
              "Set up %s with %s keys and %s callbacks", this, pcb.numKeys(), pcb.numCallbacks());
    }

    return optimizedOp;
  }
  public void testSingleGetOperationCloning() {
    GetOperation.Callback callback =
        (GetOperation.Callback) mock(GetOperation.Callback.class).proxy();
    GetOperation op = ofact.get(TEST_KEY, callback);

    GetOperation op2 = cloneOne(GetOperation.class, op);
    assertKey(op2);
    assertSame(callback, op.getCallback());
  }
  // These are harder cases as they fan out.
  public void testMultipleGetOperationCloning() {
    Collection<String> keys = Arrays.asList("k1", "k2", "k3");
    GetOperation.Callback callback =
        (GetOperation.Callback) mock(GetOperation.Callback.class).proxy();
    GetOperation op = ofact.get(keys, callback);

    Collection<Operation> ops = ofact.clone(op);
    assertEquals(3, ops.size());

    Collection<String> mutableKeys = new ArrayList<String>(keys);
    int i = 3;
    for (Operation o : ops) {
      assertEquals(i, mutableKeys.size()); // Starting size
      GetOperation go = (GetOperation) o;
      mutableKeys.removeAll(go.getKeys());
      // Verify we matched and removed 1
      assertEquals(--i, mutableKeys.size());
    }
  }