package instance;
import java.util.List;
import java.util.concurrent.*;
import list.ComparisonTable;
import list.Document;
public class DocumentProcessor implements Callable< String >
{
private String filename;
public static void process( List< String > filenames )
{
int documentCount = Options.getDocuments( ).size( );
Executor pool = Executors.newFixedThreadPool( documentCount < Options.getMaxThreads( )
?
documentCount : Options.getMaxThreads( ) );
CompletionService< String > completion = new ExecutorCompletionService< String >( pool );
for( String filename : Options.getDocuments( ) )
{
completion.submit( new DocumentProcessor( filename ) );
}
for( int i = 0; i < documentCount; ++i )
{
try
{
System.out.println( "\n\n" + completion.take( ).get( ) );
}
catch( Exception e ) { }
}
( (ThreadPoolExecutor)pool ).shutdown( );
}
public DocumentProcessor( String filename )
{
this.filename = filename;
}
@Override
public String call( )
{
Document doc = new Document( filename );
return ( "Results for document \"" + filename + "\":\n\n"
+ ComparisonTable.getTable( doc.getExistingList( ),
doc.getNewList( ) )
+ "\n"
+ doc.toString( ) );
}
}