001: package org.drools.event;
002:
003: /*
004: * Copyright 2005 JBoss Inc
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.io.Serializable;
020: import java.util.ArrayList;
021: import java.util.Collections;
022: import java.util.List;
023:
024: import org.drools.FactHandle;
025: import org.drools.WorkingMemory;
026: import org.drools.common.InternalWorkingMemory;
027: import org.drools.spi.PropagationContext;
028:
029: /**
030: * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris </a>
031: */
032: public class WorkingMemoryEventSupport implements Serializable {
033: /**
034: *
035: */
036: private static final long serialVersionUID = 400L;
037: private final List listeners = Collections
038: .synchronizedList(new ArrayList());
039:
040: public WorkingMemoryEventSupport() {
041: }
042:
043: public void addEventListener(
044: final WorkingMemoryEventListener listener) {
045: if (!this .listeners.contains(listener)) {
046: this .listeners.add(listener);
047: }
048: }
049:
050: public void removeEventListener(
051: final WorkingMemoryEventListener listener) {
052: this .listeners.remove(listener);
053: }
054:
055: public List getEventListeners() {
056: return Collections.unmodifiableList(this .listeners);
057: }
058:
059: public int size() {
060: return this .listeners.size();
061: }
062:
063: public boolean isEmpty() {
064: return this .listeners.isEmpty();
065: }
066:
067: public void fireObjectInserted(
068: final PropagationContext propagationContext,
069: final FactHandle handle, final Object object,
070: final InternalWorkingMemory workingMemory) {
071: if (this .listeners.isEmpty()) {
072: return;
073: }
074:
075: final ObjectInsertedEvent event = new ObjectInsertedEvent(
076: workingMemory, propagationContext, handle, object);
077:
078: for (int i = 0, size = this .listeners.size(); i < size; i++) {
079: ((WorkingMemoryEventListener) this .listeners.get(i))
080: .objectInserted(event);
081: }
082: }
083:
084: public void fireObjectUpdated(
085: final PropagationContext propagationContext,
086: final FactHandle handle, final Object oldObject,
087: final Object object,
088: final InternalWorkingMemory workingMemory) {
089: if (this .listeners.isEmpty()) {
090: return;
091: }
092:
093: final ObjectUpdatedEvent event = new ObjectUpdatedEvent(
094: workingMemory, propagationContext, handle, oldObject,
095: object);
096:
097: for (int i = 0, size = this .listeners.size(); i < size; i++) {
098: ((WorkingMemoryEventListener) this .listeners.get(i))
099: .objectUpdated(event);
100: }
101: }
102:
103: public void fireObjectRetracted(
104: final PropagationContext propagationContext,
105: final FactHandle handle, final Object oldObject,
106: final InternalWorkingMemory workingMemory) {
107: if (this .listeners.isEmpty()) {
108: return;
109: }
110:
111: final ObjectRetractedEvent event = new ObjectRetractedEvent(
112: workingMemory, propagationContext, handle, oldObject);
113:
114: for (int i = 0, size = this .listeners.size(); i < size; i++) {
115: ((WorkingMemoryEventListener) this.listeners.get(i))
116: .objectRetracted(event);
117: }
118: }
119:
120: }
|