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