1 /* 2 * Copyright (c) 2003-2008, by Henrik Arro and Contributors 3 * 4 * This file is part of JSeq, a tool to automatically create 5 * sequence diagrams by tracing program execution. 6 * 7 * See <http://jseq.sourceforge.net> for more information. 8 * 9 * JSeq is free software: you can redistribute it and/or modify 10 * it under the terms of the GNU Lesser General Public License as 11 * published by the Free Software Foundation, either version 3 of 12 * the License, or (at your option) any later version. 13 * 14 * JSeq is distributed in the hope that it will be useful, 15 * but WITHOUT ANY WARRANTY; without even the implied warranty of 16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 * GNU Lesser General Public License for more details. 18 * 19 * You should have received a copy of the GNU Lesser General Public License 20 * along with JSeq. If not, see <http://www.gnu.org/licenses/>. 21 */ 22 23 package th.co.edge.jseq; 24 25 import java.util.Collections; 26 import java.util.HashMap; 27 import java.util.LinkedList; 28 import java.util.List; 29 import java.util.Map; 30 31 /** 32 * A <code>MockObjectMap</code> is a mapping from names to 33 * <code>MockObject</code>s, with utility methods to make it easy to 34 * translate from <code>Activation</code>s to <code>MockObject</code>s, 35 * with automatic handling of column numbers. 36 */ 37 public class MockObjectMap { 38 private Map<String, MockObject> objectMap = new HashMap<String, MockObject>(); 39 private int nextColumn = 0; 40 41 /** 42 * Ensures that all <code>MockObject</code>s in another 43 * <code>MockObjectMap</code> is also in this map. 44 * 45 * @param otherObjectMap 46 * the <code>MockObjectMap</code> whose <code>MockObject</code>s 47 * to add to this map if necessary 48 */ 49 public void add(MockObjectMap otherObjectMap) { 50 for (MockObject object : otherObjectMap.listView()) { 51 getInstance(object.getName()); 52 } 53 } 54 55 /** 56 * Returns a new <code>MockObjectMap</code> where the method calls 57 * represented by the given <code>Activation</code> and its nested 58 * activations have been added as <code>MockObject</code>s to this map, 59 * with the activation method call first, and the nested method calls in the 60 * order they were added to the activation. 61 * 62 * @param activation 63 * the <code>Activation</code> whose method call and nested 64 * method calls to add to this map 65 * 66 * @return a new <code>MockObjectMap</code> containing all method calls 67 * from <code>activation</code> 68 */ 69 public static MockObjectMap addAll(Activation activation) { 70 MockObjectMap objectMap = new MockObjectMap(); 71 addAll(activation, objectMap); 72 return objectMap; 73 } 74 75 private static void addAll(Activation activation, MockObjectMap objectMap) { 76 objectMap.getInstance(activation.getClassName()); 77 for (Activation nestedActivation : activation.getNestedActivations()) { 78 addAll(nestedActivation, objectMap); 79 } 80 } 81 82 /** 83 * Returns the <code>MockObject</code> associated with the given name. If 84 * there is none, a new <code>MockObject</code> is created, using the 85 * given name and the next available column number, and this object is added 86 * to the map. 87 * 88 * @param name 89 * the name of the <code>MockObject</code> to look up 90 * 91 * @return the <code>MockObject</code> associated with <code>name</code>, 92 * or a newly created <code>MockObject</code> (this method never 93 * returns <code>null</code> 94 */ 95 private MockObject getInstance(String name) { 96 MockObject object = objectMap.get(name); 97 if (object == null) { 98 object = new MockObject(name, nextColumn++); 99 objectMap.put(name, object); 100 } 101 return object; 102 } 103 104 /** 105 * Returns the <code>MockObject</code> associated with the given name, or 106 * <code>null</code> if there is none. 107 * 108 * @param name 109 * the name of the <code>MockObject</code> to look up 110 * 111 * @return the <code>MockObject</code> associated with <code>name</code>, 112 * or <code>null</code> if there is none 113 */ 114 public MockObject get(String name) { 115 return objectMap.get(name); 116 } 117 118 /** 119 * Returns a list of all <code>MockObject</code>s in this map, ordered by 120 * column number (and name if the same column number occurs several times, 121 * which it normally should not do). 122 * 123 * @return a list of all <code>MockObject</code>s in this map 124 */ 125 public List<MockObject> listView() { 126 List<MockObject> values = new LinkedList<MockObject>(objectMap.values()); 127 Collections.sort(values); 128 return values; 129 } 130 131 /** 132 * Returns a string representation of this <code>MockObjectMap</code>, 133 * mainly useful for logging. 134 * 135 * @return a string representation of this <code>MockObjectMap</code> 136 */ 137 @Override 138 public String toString() { 139 return listView().toString(); 140 } 141 }