ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java
(Generate patch)

Comparing jsr166/src/main/java/util/concurrent/ConcurrentHashMap.java (file contents):
Revision 1.32 by dl, Wed Dec 3 21:07:44 2003 UTC vs.
Revision 1.33 by dl, Sat Dec 6 00:16:20 2003 UTC

# Line 305 | Line 305 | public class ConcurrentHashMap<K, V> ext
305                      e = e.next;
306                  }
307  
308 <                if (oldValue != null) {
309 <                    V v = e.value;
310 <                    if (v == null || !oldValue.equals(v))
311 <                        return false;
312 <                }
308 >                V v = e.value;
309 >                if (v == null || !oldValue.equals(v))
310 >                    return false;
311  
312                  e.value = newValue;
313                  count = c; // write-volatile
# Line 320 | Line 318 | public class ConcurrentHashMap<K, V> ext
318              }
319          }
320  
321 +        V replace(K key, int hash, V newValue) {
322 +            lock();
323 +            try {
324 +                int c = count;
325 +                HashEntry[] tab = table;
326 +                int index = hash & (tab.length - 1);
327 +                HashEntry<K,V> first = (HashEntry<K,V>) tab[index];
328 +                HashEntry<K,V> e = first;
329 +                for (;;) {
330 +                    if (e == null)
331 +                        return null;
332 +                    if (e.hash == hash && key.equals(e.key))
333 +                        break;
334 +                    e = e.next;
335 +                }
336 +
337 +                V v = e.value;
338 +                e.value = newValue;
339 +                count = c; // write-volatile
340 +                return v;
341 +                
342 +            } finally {
343 +                unlock();
344 +            }
345 +        }
346 +
347  
348          V put(K key, int hash, V value, boolean onlyIfAbsent) {
349              lock();
# Line 853 | Line 877 | public class ConcurrentHashMap<K, V> ext
877      }
878  
879      /**
880 <     * Replace entry for key only if key is currently mapped.
880 >     * Replace entry for key only if currently mapped to some value.
881       * Acts as
882       * <pre>
883       *  if ((map.containsKey(key)) {
884 <     *     map.put(key, value);
885 <     *     return true;
862 <     * } else return false;
884 >     *     return map.put(key, value);
885 >     * } else return null;
886       * </pre>
887       * except that the action is performed atomically.
888       * @param key key with which the specified value is associated.
889       * @param value value to be associated with the specified key.
890 <     * @return true if the value was replaced
890 >     * @return previous value associated with specified key, or <tt>null</tt>
891 >     *         if there was no mapping for key.  
892       * @throws NullPointerException if the specified key or value is
893       *            <tt>null</tt>.
894       */
895 <    public boolean replace(K key, V value) {
895 >    public V replace(K key, V value) {
896          if (value == null)
897              throw new NullPointerException();
898          int hash = hash(key);
899 <        return segmentFor(hash).replace(key, hash, null, value);
899 >        return segmentFor(hash).replace(key, hash, value);
900      }
901  
902  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines