Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright SpringSource Inc 2009.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package com.springsource.util.common;
 
SynchronizedCollection wraps a given java.util.Collection and protects access to the collection and any iterators created from the collection by synchronizing on a given monitor.

Concurrent Semantics
This class is thread safe, but ConcurrentModificationException may still be thrown.

Parameters:
<T> type of element in collection
Author(s):
Glyn Normington
Since:
jersey
 
 public class SynchronizedCollection<T> extends SynchronizedObject implements Collection<T> {
 
     private final Collection<T> collection;
 
     private final Object monitor;
 
     private static final Object tieMonitor = new Object();

    
Creates a SynchronizedCollection wrapping the given collection and synchronizing on the given monitor, neither of which may be null.

Parameters:
collection the collection to be wrapped, which must not be null
monitor the monitor which will be used to synchronize access to the collection
 
     @SuppressWarnings("unchecked")
     public SynchronizedCollection(Collection<? extends T> collectionObject monitor) {
         super(collectionmonitor); // throws an exception if either argument is null
         this. = (Collection<T>) collection;
         this. = monitor;
     }

    
 
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
         synchronized (this.) {
             result = prime * result + .hashCode();
         }
         return result;
     }

    
 
     @SuppressWarnings("unchecked")
     @Override
     public boolean equals(Object obj) {
         if (this == obj) {
             return true;
         }
         if (obj == null) {
             return false;
         }
         if (getClass() != obj.getClass()) {
             return false;
         }
         SynchronizedCollection<T> other = (SynchronizedCollection<T>) obj;
         int thisHash = System.identityHashCode(this);
         int otherHash = System.identityHashCode(other);
         if (thisHash < otherHash) {
             synchronized (this.) {
                 synchronized (other.monitor) {
                     if (!.equals(other.collection)) {
                         return false;
                     }
                 }
             }
         } else if (thisHash > otherHash) {
             synchronized (other.monitor) {
                 synchronized (this.) {
                     if (!.equals(other.collection)) {
                        return false;
                    }
                }
            }
        } else {
            synchronized () {
                synchronized (this.) {
                    synchronized (other.monitor) {
                        if (!.equals(other.collection)) {
                            return false;
                        }
                    }
                }
            }
        }
        return true;
    }

    
    public boolean add(T e) {
        synchronized (this.) {
            return this..add(e);
        }
    }

    
    public boolean addAll(Collection<? extends T> c) {
        synchronized (this.) {
            return this..addAll(c);
        }
    }

    
    public void clear() {
        synchronized (this.) {
            this..clear();
        }
    }

    
    public boolean contains(Object o) {
        synchronized (this.) {
            return this..contains(o);
        }
    }

    
    public boolean containsAll(Collection<?> c) {
        synchronized (this.) {
            return this..containsAll(c);
        }
    }

    
    public boolean isEmpty() {
        synchronized (this.) {
            return this..isEmpty();
        }
    }

    
    public Iterator<T> iterator() {
        synchronized (this.) {
            return new SynchronizedIterator<T>(this..iterator(), this.);
        }
    }

    
    public boolean remove(Object o) {
        synchronized (this.) {
            return this..remove(o);
        }
    }

    
    public boolean removeAll(Collection<?> c) {
        synchronized (this.) {
            return this..removeAll(c);
        }
    }

    
    public boolean retainAll(Collection<?> c) {
        synchronized (this.) {
            return this..retainAll(c);
        }
    }

    
    public int size() {
        synchronized (this.) {
            return this..size();
        }
    }

    
    public Object[] toArray() {
        synchronized (this.) {
            return this..toArray();
        }
    }

    
    public <U> U[] toArray(U[] a) {
        synchronized (this.) {
            return this..toArray(a);
        }
    }
New to GrepCode? Check out our FAQ X