1 package org.hibernate.type; 3 4 import java.io.Serializable ; 5 import java.util.Comparator ; 6 import java.util.TreeMap ; 7 8 import org.dom4j.Element; 9 import org.hibernate.EntityMode; 10 import org.hibernate.collection.PersistentCollection; 11 import org.hibernate.collection.PersistentElementHolder; 12 import org.hibernate.collection.PersistentMapElementHolder; 13 import org.hibernate.collection.PersistentSortedMap; 14 import org.hibernate.engine.SessionImplementor; 15 import org.hibernate.persister.collection.CollectionPersister; 16 17 18 public class SortedMapType extends MapType { 19 20 private final Comparator comparator; 21 22 public SortedMapType(String role, String propertyRef, Comparator comparator, boolean isEmbeddedInXML) { 23 super(role, propertyRef, isEmbeddedInXML); 24 this.comparator = comparator; 25 } 26 27 public PersistentCollection instantiate(SessionImplementor session, CollectionPersister persister, Serializable key) { 28 if ( session.getEntityMode()==EntityMode.DOM4J ) { 29 return new PersistentMapElementHolder(session, persister, key); 30 } 31 else { 32 PersistentSortedMap map = new PersistentSortedMap(session); 33 map.setComparator(comparator); 34 return map; 35 } 36 } 37 38 public Class getReturnedClass() { 39 return java.util.SortedMap .class; 40 } 41 42 public Object instantiate(Object original) { 43 return new TreeMap (comparator); 44 } 45 46 public PersistentCollection wrap(SessionImplementor session, Object collection) { 47 if ( session.getEntityMode()==EntityMode.DOM4J ) { 48 return new PersistentElementHolder( session, (Element) collection ); 49 } 50 else { 51 return new PersistentSortedMap( session, (java.util.SortedMap ) collection ); 52 } 53 } 54 55 } 56 57 58 59 60 61 62 | Popular Tags |