source: trunk/EventBenchCore/src/de/ugoe/cs/eventbench/models/TrieNode.java @ 102

Last change on this file since 102 was 102, checked in by sherbold, 13 years ago
  • replaced java.util.Set and Java.util.List with java.util.Collection where possible
File size: 3.7 KB
Line 
1package de.ugoe.cs.eventbench.models;
2
3import java.io.Serializable;
4import java.security.InvalidParameterException;
5import java.util.Collection;
6import java.util.LinkedList;
7import java.util.List;
8
9import de.ugoe.cs.eventbench.models.Trie.Edge;
10import de.ugoe.cs.eventbench.models.Trie.TrieVertex;
11import de.ugoe.cs.util.StringTools;
12import edu.uci.ics.jung.graph.DelegateTree;
13
14
15class TrieNode<T> implements Serializable {
16       
17        /**
18         * Id for object serialization.
19         */
20        private static final long serialVersionUID = 1L;
21       
22        private int count;
23        private final T symbol;
24       
25        private Collection<TrieNode<T>> children;
26       
27        TrieNode() {
28                this.symbol = null;
29                count = 0;
30                children = new LinkedList<TrieNode<T>>();
31        }
32       
33        public TrieNode(T symbol) {
34                if( symbol==null ) {
35                        throw new InvalidParameterException("symbol must not be null. null is reserved for root node!");
36                }
37                this.symbol = symbol;
38                count = 0;
39                children = new LinkedList<TrieNode<T>>();
40        }
41
42        public void add(List<T> subsequence) {
43                if( !subsequence.isEmpty() ) {
44                        if( !symbol.equals(subsequence.get(0)) ) { // should be guaranteed by the recursion/TrieRoot!
45                                throw new AssertionError("Invalid trie operation!");
46                        }
47                        count++;
48                        subsequence.remove(0);
49                        if( !subsequence.isEmpty() ) {
50                                T nextSymbol = subsequence.get(0);
51                                getChildCreate(nextSymbol).add(subsequence);
52                        }
53                }
54        }
55       
56        public T getSymbol() {
57                return symbol;
58        }
59       
60        public int getCount() {
61                return count;
62        }
63       
64        protected TrieNode<T>  getChildCreate(T symbol) {
65                TrieNode<T> node = getChild(symbol);
66                if( node==null ) {
67                        node = new TrieNode<T>(symbol);
68                        children.add(node);
69                }
70                return node;
71        }
72       
73        protected TrieNode<T> getChild(T symbol) {
74                for( TrieNode<T> child : children ) {
75                        if( child.getSymbol().equals(symbol) ) {
76                                return child;
77                        }
78                }
79                return null;
80        }
81       
82
83       
84        public TrieNode<T> find(List<T> subsequence) {
85                TrieNode<T> result = null;
86                if( subsequence.isEmpty() ) {
87                        result = this;
88                } else {
89                        TrieNode<T> node = getChild(subsequence.get(0));
90                        if( node!=null ) {
91                                subsequence.remove(0);
92                                result = node.find(subsequence);
93                        }
94                }
95                return result;
96        }
97       
98        // returns all symbols that follow this node
99        public Collection<T> getFollowingSymbols() {
100                Collection<T> followingSymbols = new LinkedList<T>();
101                for( TrieNode<T> child : children ) {
102                        followingSymbols.add(child.getSymbol());
103                }
104                return followingSymbols;
105        }
106       
107        @Override
108        public String toString() {
109                String str = symbol.toString()+" #"+count;
110                if( !children.isEmpty() ) {
111                        str += StringTools.ENDLINE + children.toString();
112                }
113                return str;
114        }
115
116        public void getGraph(TrieVertex parent, DelegateTree<TrieVertex, Edge> graph) {
117                TrieVertex currentVertex;
118                if( symbol==null ){
119                        currentVertex = new TrieVertex("root");
120                        graph.addVertex(currentVertex);
121                } else {
122                        currentVertex = new TrieVertex(getSymbol().toString()+"#"+getCount());
123                        graph.addChild( new Edge() , parent, currentVertex );
124                }
125                for( TrieNode<T> node : children ) {
126                        node.getGraph(currentVertex, graph);
127                }               
128        }
129       
130        void appendDotRepresentation(StringBuilder stringBuilder) {
131                String thisSaneId;
132                if( symbol==null ) {
133                        thisSaneId = "root";
134                } else {
135                        thisSaneId = symbol.toString().replace("\"", "\\\"").replaceAll("[\r\n]","")+"#"+count;
136                }
137                stringBuilder.append(" " + hashCode() + " [label=\""+thisSaneId+"\"];" + StringTools.ENDLINE);
138                for( TrieNode<T> childNode : children ) {
139                        stringBuilder.append(" "+hashCode()+" -> " + childNode.hashCode() + ";" + StringTools.ENDLINE);
140                }
141                for( TrieNode<T> childNode : children ) {
142                        childNode.appendDotRepresentation(stringBuilder);
143                }
144        }
145}
Note: See TracBrowser for help on using the repository browser.