This code has loops under loops which affects the performance badly. Please help me to optimize the code to improve its performance.
private void processPlainOutputFormat() {
// we reached the end of corpus processing.Process all Documents from corpus
Iterator<Document> documentIterator = getCorpus().iterator();
// method which create a header string
String header = getHeaderString(getFeaturesList());
List outputList = new ArrayList();
outputList.add(header);
boolean deleteDoc = (corpus.getDataStore() != null);
while (documentIterator.hasNext()) {
Document currentDocument = documentIterator.next();
AnnotationSet inputAS = inputAnnotationSet == null || inputAnnotationSet.trim().length() == 0 ? currentDocument.getAnnotations() : currentDocument.getAnnotations(inputAnnotationSet);
if (inputAS != null && inputAS.size() > 0) {
AnnotationSet annotationsToPrintSet = inputAS.get(getAnnotationName());
if (getFeaturesList() != null && getFeaturesList().size() > 0) {
List<Annotation> sortedAnnotations = new ArrayList(annotationsToPrintSet);
Collections.sort(sortedAnnotations, new OffsetComparator());
Iterator<Annotation> iterator = sortedAnnotations.iterator();
// just print out values directly to the GATE message window
while (iterator.hasNext()) {
Annotation annotation = iterator.next();
Iterator featuresIterator = getFeaturesList().iterator();
// local quick output
StringBuffer outputBuffer = new StringBuffer();
if (getPrintContent()) {
String content = currentDocument.getContent().toString().substring(annotation.getStartNode().getOffset().intValue(), annotation.getEndNode().getOffset().intValue());
outputBuffer.append(content).append(getFeaturesSeparatorSymbol());
}
while (featuresIterator.hasNext()) {
Object feature = featuresIterator.next();
outputBuffer.append(annotation.getFeatures().get(feature));
if (featuresIterator.hasNext()) {
outputBuffer.append(getFeaturesSeparatorSymbol());
}
}
outputList.add(outputBuffer.toString());
}
} else if (getPrintContent()) {
// just print content of the Annotation
List<Annotation> sortedAnnotations = new ArrayList(annotationsToPrintSet);
Collections.sort(sortedAnnotations, new OffsetComparator());
Iterator<Annotation> iterator = sortedAnnotations.iterator();
// just print out values directly to the GATE message window
while (iterator.hasNext()) {
Annotation annotation = iterator.next();
StringBuffer outputBuffer = new StringBuffer();
if (getPrintContent()) {
String content = currentDocument.getContent().toString().substring(annotation.getStartNode().getOffset().intValue(), annotation.getEndNode().getOffset().intValue());
outputBuffer.append(content);
}
outputList.add(outputBuffer.toString());
}
}
}
// cleanup document
corpus.unloadDocument(currentDocument);
if (deleteDoc) {
Factory.deleteResource(currentDocument);
}
}
// check output destination
if (getOutputDestination() == null) {
// just print out results
Iterator iterator = outputList.iterator();
while (iterator.hasNext()) {
Out.println(iterator.next());
}
} else {
write(outputList, getOutputDestination().getPath());
}
}