KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > collection > PersistentSortedMap


1 //$Id: PersistentSortedMap.java,v 1.5 2005/06/28 08:59:06 maxcsaucdk Exp $
2
package org.hibernate.collection;
3
4
5 import java.io.Serializable JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.TreeMap JavaDoc;
8 import java.util.Comparator JavaDoc;
9 import java.util.Collection JavaDoc;
10
11 import org.hibernate.EntityMode;
12 import org.hibernate.HibernateException;
13 import org.hibernate.engine.SessionImplementor;
14 import org.hibernate.persister.collection.BasicCollectionPersister;
15 import org.hibernate.persister.collection.CollectionPersister;
16
17
18 /**
19  * A persistent wrapper for a <tt>java.util.SortedMap</tt>. Underlying
20  * collection is a <tt>TreeMap</tt>.
21  *
22  * @see java.util.TreeMap
23  * @author <a HREF="mailto:doug.currie@alum.mit.edu">e</a>
24  */

25 public class PersistentSortedMap extends PersistentMap implements java.util.SortedMap JavaDoc {
26
27     protected Comparator JavaDoc comparator;
28
29     protected Serializable JavaDoc snapshot(BasicCollectionPersister persister, EntityMode entityMode) throws HibernateException {
30         TreeMap JavaDoc clonedMap = new TreeMap JavaDoc(comparator);
31         Iterator JavaDoc iter = map.entrySet().iterator();
32         while ( iter.hasNext() ) {
33             java.util.Map.Entry e = (java.util.Map.Entry) iter.next();
34             clonedMap.put( e.getKey(), persister.getElementType().deepCopy( e.getValue(), entityMode, persister.getFactory() ) );
35         }
36         return clonedMap;
37     }
38
39     public PersistentSortedMap(SessionImplementor session) {
40         super(session);
41     }
42
43     public PersistentSortedMap(SessionImplementor session, CollectionPersister persister, Comparator JavaDoc comparator, Serializable JavaDoc disassembled, Object JavaDoc owner)
44     throws HibernateException {
45         super(session);
46         this.comparator=comparator;
47         beforeInitialize(persister);
48         Serializable JavaDoc[] array = (Serializable JavaDoc[]) disassembled;
49         for (int i=0; i<array.length; i+=2 ) map.put(
50             persister.getIndexType().assemble( array[i], session, owner ),
51             persister.getElementType().assemble( array[i+1], session, owner )
52         );
53         setInitialized();
54     }
55
56     public void beforeInitialize(CollectionPersister persister) {
57         this.map = new TreeMap JavaDoc(comparator);
58     }
59
60     public void setComparator(Comparator JavaDoc comparator) {
61         this.comparator = comparator;
62     }
63
64     //need to distinguish between the different 2-argument constructors
65
/*public SortedMap(SessionImplementor session, Comparator comp) {
66         super(session);
67         this.map = new TreeMap(comp);
68     }*/

69
70     public PersistentSortedMap(SessionImplementor session, java.util.SortedMap JavaDoc map) {
71         super(session, map);
72         comparator = map.comparator();
73     }
74
75     public PersistentSortedMap() {} //needed for SOAP libraries, etc
76

77     /**
78      * @see PersistentSortedMap#comparator()
79      */

80     public Comparator JavaDoc comparator() {
81         return comparator;
82     }
83
84     /**
85      * @see PersistentSortedMap#subMap(Object, Object)
86      */

87     public java.util.SortedMap JavaDoc subMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
88         read();
89         java.util.SortedMap JavaDoc m = ( (java.util.SortedMap JavaDoc) map ).subMap(fromKey, toKey);
90         return new SortedSubMap(m);
91     }
92
93     /**
94      * @see PersistentSortedMap#headMap(Object)
95      */

96     public java.util.SortedMap JavaDoc headMap(Object JavaDoc toKey) {
97         read();
98         java.util.SortedMap JavaDoc m;
99         m = ( (java.util.SortedMap JavaDoc) map ).headMap(toKey);
100         return new SortedSubMap(m);
101     }
102
103     /**
104      * @see PersistentSortedMap#tailMap(Object)
105      */

106     public java.util.SortedMap JavaDoc tailMap(Object JavaDoc fromKey) {
107         read();
108         java.util.SortedMap JavaDoc m;
109         m = ( (java.util.SortedMap JavaDoc) map ).tailMap(fromKey);
110         return new SortedSubMap(m);
111     }
112
113     /**
114      * @see PersistentSortedMap#firstKey()
115      */

116     public Object JavaDoc firstKey() {
117         read();
118         return ( (java.util.SortedMap JavaDoc) map ).firstKey();
119     }
120
121     /**
122      * @see PersistentSortedMap#lastKey()
123      */

124     public Object JavaDoc lastKey() {
125         read();
126         return ( (java.util.SortedMap JavaDoc) map ).lastKey();
127     }
128
129     class SortedSubMap implements java.util.SortedMap JavaDoc {
130
131         java.util.SortedMap JavaDoc submap;
132
133         SortedSubMap(java.util.SortedMap JavaDoc m) {
134             this.submap = m;
135         }
136         // from Map
137
public int size() {
138             return submap.size();
139         }
140         public boolean isEmpty() {
141             return submap.isEmpty();
142         }
143         public boolean containsKey(Object JavaDoc key) {
144             return submap.containsKey(key);
145         }
146         public boolean containsValue(Object JavaDoc key) {
147             return submap.containsValue(key) ;
148         }
149         public Object JavaDoc get(Object JavaDoc key) {
150             return submap.get(key);
151         }
152         public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value) {
153             write();
154             return submap.put(key, value);
155         }
156         public Object JavaDoc remove(Object JavaDoc key) {
157             write();
158             return submap.remove(key);
159         }
160         public void putAll(java.util.Map JavaDoc other) {
161             write();
162             submap.putAll(other);
163         }
164         public void clear() {
165             write();
166             submap.clear();
167         }
168         public java.util.Set JavaDoc keySet() {
169             return new SetProxy( submap.keySet() );
170         }
171         public Collection JavaDoc values() {
172             return new SetProxy( submap.values() );
173         }
174         public java.util.Set JavaDoc entrySet() {
175             return new EntrySetProxy( submap.entrySet() );
176         }
177         // from SortedMap
178
public Comparator JavaDoc comparator() {
179             return submap.comparator();
180         }
181         public java.util.SortedMap JavaDoc subMap(Object JavaDoc fromKey, Object JavaDoc toKey) {
182             java.util.SortedMap JavaDoc m;
183             m = submap.subMap(fromKey, toKey);
184             return new SortedSubMap( m );
185         }
186         public java.util.SortedMap JavaDoc headMap(Object JavaDoc toKey) {
187             java.util.SortedMap JavaDoc m;
188             m = submap.headMap(toKey);
189             return new SortedSubMap(m);
190         }
191         public java.util.SortedMap JavaDoc tailMap(Object JavaDoc fromKey) {
192             java.util.SortedMap JavaDoc m;
193             m = submap.tailMap(fromKey);
194             return new SortedSubMap(m);
195         }
196         public Object JavaDoc firstKey() {
197             return submap.firstKey();
198         }
199         public Object JavaDoc lastKey() {
200             return submap.lastKey();
201         }
202
203     }
204
205 }
206
207
208
209
210
211
212
213
Popular Tags