1 8 package org.apache.avalon.excalibur.util; 9 10 import java.util.ArrayList ; 11 import java.util.HashMap ; 12 import java.util.Iterator ; 13 import java.util.List ; 14 import java.util.Map ; 15 16 23 public class Circuit 24 { 25 protected Map m_map; 26 27 public Circuit() 28 { 29 m_map = new HashMap (); 30 } 31 32 public void addNode( final String 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 name ) 41 { 42 String tmp = null; 43 Iterator e = m_map.keySet().iterator(); 44 45 while( e.hasNext() ) 46 { 47 tmp = (String )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 parent, final String 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 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 parent, final String 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 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 getAncestors() 124 { 125 final List ancestors = new ArrayList (5); 126 String name = null; 127 Node tempNode = null; 128 final Iterator e = m_map.keySet().iterator(); 129 130 while( e.hasNext() ) 131 { 132 name = (String )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 getAncestor() 145 { 146 String name = null; 147 Node tempNode = null; 148 final Iterator e = m_map.keySet().iterator(); 149 150 while( e.hasNext() ) 151 { 152 name = (String )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 m_parents; 172 protected final String m_name; 173 174 protected Node( final String name ) 175 { 176 m_parents = new ArrayList ( 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 toString() 187 { 188 StringBuffer buffer = new StringBuffer (); 189 Iterator 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 204 { 205 public CircuitException() 206 { 207 } 208 209 public CircuitException( final String message ) 210 { 211 super( message ); 212 } 213 } 214 215 public String toString() 216 { 217 StringBuffer buffer = new StringBuffer (); 218 String name = null; 219 Node tempNode = null; 220 final Iterator e = m_map.keySet().iterator(); 221 222 while( e.hasNext() ) 223 { 224 name = (String )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 |