/jbpm-flow/src/main/java/org/jbpm/workflow/core/impl/ConnectionImpl.java

https://github.com/mariofusco/jbpm · Java · 151 lines · 95 code · 23 blank · 33 comment · 10 complexity · cb97909f764d84781a997b4051f89cc7 MD5 · raw file

  1. /*
  2. * Copyright 2005 JBoss Inc
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.jbpm.workflow.core.impl;
  17. import java.io.Serializable;
  18. import java.util.HashMap;
  19. import java.util.List;
  20. import java.util.Map;
  21. import org.kie.api.definition.process.Node;
  22. import org.jbpm.workflow.core.Connection;
  23. /**
  24. * Default implementation of a connection.
  25. *
  26. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  27. */
  28. public class ConnectionImpl implements Connection, Serializable {
  29. private static final long serialVersionUID = 510l;
  30. private Node from;
  31. private Node to;
  32. private String fromType;
  33. private String toType;
  34. private Map<String, Object> metaData = new HashMap<String, Object>();
  35. public ConnectionImpl() {
  36. }
  37. /**
  38. * This constructor calls {@link #connect()} itself! That means
  39. * that simply creating the object also adds it to the appropriate
  40. * {@link List} fields in other objects.
  41. * </p>
  42. * Creates a new connection, given a from node, a to node
  43. * and a type.
  44. *
  45. * @param from The from node
  46. * @param fromType The node type
  47. * @param to The to node
  48. * @param toType The connection type
  49. */
  50. public ConnectionImpl(final Node from, final String fromType,
  51. final Node to, final String toType) {
  52. if (from == null) {
  53. throw new IllegalArgumentException("From node is null!");
  54. }
  55. if (fromType == null) {
  56. throw new IllegalArgumentException("From type is null!");
  57. }
  58. if (to == null) {
  59. throw new IllegalArgumentException("To node is null!");
  60. }
  61. if (toType == null) {
  62. throw new IllegalArgumentException("To type is null!");
  63. }
  64. this.from = from;
  65. this.fromType = fromType;
  66. this.to = to;
  67. this.toType = toType;
  68. connect();
  69. }
  70. public void connect() {
  71. ((org.jbpm.workflow.core.Node) this.from).addOutgoingConnection(fromType, this);
  72. ((org.jbpm.workflow.core.Node) this.to).addIncomingConnection(toType, this);
  73. }
  74. public synchronized void terminate() {
  75. ((org.jbpm.workflow.core.Node) this.from).removeOutgoingConnection(fromType, this);
  76. ((org.jbpm.workflow.core.Node) this.to).removeIncomingConnection(toType, this);
  77. this.from = null;
  78. this.fromType = null;
  79. this.to = null;
  80. this.toType = null;
  81. }
  82. public Node getFrom() {
  83. return this.from;
  84. }
  85. public Node getTo() {
  86. return this.to;
  87. }
  88. public String getFromType() {
  89. return this.fromType;
  90. }
  91. public String getToType() {
  92. return this.toType;
  93. }
  94. public void setFrom(Node from) {
  95. this.from = from;
  96. }
  97. public void setTo(Node to) {
  98. this.to = to;
  99. }
  100. public void setFromType(String fromType) {
  101. this.fromType = fromType;
  102. }
  103. public void setToType(String toType) {
  104. this.toType = toType;
  105. }
  106. public Map<String, Object> getMetaData() {
  107. return this.metaData;
  108. }
  109. public void setMetaData(String name, Object value) {
  110. this.metaData.put(name, value);
  111. }
  112. public Object getMetaData(String name) {
  113. return this.metaData.get(name);
  114. }
  115. public String toString() {
  116. final StringBuilder sb = new StringBuilder("Connection ");
  117. sb.append(getFrom() == null ? "null" : getFrom().getName());
  118. sb.append(" [type=");
  119. sb.append(getFromType());
  120. sb.append("]");
  121. sb.append(" - ");
  122. sb.append(getTo() == null ? "null" : getTo().getName());
  123. sb.append(" [type=");
  124. sb.append(getToType());
  125. sb.append("]");
  126. return sb.toString();
  127. }
  128. }