1 package org.hibernate.collection; 3 4 5 import java.io.Serializable ; 6 import java.util.Iterator ; 7 import java.util.TreeMap ; 8 import java.util.Comparator ; 9 import java.util.Collection ; 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 25 public class PersistentSortedMap extends PersistentMap implements java.util.SortedMap { 26 27 protected Comparator comparator; 28 29 protected Serializable snapshot(BasicCollectionPersister persister, EntityMode entityMode) throws HibernateException { 30 TreeMap clonedMap = new TreeMap (comparator); 31 Iterator 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 comparator, Serializable disassembled, Object owner) 44 throws HibernateException { 45 super(session); 46 this.comparator=comparator; 47 beforeInitialize(persister); 48 Serializable [] array = (Serializable []) 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 (comparator); 58 } 59 60 public void setComparator(Comparator comparator) { 61 this.comparator = comparator; 62 } 63 64 69 70 public PersistentSortedMap(SessionImplementor session, java.util.SortedMap map) { 71 super(session, map); 72 comparator = map.comparator(); 73 } 74 75 public PersistentSortedMap() {} 77 80 public Comparator comparator() { 81 return comparator; 82 } 83 84 87 public java.util.SortedMap subMap(Object fromKey, Object toKey) { 88 read(); 89 java.util.SortedMap m = ( (java.util.SortedMap ) map ).subMap(fromKey, toKey); 90 return new SortedSubMap(m); 91 } 92 93 96 public java.util.SortedMap headMap(Object toKey) { 97 read(); 98 java.util.SortedMap m; 99 m = ( (java.util.SortedMap ) map ).headMap(toKey); 100 return new SortedSubMap(m); 101 } 102 103 106 public java.util.SortedMap tailMap(Object fromKey) { 107 read(); 108 java.util.SortedMap m; 109 m = ( (java.util.SortedMap ) map ).tailMap(fromKey); 110 return new SortedSubMap(m); 111 } 112 113 116 public Object firstKey() { 117 read(); 118 return ( (java.util.SortedMap ) map ).firstKey(); 119 } 120 121 124 public Object lastKey() { 125 read(); 126 return ( (java.util.SortedMap ) map ).lastKey(); 127 } 128 129 class SortedSubMap implements java.util.SortedMap { 130 131 java.util.SortedMap submap; 132 133 SortedSubMap(java.util.SortedMap m) { 134 this.submap = m; 135 } 136 public int size() { 138 return submap.size(); 139 } 140 public boolean isEmpty() { 141 return submap.isEmpty(); 142 } 143 public boolean containsKey(Object key) { 144 return submap.containsKey(key); 145 } 146 public boolean containsValue(Object key) { 147 return submap.containsValue(key) ; 148 } 149 public Object get(Object key) { 150 return submap.get(key); 151 } 152 public Object put(Object key, Object value) { 153 write(); 154 return submap.put(key, value); 155 } 156 public Object remove(Object key) { 157 write(); 158 return submap.remove(key); 159 } 160 public void putAll(java.util.Map other) { 161 write(); 162 submap.putAll(other); 163 } 164 public void clear() { 165 write(); 166 submap.clear(); 167 } 168 public java.util.Set keySet() { 169 return new SetProxy( submap.keySet() ); 170 } 171 public Collection values() { 172 return new SetProxy( submap.values() ); 173 } 174 public java.util.Set entrySet() { 175 return new EntrySetProxy( submap.entrySet() ); 176 } 177 public Comparator comparator() { 179 return submap.comparator(); 180 } 181 public java.util.SortedMap subMap(Object fromKey, Object toKey) { 182 java.util.SortedMap m; 183 m = submap.subMap(fromKey, toKey); 184 return new SortedSubMap( m ); 185 } 186 public java.util.SortedMap headMap(Object toKey) { 187 java.util.SortedMap m; 188 m = submap.headMap(toKey); 189 return new SortedSubMap(m); 190 } 191 public java.util.SortedMap tailMap(Object fromKey) { 192 java.util.SortedMap m; 193 m = submap.tailMap(fromKey); 194 return new SortedSubMap(m); 195 } 196 public Object firstKey() { 197 return submap.firstKey(); 198 } 199 public Object lastKey() { 200 return submap.lastKey(); 201 } 202 203 } 204 205 } 206 207 208 209 210 211 212 213 | Popular Tags |