001/**
002 *
003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved.
004 *
005 * This library is free software; you can redistribute it and/or
006 * modify it under the terms of the GNU Lesser General Public
007 * License as published by the Free Software Foundation; either 
008 * version 2.1 of the License, or (at your option) any later version.
009 * 
010 * This library is distributed in the hope that it will be useful,
011 * but WITHOUT ANY WARRANTY; without even the implied warranty of
012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013 * Lesser General Public License for more details.
014 * 
015 * You should have received a copy of the GNU Lesser General Public 
016 * License along with this library.  If not, see <http://www.gnu.org/licenses/>.
017 * 
018 **/
019package lucee.commons.collection;
020
021import java.io.IOException;
022import java.io.ObjectOutputStream;
023import java.io.Serializable;
024import java.util.Collection;
025import java.util.Iterator;
026
027public class SyncCollection<E> implements Collection<E>, Serializable {
028        private static final long serialVersionUID = 3053995032091335093L;
029
030        final Collection<E> c;  // Backing Collection
031        final Object mutex;     // Object on which to synchronize
032
033        SyncCollection(Collection<E> c) {
034            if (c==null)
035                throw new NullPointerException();
036            this.c = c;
037            mutex = this;
038        }
039        SyncCollection(Collection<E> c, Object mutex) {
040            this.c = c;
041            this.mutex = mutex;
042        }
043
044        public int size() {
045            synchronized (mutex) {return c.size();}
046        }
047        public boolean isEmpty() {
048            synchronized (mutex) {return c.isEmpty();}
049        }
050        public boolean contains(Object o) {
051            synchronized (mutex) {return c.contains(o);}
052        }
053        public Object[] toArray() {
054            synchronized (mutex) {return c.toArray();}
055        }
056        public <T> T[] toArray(T[] a) {
057            synchronized (mutex) {return c.toArray(a);}
058        }
059
060        public Iterator<E> iterator() {
061            return c.iterator(); // Must be manually synched by user!
062        }
063
064        public boolean add(E e) {
065            synchronized (mutex) {return c.add(e);}
066        }
067        public boolean remove(Object o) {
068            synchronized (mutex) {return c.remove(o);}
069        }
070
071        public boolean containsAll(Collection<?> coll) {
072            synchronized (mutex) {return c.containsAll(coll);}
073        }
074        public boolean addAll(Collection<? extends E> coll) {
075            synchronized (mutex) {return c.addAll(coll);}
076        }
077        public boolean removeAll(Collection<?> coll) {
078            synchronized (mutex) {return c.removeAll(coll);}
079        }
080        public boolean retainAll(Collection<?> coll) {
081            synchronized (mutex) {return c.retainAll(coll);}
082        }
083        public void clear() {
084            synchronized (mutex) {c.clear();}
085        }
086        public String toString() {
087            synchronized (mutex) {return c.toString();}
088        }
089        private void writeObject(ObjectOutputStream s) throws IOException {
090            synchronized (mutex) {s.defaultWriteObject();}
091        }
092    }