~ubuntu-branches/ubuntu/wily/libhibernate3-java/wily-proposed

« back to all changes in this revision

Viewing changes to src/org/hibernate/collection/PersistentSortedSet.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2007-10-14 14:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20071014144334-eamc8i0q10gs1aro
Tags: upstream-3.2.5
ImportĀ upstreamĀ versionĀ 3.2.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//$Id: PersistentSortedSet.java 7714 2005-08-01 16:29:33Z oneovthafew $
 
2
package org.hibernate.collection;
 
3
 
 
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;
 
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
 
 
15
 
 
16
/**
 
17
 * A persistent wrapper for a <tt>java.util.SortedSet</tt>. Underlying
 
18
 * collection is a <tt>TreeSet</tt>.
 
19
 *
 
20
 * @see java.util.TreeSet
 
21
 * @author <a href="mailto:doug.currie@alum.mit.edu">e</a>
 
22
 */
 
23
public class PersistentSortedSet extends PersistentSet implements SortedSet {
 
24
 
 
25
        protected Comparator comparator;
 
26
 
 
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);
 
35
                }
 
36
                return clonedSet;
 
37
        }
 
38
 
 
39
        public void setComparator(Comparator comparator) {
 
40
                this.comparator = comparator;
 
41
        }
 
42
 
 
43
        public PersistentSortedSet(SessionImplementor session) {
 
44
                super(session);
 
45
        }
 
46
 
 
47
        public PersistentSortedSet(SessionImplementor session, SortedSet set) {
 
48
                super(session, set);
 
49
                comparator = set.comparator();
 
50
        }
 
51
 
 
52
        public PersistentSortedSet() {} //needed for SOAP libraries, etc
 
53
 
 
54
        /**
 
55
         * @see PersistentSortedSet#comparator()
 
56
         */
 
57
        public Comparator comparator() {
 
58
                return comparator;
 
59
        }
 
60
 
 
61
        /**
 
62
         * @see PersistentSortedSet#subSet(Object,Object)
 
63
         */
 
64
        public SortedSet subSet(Object fromElement, Object toElement) {
 
65
                read();
 
66
                SortedSet s;
 
67
                s = ( (SortedSet) set ).subSet(fromElement, toElement);
 
68
                return new SubSetProxy(s);
 
69
        }
 
70
 
 
71
        /**
 
72
         * @see PersistentSortedSet#headSet(Object)
 
73
         */
 
74
        public SortedSet headSet(Object toElement) {
 
75
                read();
 
76
                SortedSet s = ( (SortedSet) set ).headSet(toElement);
 
77
                return new SubSetProxy(s);
 
78
        }
 
79
 
 
80
        /**
 
81
         * @see PersistentSortedSet#tailSet(Object)
 
82
         */
 
83
        public SortedSet tailSet(Object fromElement) {
 
84
                read();
 
85
                SortedSet s = ( (SortedSet) set ).tailSet(fromElement);
 
86
                return new SubSetProxy(s);
 
87
        }
 
88
 
 
89
        /**
 
90
         * @see PersistentSortedSet#first()
 
91
         */
 
92
        public Object first() {
 
93
                read();
 
94
                return ( (SortedSet) set ).first();
 
95
        }
 
96
 
 
97
        /**
 
98
         * @see PersistentSortedSet#last()
 
99
         */
 
100
        public Object last() {
 
101
                read();
 
102
                return ( (SortedSet) set ).last();
 
103
        }
 
104
 
 
105
        /** wrapper for subSets to propagate write to its backing set */
 
106
        class SubSetProxy extends SetProxy implements SortedSet {
 
107
 
 
108
                SubSetProxy(SortedSet s) {
 
109
                        super(s);
 
110
                }
 
111
 
 
112
                public Comparator comparator() {
 
113
                        return ( (SortedSet) this.set ).comparator();
 
114
                }
 
115
 
 
116
                public Object first() {
 
117
                        return ( (SortedSet) this.set ).first();
 
118
                }
 
119
 
 
120
                public SortedSet headSet(Object toValue) {
 
121
                        return new SubSetProxy( ( (SortedSet) this.set ).headSet(toValue) );
 
122
                }
 
123
 
 
124
                public Object last() {
 
125
                        return ( (SortedSet) this.set ).last();
 
126
                }
 
127
 
 
128
                public SortedSet subSet(Object fromValue, Object toValue) {
 
129
                        return new SubSetProxy( ( (SortedSet) this.set ).subSet(fromValue, toValue) );
 
130
                }
 
131
 
 
132
                public SortedSet tailSet(Object fromValue) {
 
133
                        return new SubSetProxy( ( (SortedSet) this.set ).tailSet(fromValue) );
 
134
                }
 
135
 
 
136
        }
 
137
 
 
138
}
 
139
 
 
140
 
 
141
 
 
142
 
 
143
 
 
144
 
 
145