private final Object internalReplace(Object k,Object v,Object cv){
  int h=spread(k.hashCode());
  Object oldVal=null;
  for (Node[] tab=table; ; ) {
    Node f;
    int i, fh;
    Object fk;
    if (tab == null || (f=tabAt(tab,i=(tab.length - 1) & h)) == null)     break;
 else     if ((fh=f.hash) == MOVED) {
      if ((fk=f.key) instanceof TreeBin) {
        TreeBin t=(TreeBin)fk;
        boolean validated=false;
        boolean deleted=false;
        t.acquire(0);
        try {
          if (tabAt(tab,i) == f) {
            validated=true;
            TreeNode p=t.getTreeNode(h,k,t.root);
            if (p != null) {
              Object pv=p.val;
              if (cv == null || cv == pv || cv.equals(pv)) {
                oldVal=pv;
                if ((p.val=v) == null) {
                  deleted=true;
                  t.deleteTreeNode(p);
                }
              }
            }
          }
        }
  finally {
          t.release(0);
        }
        if (validated) {
          if (deleted)           counter.add(-1L);
          break;
        }
      }
 else       tab=(Node[])fk;
    }
 else     if ((fh & HASH_BITS) != h && f.next == null)     break;
 else     if ((fh & LOCKED) != 0) {
      checkForResize();
      f.tryAwaitLock(tab,i);
    }
 else     if (f.casHash(fh,fh | LOCKED)) {
      boolean validated=false;
      boolean deleted=false;
      try {
        if (tabAt(tab,i) == f) {
          validated=true;
          for (Node e=f, pred=null; ; ) {
            Object ek, ev;
            if ((e.hash & HASH_BITS) == h && ((ev=e.val) != null) && ((ek=e.key) == k || k.equals(ek))) {
              if (cv == null || cv == ev || cv.equals(ev)) {
                oldVal=ev;
                if ((e.val=v) == null) {
                  deleted=true;
                  Node en=e.next;
                  if (pred != null)                   pred.next=en;
 else                   setTabAt(tab,i,en);
                }
              }
              break;
            }
            pred=e;
            if ((e=e.next) == null)             break;
          }
        }
      }
  finally {
        if (!f.casHash(fh | LOCKED,fh)) {
          f.hash=fh;
synchronized (f) {
            f.notifyAll();
          }
          ;
        }
      }
      if (validated) {
        if (deleted)         counter.add(-1L);
        break;
      }
    }
  }
  return oldVal;
}

private final Object internalReplace ( Object k , Object v , Object cv ) { int h = spread ( k . hashCode ( ) ) ; Object oldVal = null ; for ( Node [ ] tab = table ; ; ) { Node f ; int i , fh ; Object fk ; if ( tab == null || ( f = tabAt ( tab , i = ( tab . length - 1 ) & h ) ) == null ) break ; else if ( ( fh = f . hash ) == MOVED ) { if ( ( fk = f . key ) instanceof TreeBin ) { TreeBin t = ( TreeBin ) fk ; boolean validated = false ; boolean deleted = false ; t . acquire ( 0 ) ; try { if ( tabAt ( tab , i ) == f ) { validated = true ; TreeNode p = t . getTreeNode ( h , k , t . root ) ; if ( p != null ) { Object pv = p . val ; if ( cv == null || cv == pv || cv . equals ( pv ) ) { oldVal = pv ; if ( ( p . val = v ) == null ) { deleted = true ; t . deleteTreeNode ( p ) ; } } } } } finally { t . release ( 0 ) ; } if ( validated ) { if ( deleted ) counter . add ( - 1L ) ; break ; } } else tab = ( Node [ ] ) fk ; } else if ( ( fh & HASH_BITS ) != h && f . next == null ) break ; else if ( ( fh & LOCKED ) != 0 ) { checkForResize ( ) ; f . tryAwaitLock ( tab , i ) ; } else if ( f . casHash ( fh , fh | LOCKED ) ) { boolean validated = false ; boolean deleted = false ; try { if ( tabAt ( tab , i ) == f ) { validated = true ; for ( Node e = f , pred = null ; ; ) { Object ek , ev ; if ( ( e . hash & HASH_BITS ) == h && ( ( ev = e . val ) != null ) && ( ( ek = e . key ) == k || k . equals ( ek ) ) ) { if ( cv == null || cv == ev || cv . equals ( ev ) ) { oldVal = ev ; if ( ( e . val = v ) == null ) { deleted = true ; Node en = e . next ; if ( pred != null ) pred . next = en ; else setTabAt ( tab , i , en ) ; } } break ; } pred = e ; if ( ( e = e . next ) == null ) break ; } } } finally { if ( ! f . casHash ( fh | LOCKED , fh ) ) { f . hash = fh ; synchronized ( f ) { f . notifyAll ( ) ; } ; } } if ( validated ) { if ( deleted ) counter . add ( - 1L ) ; break ; } } } return oldVal ; }
implementation for the four public remove / replace methods : replaces node value with v , conditional upon match of cv if non - null . if resulting value is null , delete .
