|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
TupleSet.java | 66.7% | 68.4% | 63.6% | 66.7% |
|
1 | package org.drools.reteoo; | |
2 | ||
3 | /* | |
4 | * $Id: TupleSet.java,v 1.29 2004/12/14 19:45:05 dbarnett Exp $ | |
5 | * | |
6 | * Copyright 2001-2003 (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 trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. (http://werken.com/) | |
28 | * | |
29 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
30 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
39 | * POSSIBILITY OF SUCH DAMAGE. | |
40 | * | |
41 | */ | |
42 | ||
43 | import java.io.Serializable; | |
44 | import java.util.Collections; | |
45 | import java.util.HashMap; | |
46 | import java.util.Iterator; | |
47 | import java.util.Map; | |
48 | ||
49 | /** | |
50 | * A set of <code>Tuple<code>s indexed by <code>TupleKey<code>s. | |
51 | * | |
52 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a> | |
53 | */ | |
54 | class TupleSet | |
55 | implements | |
56 | Serializable | |
57 | { | |
58 | // ------------------------------------------------------------ | |
59 | // Instance members | |
60 | // ------------------------------------------------------------ | |
61 | ||
62 | /** Tuples, indexed by TupleKey. */ | |
63 | private Map tuples; | |
64 | ||
65 | // ------------------------------------------------------------ | |
66 | // Constructors | |
67 | // ------------------------------------------------------------ | |
68 | ||
69 | /** | |
70 | * Construct. | |
71 | */ | |
72 | 105 | TupleSet() |
73 | { | |
74 | 105 | this.tuples = new HashMap( ); |
75 | } | |
76 | ||
77 | /** | |
78 | * Construct. | |
79 | * @param initialCapacity The initial capacity of the set. | |
80 | * @param loadFactor The load factor of the set. | |
81 | */ | |
82 | 2095 | TupleSet( int initialCapacity, |
83 | float loadFactor ) | |
84 | { | |
85 | 2095 | this.tuples = new HashMap( initialCapacity, |
86 | loadFactor ); | |
87 | } | |
88 | ||
89 | /** | |
90 | * Construct with a single tuple. | |
91 | * | |
92 | * @param tuple The tuple. | |
93 | */ | |
94 | 0 | TupleSet(ReteTuple tuple) |
95 | { | |
96 | 0 | this.tuples = Collections.singletonMap( tuple.getKey( ), |
97 | tuple ); | |
98 | } | |
99 | ||
100 | /** | |
101 | * Retrieve the size (number of tuples) in this set. | |
102 | * | |
103 | * @return The size of this set. | |
104 | */ | |
105 | 2096 | public int size() |
106 | { | |
107 | 2096 | return this.tuples.size( ); |
108 | } | |
109 | ||
110 | /** | |
111 | * Return empty status | |
112 | * | |
113 | * @return The size of this set. | |
114 | */ | |
115 | 2095 | public boolean isEmpty() |
116 | { | |
117 | 2095 | return this.tuples.isEmpty( ); |
118 | } | |
119 | ||
120 | /** | |
121 | * Add a single <code>Tuple</code> to this set. | |
122 | * | |
123 | * @param tuple The tuple. | |
124 | */ | |
125 | 26892 | public void addTuple(ReteTuple tuple) |
126 | { | |
127 | 26892 | this.tuples.put( tuple.getKey( ), |
128 | tuple ); | |
129 | } | |
130 | ||
131 | /** | |
132 | * Retract tuples from this memory. | |
133 | * | |
134 | * @param key The key for the tuples to be removed. | |
135 | * @return <code>true</code> if at least one tuple was removed; <code>false</code> otherwise. | |
136 | */ | |
137 | 2924 | public boolean removeAllTuples(TupleKey key) |
138 | { | |
139 | 2924 | boolean removed = false; |
140 | 2924 | Iterator tupleIter = this.tuples.values( ).iterator( ); |
141 | 2924 | while ( tupleIter.hasNext( ) ) |
142 | { | |
143 | 41853 | if ( ( ( ReteTuple ) tupleIter.next( ) ).getKey( ).containsAll( key ) ) |
144 | { | |
145 | 1249 | tupleIter.remove( ); |
146 | 1249 | removed = true; |
147 | } | |
148 | } | |
149 | 2924 | return removed; |
150 | } | |
151 | ||
152 | /** | |
153 | * Retrieve a <code>Tuple</code> by <code>TupleKey</code>. | |
154 | * | |
155 | * @param key The tuple key. | |
156 | * | |
157 | * @return The matching tuple or <code>null</code> if this set contains no | |
158 | * matching tuple. | |
159 | */ | |
160 | 0 | public ReteTuple getTuple(TupleKey key) |
161 | { | |
162 | 0 | return ( ReteTuple ) this.tuples.get( key ); |
163 | } | |
164 | ||
165 | /** | |
166 | * Remove a tuple from this set. | |
167 | * | |
168 | * @param key Key matching the tuple. | |
169 | */ | |
170 | 0 | public void removeTuple(TupleKey key) |
171 | { | |
172 | 0 | if ( 1 == this.tuples.size( ) ) |
173 | { | |
174 | 0 | this.tuples = new HashMap( ); |
175 | } | |
176 | else | |
177 | { | |
178 | 0 | this.tuples.remove( key ); |
179 | } | |
180 | } | |
181 | ||
182 | /** | |
183 | * Retrieve an iterator over the tuples. | |
184 | * | |
185 | * @return The iterator. | |
186 | */ | |
187 | 3583 | public Iterator iterator() |
188 | { | |
189 | 3583 | return this.tuples.values( ).iterator( ); |
190 | } | |
191 | ||
192 | 0 | public String toString() |
193 | { | |
194 | 0 | return this.tuples.values( ).toString( ); |
195 | } | |
196 | } |
|