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