V (long key, V value, boolean onlyIfAbsent) { int hash = spread(key); int binCount = 0; for (Node<V>[] tab = table;;) { Node<V> f; int n, i, fh; long fk; V fv; if (tab == null || (n = tab.length) == 0) { tab = initTable(); } else if ((f = tabAt(tab, i = (n - 1) & hash)) == null) { if (casTabAt(tab, i, new Node<V>(hash, key, value, null))) { break; // no lock when adding to empty bin } } else if ((fh = f.hash) == MOVED) { tab = helpTransfer(tab, f); } else if (onlyIfAbsent // check first node without acquiring lock && fh == hash && ((fk = f.key) == key) && (fv = f.val) != null) { return fv; } else { V oldVal = null; synchronized (f) { if (tabAt(tab, i) == f) { if (fh >= 0) { binCount = 1; for (Node<V> e = f; ; ++binCount) { long ek; if (e.hash == hash && ((ek = e.key) == key)) { oldVal = e.val; if (!onlyIfAbsent) { e.val = value; } break; } Node<V> pred = e; if ((e = e.next) == null) { pred.next = new Node<V>(hash, key, value, null); break; } } } else if (f instanceof TreeBin) { Node<V> p; binCount = 2; if ((p = ((TreeBin<V>)f).putTreeVal(hash, key, value)) != null) { oldVal = p.val; if (!onlyIfAbsent) { p.val = value; } } } else if (f instanceof ReservationNode) { throw new IllegalStateException("Recursive update"); } } } if (binCount != 0) { if (binCount >= TREEIFY_THRESHOLD) { treeifyBin(tab, i); } if (oldVal != null) { return oldVal; } break; } } } addCount(1L, binCount); return null; }