001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004     *
005     *   This file is part of Granite Data Services.
006     *
007     *   Granite Data Services is free software; you can redistribute it and/or modify
008     *   it under the terms of the GNU Library General Public License as published by
009     *   the Free Software Foundation; either version 2 of the License, or (at your
010     *   option) any later version.
011     *
012     *   Granite Data Services is distributed in the hope that it will be useful, but
013     *   WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014     *   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015     *   for more details.
016     *
017     *   You should have received a copy of the GNU Library General Public License
018     *   along with this library; if not, see <http://www.gnu.org/licenses/>.
019     */
020    package org.granite.client.persistence.collection;
021    
022    import java.util.Collection;
023    import java.util.Iterator;
024    
025    import org.granite.messaging.persistence.PersistentCollectionSnapshot;
026    
027    /**
028     * @author Franck WOLFF
029     */
030    public abstract class AbstractPersistentSimpleCollection<E, C extends Collection<E>> extends AbstractPersistentCollection<C> implements Collection<E> {
031    
032            public AbstractPersistentSimpleCollection() {
033            }
034    
035            public int size() {
036                    if (!checkInitializedRead())
037                            return 0;
038                    return getCollection().size();
039            }
040    
041            public boolean isEmpty() {
042                    if (!checkInitializedRead())
043                            return true;
044                    return getCollection().isEmpty();
045            }
046    
047            public boolean contains(Object o) {
048                    if (!checkInitializedRead())
049                            return false;
050                    return getCollection().contains(o);
051            }
052    
053            public Iterator<E> iterator() {
054                    if (!checkInitializedRead())
055                            return null;
056                    return new IteratorProxy<E>(getCollection().iterator());
057            }
058    
059            public Object[] toArray() {
060                    if (!checkInitializedRead())
061                            return null;
062                    return getCollection().toArray();
063            }
064    
065            public <T> T[] toArray(T[] a) {
066                    if (!checkInitializedRead())
067                            return null;
068                    return getCollection().toArray(a);
069            }
070    
071            public boolean add(E e) {
072                    checkInitializedWrite();
073                    if (getCollection().add(e)) {
074                            dirty();
075                            return true;
076                    }
077                    return false;
078            }
079    
080            public boolean remove(Object o) {
081                    checkInitializedWrite();
082                    if (getCollection().remove(o)) {
083                            dirty();
084                            return true;
085                    }
086                    return false;
087            }
088    
089            public boolean containsAll(Collection<?> c) {
090                    if (!checkInitializedRead())
091                            return false;
092                    return getCollection().containsAll(c);
093            }
094    
095            public boolean addAll(Collection<? extends E> c) {
096                    checkInitializedWrite();
097                    if (getCollection().addAll(c)) {
098                            dirty();
099                            return true;
100                    }
101                    return false;
102            }
103    
104            public boolean removeAll(Collection<?> c) {
105                    checkInitializedWrite();
106                    if (getCollection().removeAll(c)) {
107                            dirty();
108                            return true;
109                    }
110                    return false;
111            }
112    
113            public boolean retainAll(Collection<?> c) {
114                    checkInitializedWrite();
115                    if (getCollection().retainAll(c)) {
116                            dirty();
117                            return true;
118                    }
119                    return false;
120            }
121    
122            public void clear() {
123                    checkInitializedWrite();
124                    if (!getCollection().isEmpty()) {
125                            getCollection().clear();
126                            dirty();
127                    }
128            }
129    
130            @Override
131            protected PersistentCollectionSnapshot createSnapshot(Object io, boolean forReading) {
132                    PersistentCollectionSnapshotFactory factory = PersistentCollectionSnapshotFactory.newInstance(io);
133                    if (forReading || !wasInitialized())
134                            return factory.newPersistentCollectionSnapshot(getDetachedState());
135                    return factory.newPersistentCollectionSnapshot(true, getDetachedState(), isDirty(), getCollection());
136            }
137    }