1
2
3
4
5
6
7
8
9
10 package com.sri.emo.commandline.defaults;
11
12 import java.util.Set;
13 import java.util.TreeSet;
14
15 import com.jcorporate.expresso.core.db.DBException;
16 import com.jcorporate.expresso.core.dbobj.DBObject;
17 import com.sri.emo.commandline.NodeTracker;
18
19 /***
20 * Stores all ids into an internal TreeMap.
21 * @author Michael Rimov
22 *
23 */
24 public class InMemoryNodeTracker implements NodeTracker {
25
26 private final Set visitedNodes;
27
28
29 /***
30 *
31 */
32 public InMemoryNodeTracker() {
33 visitedNodes = new TreeSet();
34 }
35
36
37
38
39 public synchronized boolean isObjectVisited(DBObject dbobj) throws DBException {
40 TreeKey testKey = new TreeKey(dbobj);
41 if (visitedNodes.contains(testKey)) {
42 return true;
43 }
44 return false;
45 }
46
47
48
49
50
51
52 public synchronized void markObjectAsVisited(DBObject dbobj)throws DBException {
53 TreeKey testKey = new TreeKey(dbobj);
54 visitedNodes.add(testKey);
55
56 }
57
58
59 private static class TreeKey implements Comparable {
60
61 private final String dbobjClassName;
62
63 private final String dbobjKey;
64
65 public TreeKey(final DBObject target) {
66 this( target.getClass(), target.getKey());
67 }
68
69 public TreeKey(Class dbobjectClass, String dbobjectKey) {
70 this.dbobjClassName = dbobjectClass.getName();
71 this.dbobjKey = dbobjectKey;
72 assert dbobjectClass != null;
73 assert dbobjectKey != null;
74 }
75
76 public int compareTo(Object arg) {
77 assert arg != null;
78
79 TreeKey other = (TreeKey)arg;
80 int result = dbobjClassName.compareTo(other.dbobjClassName);
81 if (result == 0) {
82 return dbobjKey.compareTo(other.dbobjKey);
83 }
84
85 return result;
86 }
87
88 }
89
90 }