001 /*
002 GRANITE DATA SERVICES
003 Copyright (C) 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
021 package org.granite.client.persistence.collection;
022
023 import java.util.Collection;
024 import java.util.Iterator;
025
026 import org.granite.messaging.persistence.PersistentCollectionSnapshot;
027 import org.granite.messaging.persistence.PersistentCollectionSnapshotFactory;
028
029 /**
030 * @author Franck WOLFF
031 */
032 public abstract class AbstractPersistentSimpleCollection<E, C extends Collection<E>> extends AbstractPersistentCollection<C> implements Collection<E> {
033
034 public AbstractPersistentSimpleCollection() {
035 }
036
037 public int size() {
038 checkInitialized();
039 return getCollection().size();
040 }
041
042 public boolean isEmpty() {
043 checkInitialized();
044 return getCollection().isEmpty();
045 }
046
047 public boolean contains(Object o) {
048 checkInitialized();
049 return getCollection().contains(o);
050 }
051
052 public Iterator<E> iterator() {
053 checkInitialized();
054 return new IteratorProxy<E>(this, getCollection().iterator());
055 }
056
057 public Object[] toArray() {
058 checkInitialized();
059 return getCollection().toArray();
060 }
061
062 public <T> T[] toArray(T[] a) {
063 checkInitialized();
064 return getCollection().toArray(a);
065 }
066
067 public boolean add(E e) {
068 checkInitialized();
069 if (getCollection().add(e)) {
070 dirty();
071 return true;
072 }
073 return false;
074 }
075
076 public boolean remove(Object o) {
077 checkInitialized();
078 if (getCollection().remove(o)) {
079 dirty();
080 return true;
081 }
082 return false;
083 }
084
085 public boolean containsAll(Collection<?> c) {
086 checkInitialized();
087 return getCollection().containsAll(c);
088 }
089
090 public boolean addAll(Collection<? extends E> c) {
091 checkInitialized();
092 if (getCollection().addAll(c)) {
093 dirty();
094 return true;
095 }
096 return false;
097 }
098
099 public boolean removeAll(Collection<?> c) {
100 checkInitialized();
101 if (getCollection().removeAll(c)) {
102 dirty();
103 return true;
104 }
105 return false;
106 }
107
108 public boolean retainAll(Collection<?> c) {
109 checkInitialized();
110 if (getCollection().retainAll(c)) {
111 dirty();
112 return true;
113 }
114 return false;
115 }
116
117 public void clear() {
118 checkInitialized();
119 if (!getCollection().isEmpty()) {
120 getCollection().clear();
121 dirty();
122 }
123 }
124
125 @Override
126 protected PersistentCollectionSnapshot createSnapshot(boolean forReading) {
127 PersistentCollectionSnapshotFactory factory = PersistentCollectionSnapshotFactory.newInstance();
128 if (forReading || !wasInitialized())
129 return factory.newPersistentCollectionSnapshot();
130 return factory.newPersistentCollectionSnapshot(true, isDirty(), getCollection());
131 }
132 }