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