KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > AbstractType


1 //$Id: AbstractType.java,v 1.21 2005/06/03 15:55:30 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.Map JavaDoc;
8
9 import org.dom4j.Element;
10 import org.dom4j.Node;
11 import org.hibernate.EntityMode;
12 import org.hibernate.HibernateException;
13 import org.hibernate.engine.SessionFactoryImplementor;
14 import org.hibernate.engine.SessionImplementor;
15 import org.hibernate.util.EqualsHelper;
16
17 /**
18  * Abstract superclass of the built in Type hierarchy.
19  * @author Gavin King
20  */

21 public abstract class AbstractType implements Type {
22
23     public boolean isAssociationType() {
24         return false;
25     }
26
27     public boolean isCollectionType() {
28         return false;
29     }
30
31     public boolean isComponentType() {
32         return false;
33     }
34
35     public boolean isEntityType() {
36         return false;
37     }
38     
39     public boolean isXMLElement() {
40         return false;
41     }
42
43     public int compare(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode) {
44         return ( (Comparable JavaDoc) x ).compareTo(y);
45     }
46
47     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
48     throws HibernateException {
49
50         if (value==null) {
51             return null;
52         }
53         else {
54             return (Serializable JavaDoc) deepCopy( value, session.getEntityMode(), session.getFactory() );
55         }
56     }
57
58     public Object JavaDoc assemble(Serializable JavaDoc cached, SessionImplementor session, Object JavaDoc owner)
59     throws HibernateException {
60         if ( cached==null ) {
61             return null;
62         }
63         else {
64             return deepCopy( cached, session.getEntityMode(), session.getFactory() );
65         }
66     }
67
68     public boolean isDirty(Object JavaDoc old, Object JavaDoc current, SessionImplementor session)
69     throws HibernateException {
70         return !isSame( old, current, session.getEntityMode() );
71     }
72
73     public Object JavaDoc hydrate(
74         ResultSet JavaDoc rs,
75         String JavaDoc[] names,
76         SessionImplementor session,
77         Object JavaDoc owner)
78     throws HibernateException, SQLException JavaDoc {
79         // TODO: this is very suboptimal for some subclasses (namely components),
80
// since it does not take advantage of two-phase-load
81
return nullSafeGet(rs, names, session, owner);
82     }
83
84     public Object JavaDoc resolve(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
85     throws HibernateException {
86         return value;
87     }
88
89     public Object JavaDoc semiResolve(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
90     throws HibernateException {
91         return value;
92     }
93     
94     public boolean isAnyType() {
95         return false;
96     }
97
98     public boolean isModified(Object JavaDoc old, Object JavaDoc current, SessionImplementor session)
99     throws HibernateException {
100         return isDirty(old, current, session);
101     }
102     
103     public boolean isSame(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode) throws HibernateException {
104         return isEqual(x, y, entityMode);
105     }
106
107     public boolean isEqual(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode) {
108         return EqualsHelper.equals(x, y);
109     }
110     
111     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
112         return x.hashCode();
113     }
114
115     public boolean isEqual(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode, SessionFactoryImplementor factory) {
116         return isEqual(x, y, entityMode);
117     }
118     
119     public int getHashCode(Object JavaDoc x, EntityMode entityMode, SessionFactoryImplementor factory) {
120         return getHashCode(x, entityMode);
121     }
122     
123     protected static void replaceNode(Node container, Element value) {
124         if ( container!=value ) { //not really necessary, I guess...
125
Element parent = container.getParent();
126             container.detach();
127             value.setName( container.getName() );
128             value.detach();
129             parent.add(value);
130         }
131     }
132     
133     public Type getSemiResolvedType(SessionFactoryImplementor factory) {
134         return this;
135     }
136
137     public Object JavaDoc replace(
138             Object JavaDoc original,
139             Object JavaDoc target,
140             SessionImplementor session,
141             Object JavaDoc owner,
142             Map JavaDoc copyCache,
143             ForeignKeyDirection foreignKeyDirection)
144     throws HibernateException {
145         boolean include;
146         if ( isAssociationType() ) {
147             AssociationType atype = (AssociationType) this;
148             include = atype.getForeignKeyDirection()==foreignKeyDirection;
149         }
150         else {
151             include = ForeignKeyDirection.FOREIGN_KEY_FROM_PARENT==foreignKeyDirection;
152         }
153         return include ? replace(original, target, session, owner, copyCache) : target;
154     }
155
156     /*public Object copy(Object original, Object target, SessionImplementor session, Object owner, Map copyCache)
157     throws HibernateException {
158         if (original==null) return null;
159         return assemble( disassemble(original, session), session, owner );
160     }*/

161
162 }
163
Popular Tags