Coverage Report - org.webslinger.lang.AtomicCollection
 
Classes in this File Line Coverage Branch Coverage Complexity
AtomicCollection
100%
14/14
100%
2/2
0
 
 1  
 package org.webslinger.lang;
 2  
 
 3  
 import java.util.Collection;
 4  
 import java.util.Map;
 5  
 import java.util.concurrent.atomic.AtomicReference;
 6  
 
 7  
 public abstract class AtomicCollection<T> {
 8  
     private final AtomicReference<Collection<T>> ref;
 9  
 
 10  54024
     protected AtomicCollection(Collection<T> initial) {
 11  54024
         ref = new AtomicReference<Collection<T>>(initial);
 12  54024
     }
 13  
 
 14  
     protected abstract Collection<T> copyCollection(Collection<T> old);
 15  
 
 16  
     public Collection<T> collection() {
 17  5986
         return ref.get();
 18  
     }
 19  
 
 20  
     public boolean add(T value) {
 21  
         Collection<T> oldCollection, newCollection;
 22  
         boolean wasAdded;
 23  
         do {
 24  7122
             oldCollection = ref.get();
 25  7122
             newCollection = copyCollection(oldCollection);
 26  7122
             wasAdded = newCollection.add(value);
 27  7122
         } while (!ref.compareAndSet(oldCollection, newCollection));
 28  7122
         return wasAdded;
 29  
     }
 30  
 
 31  
     public boolean remove(T value) {
 32  
         Collection<T> oldCollection, newCollection;
 33  
         boolean wasRemoved;
 34  
         do {
 35  7122
             oldCollection = ref.get();
 36  7122
             newCollection = copyCollection(oldCollection);
 37  7122
             wasRemoved = newCollection.remove(value);
 38  7122
         } while (!ref.compareAndSet(oldCollection, newCollection));
 39  7122
         return wasRemoved;
 40  
     }
 41  
 }
 42