1 package com.sri.emo.dbobj.model_tree; 2 3 import java.io.Serializable; 4 5 /*** 6 * Object represents a recursive/truncation/completion status inside the model. If it is recursive 7 * you construt a ModelTruncationStatus() object pointing to the recursive 8 * node. Otherwise you use either COMPLETE_NODE or TRUNCATED_NODE to set 9 * the completion status. 10 * 11 * @author Michael Rimov 12 */ 13 final public class NodeCompletionStatus implements Serializable { 14 15 /*** 16 * 17 */ 18 private static final long serialVersionUID = 1L; 19 20 /*** 21 * Status message indicating a recursive node. 22 */ 23 private static final String RECURSIVE_MESSAGE = "Recursive Node"; 24 25 /*** 26 * Status message indicating a complete node. 27 */ 28 private static final String COMPLETE_MESSAGE = "Complete Node"; 29 30 /*** 31 * Status message indicating a truncated node. 32 */ 33 private static final String TRUNCATED_MESSAGE = "Truncated"; 34 35 36 /*** 37 * Status message indicating trucation is not defined for this node. 38 */ 39 private static final String UNDEFINED_MESSAGE = "Undefined"; 40 41 /*** 42 * Current status string. 43 */ 44 private final String recursionStatusString; 45 46 /*** 47 * Recursive node (if any) 48 */ 49 private final ModelNode recursiveNode; 50 51 /*** 52 * Instance representing a complete node. 53 */ 54 public static final NodeCompletionStatus COMPLETE_NODE = new NodeCompletionStatus(COMPLETE_MESSAGE); 55 56 /*** 57 * Instance representing a truncated node. 58 */ 59 public static final NodeCompletionStatus TRUNCATED_NODE = new NodeCompletionStatus(TRUNCATED_MESSAGE); 60 61 62 /*** 63 * Instance representing an undefined status. This is often the case because many nodes 64 * in the tree have no concept of truncation depending on the implementation of the factory. For 65 * clarification, in {@link DefaultModelFactory} only Emo 'Nodes' have the concept of 66 * truncation -- actual parts of a node are never truncated 67 */ 68 public static final NodeCompletionStatus UNDEFINED_STATUS = new NodeCompletionStatus(UNDEFINED_MESSAGE); 69 70 71 /*** 72 * Private constructor that takes a status string. 73 * 74 * @param status String 75 */ 76 private NodeCompletionStatus(String status) { 77 super(); 78 assert status != null; 79 recursionStatusString = status; 80 recursiveNode = null; 81 } 82 83 /*** 84 * Constructs a recursive model status node. 85 * 86 * @param recursiveModelNode ModelNode 87 */ 88 public NodeCompletionStatus(ModelNode recursiveModelNode) { 89 assert recursiveModelNode != null; 90 91 recursionStatusString = RECURSIVE_MESSAGE; 92 recursiveNode = recursiveModelNode; 93 } 94 95 96 /*** 97 * Retrieves the recursive node that is located elsewhere inside the 98 * model tree. 99 * 100 * @return ModelNode 101 */ 102 public ModelNode getRecursiveNode() { 103 if (!isRecursive()) { 104 throw new IllegalStateException("Not a recursive node"); 105 } 106 107 return recursiveNode; 108 } 109 110 /*** 111 * Returns true if this is a recursive node. 112 * 113 * @return boolean true if recursive. 114 */ 115 public boolean isRecursive() { 116 return (this != COMPLETE_NODE && this != TRUNCATED_NODE); 117 } 118 119 /*** 120 * Returns true if this is a truncated node. 121 * 122 * @return boolean true if truncated. 123 */ 124 public boolean isTruncated() { 125 return (this != TRUNCATED_NODE); 126 } 127 128 /*** 129 * Model Recursion status objects are equal if they are the same status type 130 * and if they are, they point to the same recursive node. 131 * <p>{@inheritDoc}</p> 132 * 133 * @param obj the reference object with which to compare. 134 * @return <code>true</code> if this object is the same as the obj 135 * argument; <code>false</code> otherwise. 136 */ 137 public boolean equals(Object obj) { 138 if (!(NodeCompletionStatus.class.equals(obj.getClass()))) { 139 return false; 140 } 141 142 NodeCompletionStatus other = (NodeCompletionStatus) obj; 143 if (other.recursionStatusString.equals(this.recursionStatusString)) { 144 if (recursiveNode == null && other.recursiveNode == null) { 145 return true; 146 } else if (recursiveNode == null && other.recursiveNode != null) { 147 return false; 148 } else if (other.recursiveNode == null && recursiveNode != null) { 149 return false; 150 } else { 151 return recursiveNode.equals(other.recursiveNode); 152 } 153 } else { 154 return false; 155 156 } 157 } 158 159 /*** 160 * {@inheritDoc} 161 * 162 * @return a hash code value for this object. 163 */ 164 public int hashCode() { 165 if (this == COMPLETE_NODE || this == TRUNCATED_NODE) { 166 return recursionStatusString.hashCode(); 167 } else { 168 return recursiveNode.hashCode(); 169 } 170 } 171 172 /*** 173 * {@inheritDoc} 174 * 175 * @return a string representation of the object. 176 */ 177 public String toString() { 178 return "[Node Completion Status: " + recursionStatusString + "]"; 179 } 180 181 182 }