1
//$Id: PersistentSortedSet.java 7714 2005-08-01 16:29:33Z oneovthafew $
2
package org.hibernate.collection;
4
import java.io.Serializable;
5
import java.util.Comparator;
6
import java.util.Iterator;
7
import java.util.SortedSet;
8
import java.util.TreeMap;
10
import org.hibernate.EntityMode;
11
import org.hibernate.HibernateException;
12
import org.hibernate.engine.SessionImplementor;
13
import org.hibernate.persister.collection.BasicCollectionPersister;
17
* A persistent wrapper for a <tt>java.util.SortedSet</tt>. Underlying
18
* collection is a <tt>TreeSet</tt>.
20
* @see java.util.TreeSet
21
* @author <a href="mailto:doug.currie@alum.mit.edu">e</a>
23
public class PersistentSortedSet extends PersistentSet implements SortedSet {
25
protected Comparator comparator;
27
protected Serializable snapshot(BasicCollectionPersister persister, EntityMode entityMode)
28
throws HibernateException {
29
//if (set==null) return new Set(session);
30
TreeMap clonedSet = new TreeMap(comparator);
31
Iterator iter = set.iterator();
32
while ( iter.hasNext() ) {
33
Object copy = persister.getElementType().deepCopy( iter.next(), entityMode, persister.getFactory() );
34
clonedSet.put(copy, copy);
39
public void setComparator(Comparator comparator) {
40
this.comparator = comparator;
43
public PersistentSortedSet(SessionImplementor session) {
47
public PersistentSortedSet(SessionImplementor session, SortedSet set) {
49
comparator = set.comparator();
52
public PersistentSortedSet() {} //needed for SOAP libraries, etc
55
* @see PersistentSortedSet#comparator()
57
public Comparator comparator() {
62
* @see PersistentSortedSet#subSet(Object,Object)
64
public SortedSet subSet(Object fromElement, Object toElement) {
67
s = ( (SortedSet) set ).subSet(fromElement, toElement);
68
return new SubSetProxy(s);
72
* @see PersistentSortedSet#headSet(Object)
74
public SortedSet headSet(Object toElement) {
76
SortedSet s = ( (SortedSet) set ).headSet(toElement);
77
return new SubSetProxy(s);
81
* @see PersistentSortedSet#tailSet(Object)
83
public SortedSet tailSet(Object fromElement) {
85
SortedSet s = ( (SortedSet) set ).tailSet(fromElement);
86
return new SubSetProxy(s);
90
* @see PersistentSortedSet#first()
92
public Object first() {
94
return ( (SortedSet) set ).first();
98
* @see PersistentSortedSet#last()
100
public Object last() {
102
return ( (SortedSet) set ).last();
105
/** wrapper for subSets to propagate write to its backing set */
106
class SubSetProxy extends SetProxy implements SortedSet {
108
SubSetProxy(SortedSet s) {
112
public Comparator comparator() {
113
return ( (SortedSet) this.set ).comparator();
116
public Object first() {
117
return ( (SortedSet) this.set ).first();
120
public SortedSet headSet(Object toValue) {
121
return new SubSetProxy( ( (SortedSet) this.set ).headSet(toValue) );
124
public Object last() {
125
return ( (SortedSet) this.set ).last();
128
public SortedSet subSet(Object fromValue, Object toValue) {
129
return new SubSetProxy( ( (SortedSet) this.set ).subSet(fromValue, toValue) );
132
public SortedSet tailSet(Object fromValue) {
133
return new SubSetProxy( ( (SortedSet) this.set ).tailSet(fromValue) );