Author: bpoussin Date: 2011-04-01 18:57:08 +0200 (Fri, 01 Apr 2011) New Revision: 755 Url: http://nuiton.org/repositories/revision/wikitty/755 Log: Evolution #1436: add new method to retrieve Topic on PagedResult Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/search/PagedResult.java Modified: trunk/wikitty-api/src/main/java/org/nuiton/wikitty/search/PagedResult.java =================================================================== --- trunk/wikitty-api/src/main/java/org/nuiton/wikitty/search/PagedResult.java 2011-03-25 13:56:41 UTC (rev 754) +++ trunk/wikitty-api/src/main/java/org/nuiton/wikitty/search/PagedResult.java 2011-04-01 16:57:08 UTC (rev 755) @@ -31,6 +31,7 @@ import java.util.ArrayList; import java.util.Collection; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.logging.Log; @@ -72,7 +73,13 @@ protected List<T> results; /** facet asked or null if no facet */ protected Map<String, List<FacetTopic>> facets; + /** facet asked of null if no facet, FacetTopic are put in map with key topic name, + * To use this variable, you must used getter, because, this variable is + * lazy loaded from facets variable. + */ + protected Map<String, Map<String, FacetTopic>> facetsAsMap = null; + /** * we don't initialize securityToken * @@ -241,6 +248,40 @@ } /** + * Return topic for the specified facet and topic name. + * + * @param facetName name of the wanted facet + * @param topicName name of the wanted topic + * @return topic + */ + public FacetTopic getTopic(String facetName, String topicName) { + FacetTopic result = getFacetsAsMap().get(facetName).get(topicName); + return result; + } + + /** + * Return topic count for the specified facet and topic name. If facet or + * topic don't exist, return 0. + * + * @param facetName name of the wanted facet + * @param topicName name of the wanted topic + * @return topic count or 0 + */ + public int getTopicCount(String facetName, String topicName) { + int result = 0; + if (getFacetsAsMap() != null) { + Map<String, FacetTopic> topics = getFacetsAsMap().get(facetName); + if (topics != null) { + FacetTopic topic = topics.get(topicName); + if (topic != null) { + result = topic.getCount(); + } + } + } + return result; + } + + /** * Get map represent facets. * * @return all facets @@ -249,6 +290,25 @@ return facets; } + public Map<String, Map<String, FacetTopic>> getFacetsAsMap() { + if (facetsAsMap == null && facets != null) { + // use local variable to prevent multi-thread problem (multiple add) + Map<String, Map<String, FacetTopic>> localFacetsAsMap = + new HashMap<String, Map<String, FacetTopic>>(); + for (Map.Entry<String, List<FacetTopic>> e : getFacets().entrySet()) { + Map<String, FacetTopic> topics = new HashMap<String, FacetTopic>(); + localFacetsAsMap.put(e.getKey(), topics); + for (FacetTopic t : e.getValue()) { + topics.put(t.getTopicName(), t); + } + } + facetsAsMap = localFacetsAsMap; + } + return facetsAsMap; + } + + + /** * Return the first element in result *