1 package org.hibernate.tuple; 3 4 import org.hibernate.mapping.PersistentClass; 5 import org.hibernate.mapping.Component; 6 7 import java.io.Serializable ; 8 import java.util.HashMap ; 9 import java.util.Map ; 10 import java.util.Set ; 11 import java.util.HashSet ; 12 import java.util.Iterator ; 13 14 15 public class DynamicMapInstantiator implements Instantiator { 16 public static final String KEY = "$type$"; 17 18 private String entityName; 19 private Set isInstanceEntityNames = new HashSet (); 20 21 public DynamicMapInstantiator() { 22 this.entityName = null; 23 } 24 25 public DynamicMapInstantiator(PersistentClass mappingInfo) { 26 this.entityName = mappingInfo.getEntityName(); 27 isInstanceEntityNames.add( entityName ); 28 if ( mappingInfo.hasSubclasses() ) { 29 Iterator itr = mappingInfo.getSubclassClosureIterator(); 30 while ( itr.hasNext() ) { 31 final PersistentClass subclassInfo = ( PersistentClass ) itr.next(); 32 isInstanceEntityNames.add( subclassInfo.getEntityName() ); 33 } 34 } 35 } 36 37 public final Object instantiate(Serializable id) { 38 return instantiate(); 39 } 40 41 public final Object instantiate() { 42 Map map = generateMap(); 43 if ( entityName!=null ) { 44 map.put( KEY, entityName ); 45 } 46 return map; 47 } 48 49 public final boolean isInstance(Object object) { 50 if ( object instanceof Map ) { 51 if ( entityName == null ) { 52 return true; 53 } 54 String type = ( String ) ( ( Map ) object ).get( KEY ); 55 return type == null || isInstanceEntityNames.contains( type ); 56 } 57 else { 58 return false; 59 } 60 } 61 62 protected Map generateMap() { 63 return new HashMap (); 64 } 65 } | Popular Tags |