/** * Creates a {@link ProcessingResult} with the provided <code>attributes</code>. Assigns unique * document identifiers if documents are present in the <code>attributes</code> map (under the key * {@link AttributeNames#DOCUMENTS}). */ @SuppressWarnings("unchecked") ProcessingResult(Map<String, Object> attributes) { this.attributes = attributes; // Replace a modifiable collection of documents with an unmodifiable one final List<Document> documents = (List<Document>) attributes.get(AttributeNames.DOCUMENTS); if (documents != null) { Document.assignDocumentIds(documents); attributes.put(AttributeNames.DOCUMENTS, Collections.unmodifiableList(documents)); } // Replace a modifiable collection of clusters with an unmodifiable one final List<Cluster> clusters = (List<Cluster>) attributes.get(AttributeNames.CLUSTERS); if (clusters != null) { Cluster.assignClusterIds(clusters); attributes.put(AttributeNames.CLUSTERS, Collections.unmodifiableList(clusters)); } // Store a reference to attributes as an unmodifiable map this.attributesView = Collections.unmodifiableMap(attributes); }
/** * Prepares a temporary attributes map for serialization purposes. Includes only the requested * elements in the map. */ private Map<String, Object> prepareAttributesForSerialization( boolean saveDocuments, boolean saveClusters, boolean saveOtherAttributes) { final Map<String, Object> tempAttributes = Maps.newHashMap(); if (saveOtherAttributes) { tempAttributes.putAll(attributes); tempAttributes.remove(AttributeNames.DOCUMENTS); tempAttributes.remove(AttributeNames.CLUSTERS); } else { tempAttributes.put(AttributeNames.QUERY, attributes.get(AttributeNames.QUERY)); } if (saveDocuments) { tempAttributes.put(AttributeNames.DOCUMENTS, getDocuments()); } if (saveClusters) { tempAttributes.put(AttributeNames.CLUSTERS, getClusters()); } return tempAttributes; }
/** Transfers document and cluster lists to the attributes map after deserialization. */ @Commit private void afterDeserialization() throws Exception { if (otherAttributesForSerialization != null) { attributes = SimpleXmlWrappers.unwrap(otherAttributesForSerialization); } attributesView = Collections.unmodifiableMap(attributes); attributes.put(AttributeNames.QUERY, query != null ? query.trim() : null); attributes.put(AttributeNames.DOCUMENTS, documents); attributes.put(AttributeNames.CLUSTERS, clusters); // Convert document ids to the actual references if (clusters != null && documents != null) { final Map<String, Document> documentsById = Maps.newHashMap(); for (Document document : documents) { documentsById.put(document.getStringId(), document); } for (Cluster cluster : clusters) { documentIdToReference(cluster, documentsById); } } }
/** * Associates an attribute with this cluster. * * @param key for the attribute * @param value for the attribute * @return this cluster for convenience */ public <T> Cluster setAttribute(String key, T value) { synchronized (attributes) { attributes.put(key, value); } return this; }