KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > tuple > DynamicMapInstantiator


1 //$Id: DynamicMapInstantiator.java,v 1.2 2005/07/11 17:31:50 steveebersole Exp $
2
package org.hibernate.tuple;
3
4 import org.hibernate.mapping.PersistentClass;
5 import org.hibernate.mapping.Component;
6
7 import java.io.Serializable JavaDoc;
8 import java.util.HashMap JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Set JavaDoc;
11 import java.util.HashSet JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14
15 public class DynamicMapInstantiator implements Instantiator {
16     public static final String JavaDoc KEY = "$type$";
17
18     private String JavaDoc entityName;
19     private Set JavaDoc isInstanceEntityNames = new HashSet JavaDoc();
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 JavaDoc 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 JavaDoc instantiate(Serializable JavaDoc id) {
38         return instantiate();
39     }
40
41     public final Object JavaDoc instantiate() {
42         Map JavaDoc map = generateMap();
43         if ( entityName!=null ) {
44             map.put( KEY, entityName );
45         }
46         return map;
47     }
48
49     public final boolean isInstance(Object JavaDoc object) {
50         if ( object instanceof Map JavaDoc ) {
51             if ( entityName == null ) {
52                 return true;
53             }
54             String JavaDoc type = ( String JavaDoc ) ( ( Map JavaDoc ) object ).get( KEY );
55             return type == null || isInstanceEntityNames.contains( type );
56         }
57         else {
58             return false;
59         }
60     }
61
62     protected Map JavaDoc generateMap() {
63         return new HashMap JavaDoc();
64     }
65 }
Popular Tags