FileDefinitionParser { void mergeContiguousRegions( Collection<AbstractBlock> blocks ) { AbstractIgnorableInstruction initialInstr = null; List<AbstractBlock> toRemove = new ArrayList<> (); StringBuilder sb = new StringBuilder(); for( AbstractBlock block : blocks ) { if( initialInstr == null ) { if( block.getInstructionType() == AbstractBlock.COMMENT || block.getInstructionType() == AbstractBlock.BLANK ) { AbstractIgnorableInstruction currentInstr = (AbstractIgnorableInstruction) block; initialInstr = currentInstr; sb = new StringBuilder( currentInstr.getContent()); } } else if( initialInstr.getInstructionType() == block.getInstructionType()) { toRemove.add( block ); sb.append( System.getProperty( "line.separator" )); sb.append(((AbstractIgnorableInstruction) block).getContent()); } else { initialInstr.setContent( sb.toString()); initialInstr = null; } } blocks.removeAll( toRemove ); for( AbstractBlock block : blocks ) { if( block.getInstructionType() == AbstractBlock.COMPONENT || block.getInstructionType() == AbstractBlock.FACET ) mergeContiguousRegions(((AbstractBlockHolder) block).getInnerBlocks()); } } FileDefinitionParser( File relationsFile, boolean ignoreComments ); FileDefinition read();  }
@Test public void testMergeContiguousRegions() { FileDefinition file = new FileDefinition( new File( "some-file" )); FileDefinitionParser parser = new FileDefinitionParser( null, false ); List<AbstractBlock> blocks = new ArrayList<> (); blocks.add( new BlockImport( file )); blocks.add( new BlockImport( file )); parser.mergeContiguousRegions( blocks ); Assert.assertEquals( 2, blocks.size()); blocks.clear(); blocks.add( new BlockProperty( file )); blocks.add( new BlockProperty( file )); parser.mergeContiguousRegions( blocks ); Assert.assertEquals( 2, blocks.size()); blocks.clear(); blocks.add( new BlockComment( file, "# comment 1" )); blocks.add( new BlockComment( file, "# comment 2" )); parser.mergeContiguousRegions( blocks ); Assert.assertEquals( 1, blocks.size()); Assert.assertEquals( AbstractBlock.COMMENT, blocks.get( 0 ).getInstructionType()); blocks.clear(); blocks.add( new BlockComment( file, "# comment 1" )); blocks.add( new BlockBlank( file, "" )); blocks.add( new BlockComment( file, "# comment 2" )); parser.mergeContiguousRegions( blocks ); Assert.assertEquals( 3, blocks.size()); blocks.clear(); blocks.add( new BlockComment( file, "# comment 1" )); blocks.add( new BlockBlank( file, "" )); blocks.add( new BlockComment( file, "# comment 2" )); blocks.add( new BlockComment( file, "# comment 3" )); parser.mergeContiguousRegions( blocks ); Assert.assertEquals( 3, blocks.size()); Assert.assertEquals( AbstractBlock.COMMENT, blocks.get( 0 ).getInstructionType()); Assert.assertEquals( AbstractBlock.BLANK, blocks.get( 1 ).getInstructionType()); Assert.assertEquals( AbstractBlock.COMMENT, blocks.get( 0 ).getInstructionType()); blocks.clear(); blocks.add( new BlockBlank( file, "" )); blocks.add( new BlockBlank( file, "" )); parser.mergeContiguousRegions( blocks ); Assert.assertEquals( 1, blocks.size()); Assert.assertEquals( AbstractBlock.BLANK, blocks.get( 0 ).getInstructionType()); blocks.clear(); blocks.add( new BlockBlank( file, "" )); blocks.add( new BlockComment( file, "# comment 1" )); blocks.add( new BlockBlank( file, "" )); parser.mergeContiguousRegions( blocks ); Assert.assertEquals( 3, blocks.size()); blocks.clear(); blocks.add( new BlockBlank( file, "" )); blocks.add( new BlockBlank( file, "" )); blocks.add( new BlockComment( file, "# comment 1" )); blocks.add( new BlockBlank( file, "" )); parser.mergeContiguousRegions( blocks ); Assert.assertEquals( 3, blocks.size()); Assert.assertEquals( AbstractBlock.BLANK, blocks.get( 0 ).getInstructionType()); Assert.assertEquals( AbstractBlock.COMMENT, blocks.get( 1 ).getInstructionType()); Assert.assertEquals( AbstractBlock.BLANK, blocks.get( 0 ).getInstructionType()); }