01: package org.drools.util;
02:
03: import java.util.NoSuchElementException;
04:
05: import org.drools.FactHandle;
06: import org.drools.ObjectFilter;
07: import org.drools.base.ShadowProxy;
08: import org.drools.common.InternalFactHandle;
09: import org.drools.util.ObjectHashMap.ObjectEntry;
10:
11: public class JavaIteratorAdapter implements java.util.Iterator {
12: public static int OBJECT = 0;
13: public static int FACT_HANDLE = 1;
14:
15: private Iterator iterator;
16: private ObjectEntry nextEntry;
17: // private Object nextObject;
18: // private InternalFactHandle nextHandle;
19: private ObjectFilter filter;
20: private int type;
21:
22: public JavaIteratorAdapter(Iterator iterator, int type) {
23: this (iterator, type, null);
24: }
25:
26: public JavaIteratorAdapter(Iterator iterator, int type,
27: ObjectFilter filter) {
28: this .iterator = iterator;
29: this .filter = filter;
30: this .type = type;
31: setNext();
32: }
33:
34: public boolean hasNext() {
35: return (this .nextEntry != null);
36: }
37:
38: public Object next() {
39: ObjectEntry current = this .nextEntry;
40:
41: if (current != null) {
42: setNext();
43: } else {
44: throw new NoSuchElementException(
45: "No more elements to return");
46: }
47:
48: if (this .type == OBJECT) {
49: InternalFactHandle handle = (InternalFactHandle) current
50: .getKey();
51: Object object = (handle.isShadowFact()) ? ((ShadowProxy) handle
52: .getObject()).getShadowedObject()
53: : handle.getObject();
54: return object;
55: } else {
56: return current.getKey();
57: }
58: }
59:
60: private void setNext() {
61: ObjectEntry entry = null;
62:
63: while (entry == null) {
64: entry = (ObjectEntry) this .iterator.next();
65: if (entry == null) {
66: break;
67: }
68: if (this .filter != null) {
69: InternalFactHandle handle = (InternalFactHandle) entry
70: .getKey();
71: Object object = (handle.isShadowFact()) ? ((ShadowProxy) handle
72: .getObject()).getShadowedObject()
73: : handle.getObject();
74: if (this .filter.accept(object) == false) {
75: entry = null;
76: }
77: }
78: }
79:
80: this .nextEntry = entry;
81: // this.nextHandle = handle;
82: // this.nextObject = object;
83: }
84:
85: public void remove() {
86: throw new UnsupportedOperationException(
87: "remove() is not support");
88: }
89: }
|