/** * Method getTails returns all the tails of this SubAssembly object. These values are set by {@link #setTails(Pipe...)}. * * @return the tails (type Pipe[]) of this SubAssembly object. */ public Pipe[] getTails() { return getPrevious(); // just returns a clone of tails }
@Override public String getName() { if( name == null ) name = Util.join( getTailNames(), "+" ); return name; }
/** * Must be called by subclasses to set the final end points of the assembly the subclass represents. * * @param tails of type Pipe */ protected void setTails( Pipe... tails ) { if( this.tails != null ) throw new IllegalStateException( "this method may only be called once" ); this.tails = tails; if( previous == null ) { LOG.warn( "previous pipes not set via setPrevious or constructor on: {}", this ); return; } Set<Pipe> stopSet = new HashSet<Pipe>(); Collections.addAll( stopSet, previous ); setParent( stopSet, tails ); }
for( Pipe tail : ( (SubAssembly) pipe ).getTails() )
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 ); } }
/** * 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() ] ); }
protected SubAssembly( String name, Pipe[] previous ) { super( name ); setPrevious( previous ); }
@Test public void testGetNames() { Pipe pipe = new Pipe( "first" ); pipe = new NestedSubAssembly( pipe ); Pipe pipe1 = new Pipe( "fifth", ( (SubAssembly) pipe ).getTails()[ 0 ] ); Pipe pipe2 = new Pipe( "sixth", ( (SubAssembly) pipe ).getTails()[ 1 ] ); assertEquals( 6, Pipe.names( pipe1, pipe2 ).length ); assertEquals( 1, Pipe.named( "second", pipe1, pipe2 ).length ); assertEquals( 1, Pipe.named( "sixth", pipe1, pipe2 ).length ); assertEquals( pipe2, Pipe.named( "sixth", pipe1, pipe2 )[ 0 ] ); } }
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 ); } }
protected SubAssembly( Pipe... previous ) { setPrevious( previous ); }
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 void verifyPipe() { if( !( previous instanceof SubAssembly ) ) return; String[] strings = ( (SubAssembly) previous ).getTailNames(); if( strings.length != 1 ) throw new IllegalArgumentException( "pipe assembly must not return more than one tail pipe instance, found " + Util.join( strings, ", " ) ); }
private void setParent( Set<Pipe> stopSet, Pipe[] tails ) { if( tails == null ) return; for( Pipe tail : tails ) { if( stopSet.contains( tail ) ) continue; tail.setParent( this ); Pipe[] current; if( tail instanceof SubAssembly ) current = ( (SubAssembly) tail ).previous; else current = tail.getPrevious(); if( current == null && tail instanceof SubAssembly ) LOG.warn( "previous pipes not set via setPrevious or constructor on: {}", tail ); setParent( stopSet, current ); } }
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; }