KickJava   Java API By Example, From Geeks To Geeks.

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


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

24 public class PersistentSortedSet extends PersistentSet implements java.util.SortedSet JavaDoc {
25
26     protected Comparator JavaDoc comparator;
27
28     protected Serializable JavaDoc snapshot(BasicCollectionPersister persister, EntityMode entityMode) throws HibernateException {
29         //if (set==null) return new Set(session);
30
TreeMap JavaDoc clonedSet = new TreeMap JavaDoc(comparator);
31         Iterator JavaDoc iter = set.iterator();
32         while ( iter.hasNext() ) {
33             Object JavaDoc copy = persister.getElementType().deepCopy( iter.next(), entityMode, persister.getFactory() );
34             clonedSet.put(copy, copy);
35         }
36         return clonedSet;
37     }
38
39     public void setComparator(Comparator JavaDoc comparator) {
40         this.comparator = comparator;
41     }
42
43     public void beforeInitialize(CollectionPersister persister) {
44         this.set = new TreeSet JavaDoc(comparator);
45     }
46
47     public PersistentSortedSet(SessionImplementor session) {
48         super(session);
49     }
50
51     public PersistentSortedSet(SessionImplementor session, java.util.SortedSet JavaDoc set) {
52         super(session, set);
53         comparator = set.comparator();
54     }
55
56     public PersistentSortedSet() {} //needed for SOAP libraries, etc
57

58     /**
59      * @see PersistentSortedSet#comparator()
60      */

61     public Comparator JavaDoc comparator() {
62         return comparator;
63     }
64
65     /**
66      * @see PersistentSortedSet#subSet(Object,Object)
67      */

68     public java.util.SortedSet JavaDoc subSet(Object JavaDoc fromElement, Object JavaDoc toElement) {
69         read();
70         java.util.SortedSet JavaDoc s;
71         s = ( (java.util.SortedSet JavaDoc) set ).subSet(fromElement, toElement);
72         return new SubSetProxy(s);
73     }
74
75     /**
76      * @see PersistentSortedSet#headSet(Object)
77      */

78     public java.util.SortedSet JavaDoc headSet(Object JavaDoc toElement) {
79         read();
80         java.util.SortedSet JavaDoc s = ( (java.util.SortedSet JavaDoc) set ).headSet(toElement);
81         return new SubSetProxy(s);
82     }
83
84     /**
85      * @see PersistentSortedSet#tailSet(Object)
86      */

87     public java.util.SortedSet JavaDoc tailSet(Object JavaDoc fromElement) {
88         read();
89         java.util.SortedSet JavaDoc s = ( (java.util.SortedSet JavaDoc) set ).tailSet(fromElement);
90         return new SubSetProxy(s);
91     }
92
93     /**
94      * @see PersistentSortedSet#first()
95      */

96     public Object JavaDoc first() {
97         read();
98         return ( (java.util.SortedSet JavaDoc) set ).first();
99     }
100
101     /**
102      * @see PersistentSortedSet#last()
103      */

104     public Object JavaDoc last() {
105         read();
106         return ( (java.util.SortedSet JavaDoc) set ).last();
107     }
108
109     /** wrapper for subSets to propagate write to its backing set */
110     class SubSetProxy extends SetProxy implements java.util.SortedSet JavaDoc {
111
112         SubSetProxy(java.util.SortedSet JavaDoc s) {
113             super(s);
114         }
115
116         public Comparator JavaDoc comparator() {
117             return ( (java.util.SortedSet JavaDoc) this.set ).comparator();
118         }
119
120         public Object JavaDoc first() {
121             return ( (java.util.SortedSet JavaDoc) this.set ).first();
122         }
123
124         public java.util.SortedSet JavaDoc headSet(Object JavaDoc toValue) {
125             return new SubSetProxy( ( (java.util.SortedSet JavaDoc) this.set ).headSet(toValue) );
126         }
127
128         public Object JavaDoc last() {
129             return ( (java.util.SortedSet JavaDoc) this.set ).last();
130         }
131
132         public java.util.SortedSet JavaDoc subSet(Object JavaDoc fromValue, Object JavaDoc toValue) {
133             return new SubSetProxy( ( (java.util.SortedSet JavaDoc) this.set ).subSet(fromValue, toValue) );
134         }
135
136         public java.util.SortedSet JavaDoc tailSet(Object JavaDoc fromValue) {
137             return new SubSetProxy( ( (java.util.SortedSet JavaDoc) this.set ).tailSet(fromValue) );
138         }
139
140     }
141
142     public PersistentSortedSet(
143         SessionImplementor session,
144         CollectionPersister persister,
145         Comparator JavaDoc comparator,
146         Serializable JavaDoc disassembled,
147         Object JavaDoc owner)
148         throws HibernateException {
149
150         this(session);
151         this.comparator=comparator;
152         beforeInitialize(persister);
153         Serializable JavaDoc[] array = (Serializable JavaDoc[]) disassembled;
154         for (int i=0; i<array.length; i++ ) set.add(
155             persister.getElementType().assemble( array[i], session, owner )
156         );
157         setInitialized();
158     }
159
160 }
161
162
163
164
165
166
167
168
Popular Tags