/** * Is responsible for unwinding nested SubAssembly instances. * * @param tails of type Pipe[] * @return a Pipe[] */ public static Pipe[] unwind( Pipe... tails ) { Set<Pipe> previous = new HashSet<Pipe>(); for( Pipe pipe : tails ) { if( pipe instanceof SubAssembly ) Collections.addAll( previous, unwind( pipe.getPrevious() ) ); else previous.add( pipe ); } return previous.toArray( new Pipe[ previous.size() ] ); }
private static void collectPipes( String name, Pipe[] tails, Set<Pipe> pipes ) { for( Pipe tail : tails ) { if( !( tail instanceof SubAssembly ) && tail.getName().equals( name ) ) pipes.add( tail ); collectPipes( name, SubAssembly.unwind( tail.getPrevious() ), pipes ); } }
private static void collectNames( Pipe[] pipes, Set<String> names ) { for( Pipe pipe : pipes ) { if( pipe instanceof SubAssembly ) names.addAll( asList( ( (SubAssembly) pipe ).getTailNames() ) ); else names.add( pipe.getName() ); collectNames( SubAssembly.unwind( pipe.getPrevious() ), names ); } }
public static int isPrevious( Pipe pipe, Pipe previous ) { if( pipe == previous ) return 0; if( pipe instanceof SubAssembly ) { Pipe[] unwind = SubAssembly.unwind( pipe ); for( Pipe unwound : unwind ) { int result = collectPipes( unwound, 0, previous ); if( result != -1 ) return result; } return -1; } return collectPipes( pipe, 0, previous ); }
private static int collectPipes( Pipe pipe, int depth, Pipe... allPrevious ) { depth++; for( Pipe previous : allPrevious ) { if( pipe == previous ) return depth; int result; if( previous instanceof SubAssembly ) result = collectPipes( pipe, depth, SubAssembly.unwind( previous ) ); else result = collectPipes( pipe, depth, previous.getPrevious() ); if( result != -1 ) return result; } return -1; }