C:\Java WorkShop\Lexx\src\lexx\trie\MultiTrie.java

1    package lexx.trie; 
2     
3    import java.util.ArrayList; 
4    import java.util.List; 
5    import java.util.Vector; 
6     
7    /** 
8     * <p>MultiTrie takes many Tries and emaulates it as one big Trie</p> 
9     * <p>This can save also of memory as you don't need to copy the Tries into 
10    * single Trie 
11    * <p>Copyright (c) 2002-2003</p> 
12    * @since 18/02/2003 
13    * @author Mohammed Imran 
14    * @version 1.0 
15    */ 
16    
17   public final class MultiTrie implements java.io.Serializable, Cloneable, Trie 
18   { 
19     private final Trie[] tries; 
20     private static final org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(MultiTrie.class); 
21    
22     public MultiTrie(Trie[] trieNodes) 
23     { 
24       tries = trieNodes; 
25     } 
26    
27     public Object clone() 
28     { 
29       if(tries == null) 
30       { 
31         return new MultiTrie( (Trie[])null); 
32       } 
33    
34       Trie[] newTries = new Trie[tries.length]; 
35    
36       for(int i = 0; i < tries.length; i++) 
37       { 
38         newTries[i] = (Trie) tries[i].clone(); 
39       } 
40    
41       return new MultiTrie(newTries); 
42     } 
43    
44     Trie[] getTries() 
45     { 
46       return tries; 
47     } 
48    
49     Trie[] getCharMapBase() 
50     { 
51       Vector l = new Vector(); 
52    
53       for(int i = 0; i < tries.length; i++) 
54       { 
55         Trie parent = tries[i]; 
56         if(parent instanceof TrieNode || parent instanceof RootNode) 
57         { 
58           TrieNode p = (TrieNode) parent; 
59           lexx.utils.Utilities.copyList(p.childNodes.toArray(), l); 
60         } 
61         if(parent instanceof MultiTrie) 
62         { 
63           MultiTrie p = (MultiTrie) parent; 
64           lexx.utils.Utilities.copyList(p.getCharMapBase(), l); 
65         } 
66       } 
67    
68       Trie[] result = new Trie[l.size()]; 
69       for(int i = 0; i < l.size(); i++) 
70       { 
71         result[i] = (Trie) l.get(i); 
72       } 
73       return result; 
74     } 
75    
76     public MultiTrie(List trieNodes) 
77     { 
78       Trie[] nodes = new Trie[trieNodes.size()]; 
79       for(int i = 0; i < trieNodes.size(); i++) 
80       { 
81         nodes[i] = (Trie) trieNodes.get(i); 
82       } 
83       tries = nodes; 
84     } 
85    
86     public final Trie getNode(String key) 
87     { 
88       ArrayList list = new ArrayList(); 
89       try 
90       { 
91         for(int i = 0; i < tries.length; i++) 
92         { 
93           if(tries[i].getNode(key) != null) 
94           { 
95             list.add(tries[i].getNode(key)); 
96           } 
97         } 
98       } 
99       catch(Exception ex) 
100      { 
101        log.fatal(ex, ex); 
102      } 
103   
104      if(list.size() == 1) 
105      { 
106        return(Trie) list.get(0); 
107      } 
108      if(list.size() > 1) 
109      { 
110        return new MultiTrie(list); 
111      } 
112      else 
113      { 
114        return null; 
115      } 
116    } 
117   
118    public final Object getObject() 
119    { 
120      return null; 
121    } 
122   
123    public final boolean isLeaf() 
124    { 
125      return tries.length > 0; 
126    } 
127   
128    public final boolean isSingleLeaf() 
129    { 
130      return tries.length > 0; 
131    } 
132   
133    public final Vector getAllNodes() 
134    { 
135      Vector l = new Vector(); 
136      for(int i = 0; i < tries.length; i++) 
137      { 
138        List tmp = tries[i].getAllNodes(); 
139        for(int x = 0; x < tmp.size(); x++) 
140        { 
141          l.add(tmp.get(x)); 
142        } 
143      } 
144      return l; 
145    } 
146   
147    public final List getCurrentObjects() 
148    { 
149      Vector l = new Vector(); 
150      for(int i = 0; i < tries.length; i++) 
151      { 
152        List tmp = tries[i].getCurrentObjects(); 
153        for(int x = 0; x < tmp.size(); x++) 
154        { 
155          l.add(tmp.get(x)); 
156        } 
157      } 
158      return l; 
159    } 
160  }