1 package org.drools.semantics.java;
2
3 /*
4 $Id: ClassObjectType.java,v 1.8 2003/03/25 19:47:29 tdiesler Exp $
5
6 Copyright 2002 (C) The Werken Company. All Rights Reserved.
7
8 Redistribution and use of this software and associated documentation
9 ("Software"), with or without modification, are permitted provided
10 that the following conditions are met:
11
12 1. Redistributions of source code must retain copyright
13 statements and notices. Redistributions must also contain a
14 copy of this document.
15
16 2. Redistributions in binary form must reproduce the
17 above copyright notice, this list of conditions and the
18 following disclaimer in the documentation and/or other
19 materials provided with the distribution.
20
21 3. The name "drools" must not be used to endorse or promote
22 products derived from this Software without prior written
23 permission of The Werken Company. For written permission,
24 please contact bob@werken.com.
25
26 4. Products derived from this Software may not be called "drools"
27 nor may "drools" appear in their names without prior written
28 permission of The Werken Company. "drools" is a registered
29 trademark of The Werken Company.
30
31 5. Due credit should be given to The Werken Company.
32 (http://drools.werken.com/).
33
34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS
35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
45 OF THE POSSIBILITY OF SUCH DAMAGE.
46
47 */
48
49 import org.apache.commons.beanutils.ConversionException;
50 import org.apache.commons.beanutils.ConvertUtils;
51 import org.apache.commons.beanutils.Converter;
52 import org.drools.spi.ObjectType;
53
54 /*** Java class semantics <code>ObjectType</code>.
55 *
56 * @author <a href="mailto:bob@werken.com">bob@werken.com</a>
57 *
58 * @version $Id: ClassObjectType.java,v 1.8 2003/03/25 19:47:29 tdiesler Exp $
59 */
60 public class ClassObjectType implements ObjectType
61 {
62
63 // ------------------------------------------------------------
64 // Class initialization
65 // ------------------------------------------------------------
66
67 /*** Register conversions for jakarta-beanutils.
68 */
69 static
70 {
71 ConvertUtils.register(
72 new Converter()
73 {
74 public Object convert(Class type, Object value)
75 {
76 if ( value instanceof Class )
77 {
78 return (Class) value;
79 }
80 else if ( value instanceof String )
81 {
82 ClassLoader cl = Thread.currentThread().getContextClassLoader();
83
84 if ( cl == null )
85 {
86 cl = getClass().getClassLoader();
87 }
88 try
89 {
90 return cl.loadClass( (String) value );
91 }
92 catch (Exception e)
93 {
94 throw new ConversionException( "Cannot convert " + value + " to a java.lang.Class",
95 e );
96 }
97 }
98 throw new ConversionException( "Cannot convert " + value + " to a java.lang.Class" );
99 }
100 },
101 Class.class
102 );
103 }
104
105 // ------------------------------------------------------------
106 // Instance members
107 // ------------------------------------------------------------
108
109 /*** Java object class. */
110 private Class objectTypeClass;
111
112 // ------------------------------------------------------------
113 // Constructors
114 // ------------------------------------------------------------
115
116 /*** Construct, partially.
117 *
118 * @see #setType
119 */
120 public ClassObjectType()
121 {
122 this.objectTypeClass = java.lang.Object.class;
123 }
124
125 /*** Construct.
126 *
127 * @param objectTypeClass Java object class.
128 */
129 public ClassObjectType(Class objectTypeClass)
130 {
131 this.objectTypeClass = objectTypeClass;
132 }
133
134 // ------------------------------------------------------------
135 // Instance methods
136 // ------------------------------------------------------------
137
138 /*** Set the Java object class.
139 *
140 * @param objectTypeClass The Java object class.
141 */
142 public void setType(Class objectTypeClass)
143 {
144 this.objectTypeClass = objectTypeClass;
145 }
146
147 /*** Return the Java object class.
148 *
149 * @return The Java object class.
150 */
151 public Class getType()
152 {
153 return this.objectTypeClass;
154 }
155
156 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
157 // org.drools.spi.ObjectType
158 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
159
160 /*** Determine if the passed <code>Object</code>
161 * belongs to the object type defined by this
162 * <code>objectType</code> instance.
163 *
164 * @param object The <code>Object</code> to test.
165 *
166 * @return <code>true</code> if the <code>Object</code>
167 * matches this object type, else <code>false</code>.
168 */
169 public boolean matches(Object object)
170 {
171 return getType().isInstance( object );
172 }
173
174 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
175 // java.lang.Object
176 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
177
178 /*** Determine if another object is equal to this.
179 *
180 * @param thatObj The object to test.
181 *
182 * @return <code>true</code> if <code>thatObj</code> is equal
183 * to this, otherwise <code>false</code>.
184 */
185 public boolean equals(Object thatObj)
186 {
187 if ( thatObj == null
188 ||
189 ( ! ( thatObj instanceof ClassObjectType ) ) )
190 {
191 return false;
192 }
193
194 ClassObjectType that = (ClassObjectType) thatObj;
195
196 return ( getType().equals( that.getType() ) );
197 }
198
199 /*** Produce a debug string.
200 *
201 * @return The debug string.
202 */
203 public String toString()
204 {
205 return "[ClassObjectType: type=" + getType().getName() + "]";
206 }
207
208 /*** Produce the hash of this object.
209 *
210 * @return The hash.
211 */
212 public int hashCode()
213 {
214 return getType().hashCode();
215 }
216 }
This page was automatically generated by Maven