View Javadoc

1   /* ===================================================================
2    * Copyright 2002-06 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
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  	/* (non-Javadoc)
37  	 * @see com.sri.emo.commandline.NodeTracker#isObjectVisited(com.jcorporate.expresso.core.dbobj.DBObject)
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  	/* (non-Javadoc)
50  	 * @see com.sri.emo.commandline.NodeTracker#markObjectAsVisited(com.jcorporate.expresso.core.dbobj.DBObject)
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  }