@Override protected List<Citation> collapseNonEmpty(List<Citation> objects) { Map<Integer, List<Citation>> citationsByPositionMap = new HashMap<Integer, List<Citation>>(); for (Citation citation : objects) { List<Citation> list = citationsByPositionMap.get(citation.getEntry().getPosition()); if (list == null) { citationsByPositionMap.put( citation.getEntry().getPosition(), list = new ArrayList<Citation>()); } list.add(citation); } List<Citation> results = new ArrayList<Citation>(citationsByPositionMap.size()); for (List<Citation> citationsByPosition : citationsByPositionMap.values()) { results.add(collapseForPosition(citationsByPosition)); } return results; }
/** * Merges existing citation with new citation entry details. * * @param existingCitation * @param newCitationEntry * @return existing citation supplemented with new citation entry details */ Citation merge(Citation existingCitation, CitationEntry newCitationEntry) { if (newCitationEntry != null) { if (newCitationEntry.getDestinationDocumentId() != null && newCitationEntry.getConfidenceLevel() != null) { // setting only when not set or when confidence level higher than already stored // important assumption is based on schema assurance: confidenceLevel is always set // when destinationDocumentId was set if (existingCitation.getEntry().getDestinationDocumentId() == null || existingCitation.getEntry().getConfidenceLevel() == null || existingCitation.getEntry().getConfidenceLevel() < newCitationEntry.getConfidenceLevel()) { existingCitation .getEntry() .setDestinationDocumentId(newCitationEntry.getDestinationDocumentId()); existingCitation.getEntry().setConfidenceLevel(newCitationEntry.getConfidenceLevel()); } } // according to schema externalDestinationDocumentIds cannot be null if (!newCitationEntry.getExternalDestinationDocumentIds().isEmpty()) { existingCitation .getEntry() .getExternalDestinationDocumentIds() .putAll(newCitationEntry.getExternalDestinationDocumentIds()); } if (newCitationEntry.getRawText() != null && newCitationEntry.getRawText().length() > 0) { existingCitation.getEntry().setRawText(newCitationEntry.getRawText()); } } return existingCitation; }