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:
021: import org.drools.ruleflow.core.Node;
022:
023: /**
024: * Default implementation of a connection.
025: *
026: * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
027: */
028: public class ConnectionImpl implements
029: org.drools.ruleflow.core.Connection, Serializable {
030:
031: private static final long serialVersionUID = 400L;
032:
033: private int type;
034: private NodeImpl from;
035: private NodeImpl to;
036:
037: private ConnectionImpl() {
038: }
039:
040: /**
041: * Creates a new connection, given a from node, a to node
042: * and a type.
043: *
044: * @param from The from node
045: * @param to The to node
046: * @param type The connection type
047: */
048: public ConnectionImpl(final Node from, final Node to, final int type) {
049: if (from == null) {
050: throw new IllegalArgumentException("From node is null!");
051: }
052: if (to == null) {
053: throw new IllegalArgumentException("To node is null!");
054: }
055: if (from.equals(to)) {
056: throw new IllegalArgumentException(
057: "To and from nodes are the same!");
058: }
059: this .from = (NodeImpl) from;
060: this .to = (NodeImpl) to;
061: this .type = type;
062: this .from.addOutgoingConnection(this );
063: this .to.addIncomingConnection(this );
064: }
065:
066: public synchronized void terminate() {
067: this .from.removeOutgoingConnection(this );
068: this .to.removeIncomingConnection(this );
069: this .type = 0;
070: this .from = null;
071: this .to = null;
072: }
073:
074: public Node getFrom() {
075: return this .from;
076: }
077:
078: public Node getTo() {
079: return this .to;
080: }
081:
082: public int getType() {
083: return this .type;
084: }
085:
086: // public boolean equals(final Object object) {
087: // if ( object instanceof Connection ) {
088: // final Connection connection = (Connection) object;
089: // return this.type == connection.getType() && getFrom().equals( connection.getFrom() ) && getTo().equals( connection.getTo() );
090: // }
091: // return false;
092: // }
093:
094: // public int hashCode() {
095: // return (getFrom() == null ? 0 : getFrom().hashCode()) + 7 * (getTo() == null ? 0 : getTo().hashCode()) + 19 * getType();
096: // }
097:
098: public String toString() {
099: final StringBuffer sb = new StringBuffer("Connection ");
100: sb.append(getFrom());
101: sb.append(" - ");
102: sb.append(getTo());
103: sb.append(" [type=");
104: sb.append(getType());
105: sb.append("]");
106: return sb.toString();
107: }
108: }
|