OverlappingRecordTransform implements RecordTransformer { @Override public Iterable<Record> transform(Iterable<Record> records) { for (Record record : records) { String reason = (String) record.getAnnotationValues()[exclusionIndex]; if (!reason.isEmpty()) { selectedRecords.add(record); } else { PixelPosProvider.PixelPosRecord pixelPosRecord = pixelPosRecordFactory.create(record); boolean addToWorkRecords = true; if (!workRecords.isEmpty()) { for (int i = 0; i < workRecords.size(); i++) { PixelPosProvider.PixelPosRecord workRecord = workRecords.get(i); boolean overlapping = isOverlapping(pixelPosRecord, workRecord, macroPixelSize); if (overlapping) { if (workRecord.getTimeDifference() > pixelPosRecord.getTimeDifference()) { workRecord.getRecord().getAnnotationValues()[exclusionIndex] = EXCLUSION_REASON_OVERLAPPING; workRecords.set(i, workRecord); } else { addToWorkRecords = false; break; } } } } if (addToWorkRecords) { workRecords.add(pixelPosRecord); } else { pixelPosRecord.getRecord().getAnnotationValues()[exclusionIndex] = EXCLUSION_REASON_OVERLAPPING; workRecords.add(pixelPosRecord); } double minYRow = pixelPosRecord.getPixelPos().y - macroPixelSize; Iterator<PixelPosProvider.PixelPosRecord> workRecordsIterator = workRecords.iterator(); while (workRecordsIterator.hasNext()) { PixelPosProvider.PixelPosRecord workRecord = workRecordsIterator.next(); if (workRecord.getPixelPos().y < minYRow) { workRecordsIterator.remove(); selectedRecords.add(workRecord.getRecord()); } } } } for (PixelPosProvider.PixelPosRecord workRecord : workRecords) { selectedRecords.add(workRecord.getRecord()); } return selectedRecords; } OverlappingRecordTransform(int macroPixelSize, PixelPosRecordFactory pixelPosRecordFactory, Header header); static RecordTransformer create(Header header, int macroPixelSize, boolean filterOverlapping); @Override Iterable<Record> transform(Iterable<Record> records);  }
@Test public void testSelection() throws Exception { int macroPixelSize = 3; OverlappingRecordTransform.PixelPosRecordFactory recordFactory = Mockito.mock(OverlappingRecordTransform.PixelPosRecordFactory.class); Header header = new TestHeader(); OverlappingRecordTransform selector = new OverlappingRecordTransform(macroPixelSize, recordFactory, header); mockPixelPosRecord(recordFactory, 0, new PixelPos(10, 11), 10); mockPixelPosRecord(recordFactory, 1, new PixelPos(100, 11), 10); mockPixelPosRecord(recordFactory, 2, new PixelPos(10, 12), 10); mockPixelPosRecord(recordFactory, 3, new PixelPos(100, 12), 2); mockPixelPosRecord(recordFactory, 4, new PixelPos(10, 13), 10); mockPixelPosRecord(recordFactory, 5, new PixelPos(100, 13), 10); mockPixelPosRecord(recordFactory, 6, new PixelPos(10, 20), 4); mockPixelPosRecord(recordFactory, 7, new PixelPos(11, 20), 5); mockPixelPosRecord(recordFactory, 8, new PixelPos(9, 21), 3); mockPixelPosRecord(recordFactory, 9, new PixelPos(10, 21), 2); mockPixelPosRecord(recordFactory, 10, new PixelPos(11, 21), 5); List<Record> selectedRecords = (List<Record>) selector.transform(records); assertEquals(11, selectedRecords.size()); assertRecord(0, "", selectedRecords.get(0)); assertRecord(1, OverlappingRecordTransform.EXCLUSION_REASON_OVERLAPPING, selectedRecords.get(1)); assertRecord(2, OverlappingRecordTransform.EXCLUSION_REASON_OVERLAPPING, selectedRecords.get(2)); assertRecord(3, "", selectedRecords.get(3)); assertRecord(4, OverlappingRecordTransform.EXCLUSION_REASON_OVERLAPPING, selectedRecords.get(4)); assertRecord(5, OverlappingRecordTransform.EXCLUSION_REASON_OVERLAPPING, selectedRecords.get(5)); assertRecord(6, OverlappingRecordTransform.EXCLUSION_REASON_OVERLAPPING, selectedRecords.get(6)); assertRecord(7, OverlappingRecordTransform.EXCLUSION_REASON_OVERLAPPING, selectedRecords.get(7)); assertRecord(8, OverlappingRecordTransform.EXCLUSION_REASON_OVERLAPPING, selectedRecords.get(8)); assertRecord(9, "", selectedRecords.get(9)); assertRecord(10, OverlappingRecordTransform.EXCLUSION_REASON_OVERLAPPING, selectedRecords.get(10)); }