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.io.ObjectInput;
023    import java.util.ArrayList;
024    import java.util.Collection;
025    import java.util.List;
026    import java.util.ListIterator;
027    
028    import org.granite.messaging.persistence.PersistentCollectionSnapshot;
029    
030    /**
031     * @author Franck WOLFF
032     */
033    public class PersistentList<E> extends AbstractPersistentSimpleCollection<E, List<E>> implements List<E> {
034    
035            public PersistentList() {
036            }
037    
038            public PersistentList(boolean initialized) {
039                    this(initialized ? new ArrayList<E>() : null, false);
040            }
041    
042            public PersistentList(List<E> collection) {
043                    this(collection, true);
044            }
045    
046            public PersistentList(List<E> collection, boolean clone) {
047                    if (collection != null)
048                            init((clone ? new ArrayList<E>(collection) : collection), null, false);
049            }
050            
051            @Override
052            public void doInitialize() {
053                    init(new ArrayList<E>(), null, false);
054            }
055            
056            public boolean addAll(int index, Collection<? extends E> c) {
057                    checkInitializedWrite();
058                    if (getCollection().addAll(index, c)) {
059                            dirty();
060                            return true;
061                    }
062                    return false;
063            }
064    
065            public E get(int index) {
066                    if (!checkInitializedRead())
067                            return null;
068                    return getCollection().get(index);
069            }
070    
071            public E set(int index, E element) {
072                    checkInitializedWrite();
073                    E previousElement = getCollection().set(index, element);
074                    if (previousElement == null ? element != null : !previousElement.equals(element))
075                            dirty();
076                    return previousElement;
077            }
078    
079            public void add(int index, E element) {
080                    checkInitializedWrite();
081                    getCollection().add(index, element);
082                    dirty();
083            }
084    
085            public E remove(int index) {
086                    checkInitializedWrite();
087                    E previousElement = getCollection().remove(index);
088                    dirty();
089                    return previousElement;
090            }
091    
092            public int indexOf(Object o) {
093                    if (!checkInitializedRead())
094                            return -1;
095                    return getCollection().indexOf(o);
096            }
097    
098            public int lastIndexOf(Object o) {
099                    if (!checkInitializedRead())
100                            return -1;
101                    return getCollection().lastIndexOf(o);
102            }
103    
104            public ListIterator<E> listIterator() {
105                    return listIterator(0);
106            }
107    
108            public ListIterator<E> listIterator(int index) {
109                    return new ListIteratorProxy<E>(getCollection().listIterator(index));
110            }
111    
112            public List<E> subList(int fromIndex, int toIndex) {
113                    if (!checkInitializedRead())
114                            return null;
115                    return new ListProxy<E>(getCollection().subList(fromIndex, toIndex));
116            }
117    
118            @SuppressWarnings("unchecked")
119            @Override
120            protected void updateFromSnapshot(ObjectInput in, PersistentCollectionSnapshot snapshot) {
121                    if (snapshot.isInitialized())
122                            init(new ArrayList<E>((Collection<? extends E>)snapshot.getElementsAsCollection()), snapshot.getDetachedState(), snapshot.isDirty());
123                    else
124                            init(null, snapshot.getDetachedState(), false);
125            }
126            
127        public PersistentList<E> clone(boolean uninitialize) {
128            PersistentList<E> list = new PersistentList<E>();
129            if (wasInitialized() && !uninitialize)
130                    list.init(getCollection(), null, isDirty());
131            return list; 
132        }
133    }