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.WorkingMemory;
025: import org.drools.common.InternalWorkingMemory;
026: import org.drools.spi.Activation;
027: import org.drools.spi.AgendaGroup;
028:
029: /**
030: * @author <a href="mailto:simon@redhillconsulting.com.au">Simon Harris </a>
031: */
032: public class AgendaEventSupport 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 AgendaEventSupport() {
041: }
042:
043: public void addEventListener(final AgendaEventListener listener) {
044: if (!this .listeners.contains(listener)) {
045: this .listeners.add(listener);
046: }
047: }
048:
049: public void removeEventListener(final AgendaEventListener listener) {
050: this .listeners.remove(listener);
051: }
052:
053: public List getEventListeners() {
054: return Collections.unmodifiableList(this .listeners);
055: }
056:
057: public int size() {
058: return this .listeners.size();
059: }
060:
061: public boolean isEmpty() {
062: return this .listeners.isEmpty();
063: }
064:
065: public void fireActivationCreated(final Activation activation,
066: final WorkingMemory workingMemory) {
067: if (this .listeners.isEmpty()) {
068: return;
069: }
070:
071: final ActivationCreatedEvent event = new ActivationCreatedEvent(
072: activation);
073:
074: for (int i = 0, size = this .listeners.size(); i < size; i++) {
075: ((AgendaEventListener) this .listeners.get(i))
076: .activationCreated(event, workingMemory);
077: }
078: }
079:
080: public void fireActivationCancelled(final Activation activation,
081: final WorkingMemory workingMemory) {
082: if (this .listeners.isEmpty()) {
083: return;
084: }
085:
086: final ActivationCancelledEvent event = new ActivationCancelledEvent(
087: activation);
088:
089: for (int i = 0, size = this .listeners.size(); i < size; i++) {
090: ((AgendaEventListener) this .listeners.get(i))
091: .activationCancelled(event, workingMemory);
092: }
093: }
094:
095: public void fireBeforeActivationFired(final Activation activation,
096: final WorkingMemory workingMemory) {
097: if (this .listeners.isEmpty()) {
098: return;
099: }
100:
101: final BeforeActivationFiredEvent event = new BeforeActivationFiredEvent(
102: activation);
103:
104: for (int i = 0, size = this .listeners.size(); i < size; i++) {
105: ((AgendaEventListener) this .listeners.get(i))
106: .beforeActivationFired(event, workingMemory);
107: }
108: }
109:
110: public void fireAfterActivationFired(final Activation activation,
111: final InternalWorkingMemory workingMemory) {
112: if (this .listeners.isEmpty()) {
113: return;
114: }
115:
116: final AfterActivationFiredEvent event = new AfterActivationFiredEvent(
117: activation);
118:
119: for (int i = 0, size = this .listeners.size(); i < size; i++) {
120: ((AgendaEventListener) this .listeners.get(i))
121: .afterActivationFired(event, workingMemory);
122: }
123: }
124:
125: public void fireAgendaGroupPopped(final AgendaGroup agendaGroup,
126: final InternalWorkingMemory workingMemory) {
127: if (this .listeners.isEmpty()) {
128: return;
129: }
130:
131: final AgendaGroupPoppedEvent event = new AgendaGroupPoppedEvent(
132: agendaGroup);
133:
134: for (int i = 0, size = this .listeners.size(); i < size; i++) {
135: ((AgendaEventListener) this .listeners.get(i))
136: .agendaGroupPopped(event, workingMemory);
137: }
138: }
139:
140: public void fireAgendaGroupPushed(final AgendaGroup agendaGroup,
141: final InternalWorkingMemory workingMemory) {
142: if (this .listeners.isEmpty()) {
143: return;
144: }
145:
146: final AgendaGroupPushedEvent event = new AgendaGroupPushedEvent(
147: agendaGroup);
148:
149: for (int i = 0, size = this .listeners.size(); i < size; i++) {
150: ((AgendaEventListener) this.listeners.get(i))
151: .agendaGroupPushed(event, workingMemory);
152: }
153: }
154: }
|