1 package org.hibernate.collection; 3 4 import java.io.Serializable ; 5 import java.util.Iterator ; 6 import java.util.TreeMap ; 7 import java.util.TreeSet ; 8 import java.util.Comparator ; 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 24 public class PersistentSortedSet extends PersistentSet implements java.util.SortedSet { 25 26 protected Comparator comparator; 27 28 protected Serializable snapshot(BasicCollectionPersister persister, EntityMode entityMode) throws HibernateException { 29 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 void beforeInitialize(CollectionPersister persister) { 44 this.set = new TreeSet (comparator); 45 } 46 47 public PersistentSortedSet(SessionImplementor session) { 48 super(session); 49 } 50 51 public PersistentSortedSet(SessionImplementor session, java.util.SortedSet set) { 52 super(session, set); 53 comparator = set.comparator(); 54 } 55 56 public PersistentSortedSet() {} 58 61 public Comparator comparator() { 62 return comparator; 63 } 64 65 68 public java.util.SortedSet subSet(Object fromElement, Object toElement) { 69 read(); 70 java.util.SortedSet s; 71 s = ( (java.util.SortedSet ) set ).subSet(fromElement, toElement); 72 return new SubSetProxy(s); 73 } 74 75 78 public java.util.SortedSet headSet(Object toElement) { 79 read(); 80 java.util.SortedSet s = ( (java.util.SortedSet ) set ).headSet(toElement); 81 return new SubSetProxy(s); 82 } 83 84 87 public java.util.SortedSet tailSet(Object fromElement) { 88 read(); 89 java.util.SortedSet s = ( (java.util.SortedSet ) set ).tailSet(fromElement); 90 return new SubSetProxy(s); 91 } 92 93 96 public Object first() { 97 read(); 98 return ( (java.util.SortedSet ) set ).first(); 99 } 100 101 104 public Object last() { 105 read(); 106 return ( (java.util.SortedSet ) set ).last(); 107 } 108 109 110 class SubSetProxy extends SetProxy implements java.util.SortedSet { 111 112 SubSetProxy(java.util.SortedSet s) { 113 super(s); 114 } 115 116 public Comparator comparator() { 117 return ( (java.util.SortedSet ) this.set ).comparator(); 118 } 119 120 public Object first() { 121 return ( (java.util.SortedSet ) this.set ).first(); 122 } 123 124 public java.util.SortedSet headSet(Object toValue) { 125 return new SubSetProxy( ( (java.util.SortedSet ) this.set ).headSet(toValue) ); 126 } 127 128 public Object last() { 129 return ( (java.util.SortedSet ) this.set ).last(); 130 } 131 132 public java.util.SortedSet subSet(Object fromValue, Object toValue) { 133 return new SubSetProxy( ( (java.util.SortedSet ) this.set ).subSet(fromValue, toValue) ); 134 } 135 136 public java.util.SortedSet tailSet(Object fromValue) { 137 return new SubSetProxy( ( (java.util.SortedSet ) this.set ).tailSet(fromValue) ); 138 } 139 140 } 141 142 public PersistentSortedSet( 143 SessionImplementor session, 144 CollectionPersister persister, 145 Comparator comparator, 146 Serializable disassembled, 147 Object owner) 148 throws HibernateException { 149 150 this(session); 151 this.comparator=comparator; 152 beforeInitialize(persister); 153 Serializable [] array = (Serializable []) 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 |