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.Collection;
025 import java.util.Comparator;
026 import java.util.SortedSet;
027 import java.util.TreeSet;
028
029 import org.granite.messaging.persistence.PersistentCollectionSnapshot;
030 import org.granite.messaging.persistence.PersistentCollectionSnapshotFactory;
031
032 /**
033 * @author Franck WOLFF
034 */
035 public class PersistentSortedSet<E> extends AbstractPersistentSimpleCollection<E, SortedSet<E>> implements SortedSet<E>, PersistentSortedCollection<E> {
036
037 public PersistentSortedSet() {
038 }
039
040 public PersistentSortedSet(SortedSet<E> collection) {
041 this(collection, true);
042 }
043
044 public PersistentSortedSet(SortedSet<E> collection, boolean clone) {
045 if (collection != null)
046 init(clone ? new TreeSet<E>(collection) : collection, false);
047 }
048
049 public Comparator<? super E> comparator() {
050 checkInitialized();
051 return getCollection().comparator();
052 }
053
054 public SortedSet<E> subSet(E fromElement, E toElement) {
055 checkInitialized();
056 return new SortedSetProxy<E>(this, getCollection().subSet(fromElement, toElement));
057 }
058
059 public SortedSet<E> headSet(E toElement) {
060 checkInitialized();
061 return new SortedSetProxy<E>(this, getCollection().headSet(toElement));
062 }
063
064 public SortedSet<E> tailSet(E fromElement) {
065 checkInitialized();
066 return new SortedSetProxy<E>(this, getCollection().tailSet(fromElement));
067 }
068
069 public E first() {
070 checkInitialized();
071 return getCollection().first();
072 }
073
074 public E last() {
075 checkInitialized();
076 return getCollection().last();
077 }
078
079 @Override
080 protected PersistentCollectionSnapshot createSnapshot(boolean forReading) {
081 PersistentCollectionSnapshotFactory factory = PersistentCollectionSnapshotFactory.newInstance();
082 if (forReading || !wasInitialized())
083 return factory.newPersistentCollectionSnapshot(true);
084 return factory.newPersistentCollectionSnapshot(true, isDirty(), getCollection());
085 }
086
087 @SuppressWarnings("unchecked")
088 @Override
089 protected void updateFromSnapshot(ObjectInput in, PersistentCollectionSnapshot snapshot) {
090 if (snapshot.isInitialized()) {
091 Comparator<? super E> comparator = null;
092 try {
093 comparator = snapshot.newComparator(in);
094 }
095 catch (Exception e) {
096 throw new RuntimeException("Could not create instance of comparator", e);
097 }
098 SortedSet<E> set = new TreeSet<E>(comparator);
099 set.addAll((Collection<? extends E>)snapshot.getElementsAsCollection());
100 init(set, snapshot.isDirty());
101 }
102 else
103 init(null, false);
104 }
105 }