001: package org.drools.ruleflow.core.impl;
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.ruleflow.core.Connection;
025: import org.drools.ruleflow.core.Node;
026:
027: /**
028: * Default implementation of a node.
029: *
030: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
031: */
032: public abstract class NodeImpl implements Node, Serializable {
033:
034: protected static final NodeImpl[] EMPTY_NODE_ARRAY = new NodeImpl[0];
035:
036: private long id;
037: private String name;
038: private List incomingConnections;
039: private List outgoingConnections;
040:
041: public NodeImpl() {
042: this .id = -1;
043: this .incomingConnections = new ArrayList();
044: this .outgoingConnections = new ArrayList();
045: }
046:
047: public long getId() {
048: return this .id;
049: }
050:
051: public void setId(final long id) {
052: this .id = id;
053: }
054:
055: public String getName() {
056: return this .name;
057: }
058:
059: public void setName(final String name) {
060: this .name = name;
061: }
062:
063: public List getIncomingConnections() {
064: return Collections.unmodifiableList(this .incomingConnections);
065: }
066:
067: public List getOutgoingConnections() {
068: return Collections.unmodifiableList(this .outgoingConnections);
069: }
070:
071: protected void addIncomingConnection(final Connection connection) {
072: validateAddIncomingConnection(connection);
073: this .incomingConnections.add(connection);
074: }
075:
076: /**
077: * This method validates whether the given connection can be added. If the
078: * connection cannot be added, an IllegalArgumentException is thrown.
079: * <p>
080: *
081: * @param connection
082: * the incoming connection to be added
083: * @throws IllegalArgumentException
084: * is thrown if the connection is null, or if a connection is
085: * added twice. If subclasses want to change the rules for
086: * adding incoming connections the
087: * <code>validateAddIncomingConnection(IConnection connection)</code>
088: * should be overridden.
089: */
090: protected void validateAddIncomingConnection(
091: final Connection connection) {
092: if (connection == null) {
093: throw new IllegalArgumentException(
094: "Connection cannot be null");
095: }
096: if (this .incomingConnections.contains(connection)) {
097: throw new IllegalArgumentException(
098: "Connection is already added");
099: }
100: }
101:
102: protected void addOutgoingConnection(final Connection connection) {
103: validateAddOutgoingConnection(connection);
104: this .outgoingConnections.add(connection);
105: }
106:
107: /**
108: * This method validates whether the given connection can be added. If the
109: * connection cannot be added, an IllegalArgumentException is thrown.
110: * <p>
111: *
112: * @param connection
113: * the outgoin connection to be added
114: * @throws IllegalArgumentException
115: * is thrown if the connection is null, or if a connection is
116: * added twice. If subclasses want to change the rules for
117: * adding outgoing connections the
118: * <code>validateAddIncomingConnection(IConnection connection)</code>
119: * should be overridden.
120: */
121: protected void validateAddOutgoingConnection(
122: final Connection connection) {
123: if (connection == null) {
124: throw new IllegalArgumentException(
125: "Connection cannot be null");
126: }
127: if (this .outgoingConnections.contains(connection)) {
128: throw new IllegalArgumentException(
129: "Connection is already added");
130: }
131: }
132:
133: protected void removeIncomingConnection(final Connection connection) {
134: validateRemoveIncomingConnection(connection);
135: this .incomingConnections.remove(connection);
136: }
137:
138: /**
139: * This method validates whether the given connection can be removed
140: * <p>
141: *
142: * @param connection
143: * the incoming connection
144: * @throws IllegalArgumentException
145: * is thrown if connectin is null, or unknown. If subclasses
146: * want to change the rules for removing incoming connections
147: * the
148: * <code>validateRemoveIncomingConnection(IConnection connection)</code>
149: * should be overridden.
150: */
151: protected void validateRemoveIncomingConnection(
152: final Connection connection) {
153: if (connection == null) {
154: throw new IllegalArgumentException("Connection is null");
155: }
156: if (!this .incomingConnections.contains(connection)) {
157: throw new IllegalArgumentException("Given connection <"
158: + connection
159: + "> is not part of the incoming connections");
160: }
161: }
162:
163: protected void removeOutgoingConnection(final Connection connection) {
164: validateRemoveOutgoingConnection(connection);
165: this .outgoingConnections.remove(connection);
166: }
167:
168: /**
169: * This method validates whether the given connection can be removed
170: * <p>
171: *
172: * @param connection
173: * the outgoing connection
174: * @throws IllegalArgumentException
175: * is thrown if connectin is null, or unknown. If subclasses
176: * want to change the rules for removing outgoing connections
177: * the
178: * <code>validateRemoveOutgoingConnection(IConnection connection)</code>
179: * should be overridden.
180: */
181: protected void validateRemoveOutgoingConnection(
182: final Connection connection) {
183: if (connection == null) {
184: throw new IllegalArgumentException("Connection is null");
185: }
186: if (!this .outgoingConnections.contains(connection)) {
187: throw new IllegalArgumentException("Given connection <"
188: + connection
189: + "> is not part of the outgoing connections");
190: }
191: }
192: }
|