|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
ClassFieldObjectType.java | 50% | 60.7% | 71.4% | 60.5% |
|
1 | package org.drools.semantics.base; | |
2 | ||
3 | /* | |
4 | * $Id: ClassFieldObjectType.java,v 1.7.2.1 2005/04/17 13:18:43 mproctor 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 that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a registered trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. | |
28 | * (http://drools.werken.com/). | |
29 | * | |
30 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
31 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
32 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
33 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
34 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
35 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
36 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
37 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
38 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
39 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
40 | * POSSIBILITY OF SUCH DAMAGE. | |
41 | * | |
42 | */ | |
43 | ||
44 | import java.lang.reflect.InvocationTargetException; | |
45 | import java.lang.reflect.Method; | |
46 | ||
47 | import org.drools.spi.ObjectType; | |
48 | ||
49 | /** | |
50 | * Java class semantics <code>ObjectType</code>. | |
51 | * | |
52 | * @author <a href="mailto:bob@werken.com">bob@werken.com </a> | |
53 | * | |
54 | * @version $Id: ClassFieldObjectType.java,v 1.7.2.1 2005/04/17 13:18:43 mproctor Exp $ | |
55 | */ | |
56 | public class ClassFieldObjectType extends ClassObjectType implements ObjectType | |
57 | { | |
58 | // ------------------------------------------------------------ | |
59 | // Instance members | |
60 | // ------------------------------------------------------------ | |
61 | /** Java object field. */ | |
62 | private String objectFieldName; | |
63 | ||
64 | private String objectFieldValue; | |
65 | ||
66 | /** Java getter method. */ | |
67 | private Method getterMethod; | |
68 | ||
69 | // ------------------------------------------------------------ | |
70 | // Constructors | |
71 | // ------------------------------------------------------------ | |
72 | ||
73 | /** | |
74 | * Construct. | |
75 | * | |
76 | * @param objectTypeClass Java object class. | |
77 | */ | |
78 | 25 | public ClassFieldObjectType( Class objectTypeClass, String fieldName, String fieldValue ) |
79 | { | |
80 | 25 | super( objectTypeClass ); |
81 | 25 | this.objectFieldName = fieldName; |
82 | 25 | this.objectFieldValue = fieldValue; |
83 | } | |
84 | ||
85 | /** | |
86 | * Return the Java object class. | |
87 | * | |
88 | * @return The Java object class. | |
89 | */ | |
90 | 42 | public String getFieldName( ) |
91 | { | |
92 | 42 | return this.objectFieldName; |
93 | } | |
94 | ||
95 | /** | |
96 | * Return the Java object class. | |
97 | * | |
98 | * @return The Java object class. | |
99 | */ | |
100 | 55 | public String getFieldValue( ) |
101 | { | |
102 | 55 | return this.objectFieldValue; |
103 | } | |
104 | ||
105 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
106 | // org.drools.spi.ObjectType | |
107 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
108 | ||
109 | /** | |
110 | * Determine if the passed <code>Object</code> belongs to the object type | |
111 | * defined by this <code>objectType</code> instance. | |
112 | * | |
113 | * @param object The <code>Object</code> to test. | |
114 | * | |
115 | * @return <code>true</code> if the <code>Object</code> matches this | |
116 | * object type, else <code>false</code>. | |
117 | */ | |
118 | 43 | public boolean matches( Object object ) |
119 | { | |
120 | 43 | if ( !this.getType( ).isInstance( object ) ) |
121 | { | |
122 | 18 | return false; |
123 | } | |
124 | ||
125 | 25 | if ( this.getterMethod == null ) |
126 | { | |
127 | 12 | String fieldName = this.getFieldName( ); |
128 | 12 | String fieldGetter = "get" + fieldName.toUpperCase( ).charAt( 0 ) |
129 | + fieldName.substring( 1 ); | |
130 | 12 | try |
131 | { | |
132 | 12 | this.getterMethod = getType( ).getMethod( fieldGetter, ( Class[] ) null ); |
133 | } | |
134 | catch ( NoSuchMethodException e ) | |
135 | { | |
136 | // shouldn't happen, this is checked in factory | |
137 | 0 | return false; |
138 | } | |
139 | } | |
140 | ||
141 | 25 | boolean result; |
142 | 25 | try |
143 | { | |
144 | 25 | result = this.getterMethod.invoke( object, ( Object[] )null ).equals( this.getFieldValue( ) ); |
145 | } | |
146 | catch ( IllegalAccessException e ) | |
147 | { | |
148 | // shouldn't happen, this is checked in factory | |
149 | 0 | result = false; |
150 | } | |
151 | catch ( InvocationTargetException e ) | |
152 | { | |
153 | // shouldn't happen, this is checked in factory | |
154 | 0 | result = false; |
155 | } | |
156 | ||
157 | 25 | return result; |
158 | } | |
159 | ||
160 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
161 | // java.lang.Object | |
162 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
163 | ||
164 | /** | |
165 | * Determine if another object is equal to this. | |
166 | * | |
167 | * @param object The object to test. | |
168 | * | |
169 | * @return <code>true</code> if <code>object</code> is equal to this, | |
170 | * otherwise <code>false</code>. | |
171 | */ | |
172 | 0 | public boolean equals( Object object ) |
173 | { | |
174 | 0 | if ( this == object ) |
175 | { | |
176 | 0 | return true; |
177 | } | |
178 | ||
179 | 0 | if ( object == null || getClass( ) != object.getClass( ) ) |
180 | { | |
181 | 0 | return false; |
182 | } | |
183 | ||
184 | 0 | ClassFieldObjectType other = ( ClassFieldObjectType ) object; |
185 | ||
186 | 0 | return getType( ).equals( other.getType( ) ) |
187 | && getFieldName().equals( other.getFieldName( ) ) | |
188 | && getFieldValue().equals( other.getFieldValue( ) ); | |
189 | } | |
190 | ||
191 | /** | |
192 | * Produce the hash of this object. | |
193 | * | |
194 | * @return The hash. | |
195 | */ | |
196 | 30 | public int hashCode( ) |
197 | { | |
198 | 30 | return getType( ).hashCode( ) ^ getFieldName().hashCode( ) ^ getFieldValue( ).hashCode( ); |
199 | } | |
200 | ||
201 | 0 | public String toString( ) |
202 | { | |
203 | 0 | String fieldName = getFieldName( ); |
204 | 0 | return getType( ).getName( ) + ".get" + fieldName.toUpperCase( ).charAt( 0 ) |
205 | + fieldName.substring( 1 ) + "(\"" + getFieldValue( ) + "\")"; | |
206 | } | |
207 | } |
|