1 package org.drools.reteoo.impl;
2
3 /*
4 $Id: TupleKey.java,v 1.3 2002/08/01 18:47:33 bob 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.drools.rule.Declaration;
50
51 import java.util.Set;
52 import java.util.HashSet;
53 import java.util.Map;
54 import java.util.HashMap;
55 import java.util.Iterator;
56
57 /*** A composite key to match tuples.
58 *
59 * @see TupleImpl
60 *
61 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
62 */
63 class TupleKey
64 {
65 // ------------------------------------------------------------
66 // Instance members
67 // ------------------------------------------------------------
68
69 /*** Columns. */
70 private Map columns;
71
72 /*** Root fact objects. */
73 private Set rootFactObjects;
74
75 // ------------------------------------------------------------
76 // Constructors
77 // ------------------------------------------------------------
78
79 /*** Construct.
80 */
81 public TupleKey()
82 {
83 this.columns = new HashMap();
84 this.rootFactObjects = new HashSet();
85 }
86
87 /*** Construct a simple 1-column tuple-key.
88 *
89 * @param declaration Column declaration.
90 * @param value Column value.
91 */
92 public TupleKey(Declaration declaration,
93 Object value)
94 {
95 this();
96
97 put( declaration,
98 value );
99 }
100
101 /*** Copy constructor.
102 *
103 * @param that The tuple key to copy.
104 */
105 public TupleKey(TupleKey that)
106 {
107 this();
108
109 this.columns.putAll( that.columns );
110 this.rootFactObjects.addAll( that.rootFactObjects );
111 }
112
113 // ------------------------------------------------------------
114 //
115 // ------------------------------------------------------------
116
117 /*** Put all values from another key into this key.
118 *
119 * @param key The source value key.
120 */
121 public void putAll(TupleKey key)
122 {
123 this.columns.putAll( key.columns );
124 this.rootFactObjects.addAll( key.rootFactObjects );
125 }
126
127 /*** Put a value for a declaration.
128 *
129 * @param declaration Column declaration.
130 * @param value The value.
131 */
132 public void put(Declaration declaration,
133 Object value)
134 {
135 this.columns.put( declaration,
136 value );
137
138 this.rootFactObjects.add( value );
139 }
140
141 /*** Retrieve a value by declaration.
142 *
143 * @param declaration The declaration.
144 *
145 * @return The value.
146 */
147 public Object get(Declaration declaration)
148 {
149 return this.columns.get( declaration );
150 }
151
152 /*** Determine if this key contains the specified declaration.
153 *
154 * @param declaration The declaration to test.
155 *
156 * @return <code>true</code> if this key contains a column
157 * for the specified declaration, otherwise
158 * <code>false</code>.
159 */
160 public boolean containsDeclaration(Declaration declaration)
161 {
162 return this.columns.containsKey( declaration );
163 }
164
165 /*** Determine if this key contains the specified root fact object.
166 *
167 * @param object The object to test.
168 *
169 * @return <code>true</code> if this key contains the
170 * specified root fact object, otherwise
171 * <code>false</code>.
172 */
173 public boolean containsRootFactObject(Object object)
174 {
175 return this.rootFactObjects.contains( object );
176 }
177
178 /*** Retrieve the number of columns in this key.
179 *
180 * @return The number of columns.
181 */
182 public int size()
183 {
184 return this.columns.size();
185 }
186
187 /*** Retrieve the declarations of this key.
188 *
189 * @see Declaration
190 *
191 * @return The set of declarations for this key.
192 */
193 public Set getDeclarations()
194 {
195 return this.columns.keySet();
196 }
197
198 /*** Determine if this key is a super-set of another key.
199 *
200 * @param that The key to test for subset.
201 *
202 * @return <code>true</code> if this key is a super-set of
203 * the specified parameter key, otherwise <code>false</code>.
204 */
205 public boolean containsAll(TupleKey that)
206 {
207 Iterator declIter = that.columns.keySet().iterator();
208 Declaration eachDecl = null;
209
210 Object thisValue = null;
211 Object thatValue = null;
212
213 while ( declIter.hasNext() )
214 {
215 eachDecl = (Declaration) declIter.next();
216
217 thisValue = this.get( eachDecl );
218 thatValue = that.get( eachDecl );
219
220 if ( thisValue == null
221 &&
222 thatValue == null )
223 {
224 continue;
225 }
226 else if (thisValue == null
227 ||
228 thatValue == null )
229 {
230 return false;
231 }
232 else if ( ! thisValue.equals( thatValue ) )
233 {
234 return false;
235 }
236 }
237
238 return true;
239 }
240
241 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
242 // java.lang.Object
243 // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
244
245 /*** Determine if this key is semantically equal to another.
246 *
247 * @param thatObj The object to compare.
248 *
249 * @return <code>true</code> if the two objects are semantically
250 * equal, otherwise <code>false</code>.
251 */
252 public boolean equals(Object thatObj)
253 {
254 TupleKey that = (TupleKey) thatObj;
255
256 return this.columns.equals( that.columns );
257 }
258
259 /*** Retrieve the hash-code for this key.
260 *
261 * @return The hash-code.
262 */
263 public int hashCode()
264 {
265 return this.columns.hashCode();
266 }
267
268 /*** Produce a debug string.
269 *
270 * @return The debug string.
271 */
272 public String toString()
273 {
274 return "[TupleKey: columns=" + this.columns + "]";
275 }
276 }
This page was automatically generated by Maven