KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > avalon > excalibur > util > Circuit


1 /*
2  * Copyright (C) The Apache Software Foundation. All rights reserved.
3  *
4  * This software is published under the terms of the Apache Software License
5  * version 1.1, a copy of which has been included with this distribution in
6  * the LICENSE.txt file.
7  */

8 package org.apache.avalon.excalibur.util;
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Iterator JavaDoc;
13 import java.util.List JavaDoc;
14 import java.util.Map JavaDoc;
15
16 /**
17  * Circuit class used to map trees of information
18  *
19  * @version 0.0.20, 04/07/1998
20  * @author Federico Barbieri <fede@apache.org>
21  * @author Stefano Mazzocchi <mazzocch@mbox.systemy.it>
22  */

23 public class Circuit
24 {
25     protected Map JavaDoc m_map;
26
27     public Circuit()
28     {
29         m_map = new HashMap JavaDoc();
30     }
31
32     public void addNode( final String JavaDoc name )
33     {
34         if( null == m_map.get( name ) )
35         {
36             m_map.put( name, new Node( name ) );
37         }
38     }
39
40     public void removeNode( final String JavaDoc name )
41     {
42         String JavaDoc tmp = null;
43         Iterator JavaDoc e = m_map.keySet().iterator();
44
45         while( e.hasNext() )
46         {
47             tmp = (String JavaDoc)e.next();
48
49             if( !tmp.equals( name ) )
50             {
51                 try { unlink( tmp, name ); }
52                 catch( final CircuitException ce) {}
53                 try { unlink( name, tmp ); }
54                 catch( final CircuitException ce ) {}
55             }
56
57         }
58
59         m_map.remove( name );
60     }
61
62     public void link( final String JavaDoc parent, final String JavaDoc child )
63         throws CircuitException
64     {
65         Node tempNode = null;
66         final Node pnode = (Node)m_map.get( parent );
67         final Node cnode = (Node)m_map.get( child );
68         if( null == pnode )
69         {
70             throw new CircuitException( "Unknown node " + parent );
71         }
72         else if( null == cnode )
73         {
74             throw new CircuitException( "Unknown node " + child );
75         }
76         else if( pnode.isChildOf( cnode ) )
77         {
78             throw new CircuitException( "Loop! Node " + parent +
79                                         " is already child of node " + child );
80         }
81         else
82         {
83             final Iterator JavaDoc e = m_map.values().iterator();
84
85             while( e.hasNext() )
86             {
87                 tempNode = (Node)e.next();
88                 if( tempNode.isChildOf( cnode ) )
89                 {
90                     tempNode.m_parents.addAll( pnode.m_parents );
91                 }
92             }
93         }
94     }
95
96     public void unlink( final String JavaDoc parent, final String JavaDoc child )
97         throws CircuitException
98     {
99         final Node cnode = (Node)m_map.get( child );
100         final Node pnode = (Node)m_map.get( parent );
101
102         if( cnode.m_parents.contains( pnode ) )
103         {
104             Node tempNode = null;
105             final Iterator JavaDoc e = m_map.values().iterator();
106
107             while( e.hasNext() )
108             {
109                 tempNode = (Node)e.next();
110
111                 if( tempNode.m_parents.contains( cnode ) )
112                 {
113                     tempNode.m_parents.removeAll( pnode.m_parents );
114                 }
115             }
116         }
117         else
118         {
119             throw new CircuitException( "Node " + parent + " is not parent of node " + child );
120         }
121     }
122
123     public List JavaDoc getAncestors()
124     {
125         final List JavaDoc ancestors = new ArrayList JavaDoc(5);
126         String JavaDoc name = null;
127         Node tempNode = null;
128         final Iterator JavaDoc e = m_map.keySet().iterator();
129
130         while( e.hasNext() )
131         {
132             name = (String JavaDoc)e.next();
133             tempNode = (Node)m_map.get( name );
134
135             if( 1 == tempNode.m_parents.size() )
136             {
137                 ancestors.add( name );
138             }
139         }
140
141         return ancestors;
142     }
143
144     public String JavaDoc getAncestor()
145     {
146         String JavaDoc name = null;
147         Node tempNode = null;
148         final Iterator JavaDoc e = m_map.keySet().iterator();
149
150         while( e.hasNext() )
151         {
152             name = (String JavaDoc)e.next();
153             tempNode = (Node)m_map.get( name );
154
155             if( 1 == tempNode.m_parents.size() )
156             {
157                 return name;
158             }
159         }
160
161         return null;
162     }
163
164     public boolean isEmpty()
165     {
166         return m_map.isEmpty();
167     }
168
169     protected final class Node
170     {
171         protected final List JavaDoc m_parents;
172         protected final String JavaDoc m_name;
173
174         protected Node( final String JavaDoc name )
175         {
176             m_parents = new ArrayList JavaDoc( 5 );
177             m_parents.add( this );
178             m_name = name;
179         }
180
181         protected final boolean isChildOf( final Node parent )
182         {
183             return m_parents.contains( parent );
184         }
185
186         public final String JavaDoc toString()
187         {
188             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
189             Iterator JavaDoc e = m_parents.iterator();
190             buffer.append( m_name + "[" );
191
192             while( e.hasNext() )
193             {
194                 buffer.append(((Node) e.next()).m_name + " ");
195             }
196
197             buffer.append("]");
198             return buffer.toString();
199         }
200     }
201
202     public final class CircuitException
203         extends RuntimeException JavaDoc
204     {
205         public CircuitException()
206         {
207         }
208
209         public CircuitException( final String JavaDoc message )
210         {
211             super( message );
212         }
213     }
214
215     public String JavaDoc toString()
216     {
217         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
218         String JavaDoc name = null;
219         Node tempNode = null;
220         final Iterator JavaDoc e = m_map.keySet().iterator();
221
222         while( e.hasNext() )
223         {
224             name = (String JavaDoc)e.next();
225             tempNode = (Node)m_map.get( name );
226             buffer.append( name ).append( "(" )
227                   .append( tempNode.m_parents.size() - 1 )
228                   .append( ") " );
229         }
230
231         return buffer.toString();
232     }
233 }
234
235
Popular Tags