001package com.hammurapi.common;
002
003import java.util.Collection;
004import java.util.Iterator;
005import java.util.concurrent.atomic.AtomicBoolean;
006
007public class FreezeableCollection<E> implements Collection<E> {
008        
009        private Collection<E> master;
010        private AtomicBoolean freezed = new AtomicBoolean(false);
011
012        public FreezeableCollection(Collection<E> master) {
013                this.master = master;
014        }
015
016        public int size() {
017                return master.size();
018        }
019
020        public boolean isEmpty() {
021                return master.isEmpty();
022        }
023
024        public boolean contains(Object o) {
025                return master.contains(o);
026        }
027
028        public Iterator<E> iterator() {
029                return new Iterator<E>() {
030                        Iterator<E> masterIterator = master.iterator();
031
032                        @Override
033                        public boolean hasNext() {
034                                return masterIterator.hasNext();
035                        }
036
037                        @Override
038                        public E next() {
039                                return masterIterator.next();
040                        }
041
042                        @Override
043                        public void remove() {
044                                checkFrozen();
045                                masterIterator.remove();
046                        }
047                        
048                };
049                
050        }
051
052        public Object[] toArray() {
053                return master.toArray();
054        }
055
056        public <T> T[] toArray(T[] a) {
057                return master.toArray(a);
058        }
059
060        public boolean add(E e) {
061                checkFrozen();
062                return master.add(e);
063        }
064
065        public boolean remove(Object o) {
066                checkFrozen();
067                return master.remove(o);
068        }
069
070        public boolean containsAll(Collection<?> c) {
071                return master.containsAll(c);
072        }
073
074        public boolean addAll(Collection<? extends E> c) {
075                checkFrozen();
076                return master.addAll(c);
077        }
078
079        public boolean removeAll(Collection<?> c) {
080                checkFrozen();
081                return master.removeAll(c);
082        }
083
084        public boolean retainAll(Collection<?> c) {
085                checkFrozen();
086                return master.retainAll(c);
087        }
088
089        public void clear() {
090                checkFrozen();
091                master.clear();
092        }
093
094        public boolean equals(Object o) {
095                return master.equals(o);
096        }
097
098        public int hashCode() {
099                return master.hashCode();
100        }
101        
102        /**
103         * Freezes collection making it unmodifiable.
104         */
105        public void freeze() {
106                freezed.set(true);
107        }
108
109        private void checkFrozen() {
110                if (freezed.get()) {
111                        IllegalStateException th = new IllegalStateException("Collection is frozen, no modifications are allowed");
112                        th.printStackTrace();
113                        throw th;
114                }
115        }
116
117}