pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / facet / src / java / org / apache / lucene / facet / search / results / FacetResult.java
1 package org.apache.lucene.facet.search.results;
2
3 import org.apache.lucene.facet.search.params.FacetRequest;
4
5 /**
6  * Licensed to the Apache Software Foundation (ASF) under one or more
7  * contributor license agreements.  See the NOTICE file distributed with
8  * this work for additional information regarding copyright ownership.
9  * The ASF licenses this file to You under the Apache License, Version 2.0
10  * (the "License"); you may not use this file except in compliance with
11  * the License.  You may obtain a copy of the License at
12  *
13  *     http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21
22 /**
23  * Result of faceted search.
24  * 
25  * @lucene.experimental
26  */
27 public class FacetResult {
28   
29   private final FacetRequest facetRequest;
30   private final FacetResultNode rootNode;
31   private final int numValidDescendants;
32   
33   public FacetResult(FacetRequest facetRequest, FacetResultNode rootNode,  int numValidDescendants) {
34     this.facetRequest = facetRequest;
35     this.rootNode = rootNode;
36     this.numValidDescendants = numValidDescendants;
37   }
38   
39   /**
40    * Facet result node matching the root of the {@link #getFacetRequest() facet request}.
41    * @see #getFacetRequest()
42    * @see FacetRequest#getCategoryPath()
43    */
44   public final FacetResultNode getFacetResultNode() {
45     return this.rootNode;
46   }
47   
48   /**
49    * Number of descendants of {@link #getFacetResultNode() root facet result node}, 
50    * up till the requested depth, which are valid by the 
51    * {@link FacetRequest#createFacetResultsHandler(org.apache.lucene.facet.taxonomy.TaxonomyReader)
52    * results handler in effect}. Typically -- have value != 0.
53    * This number does not include the root node. 
54    * @see #getFacetRequest()
55    * @see FacetRequest#getDepth()
56    */
57   public final int getNumValidDescendants() {
58     return this.numValidDescendants;
59   }
60   
61   /**
62    * Request for which this result was obtained.
63    */
64   public final FacetRequest getFacetRequest() {
65     return this.facetRequest;
66   }
67   
68   /**
69    * String representation of this facet result.
70    * Use with caution: might return a very long string.
71    * @param prefix prefix for each result line
72    * @see #toString()
73    */
74   public String toString(String prefix) {
75     StringBuilder sb = new StringBuilder();
76     String nl = "";
77     
78     // request
79     if (this.facetRequest != null) {
80       sb.append(nl).append(prefix).append("Request: ").append(
81           this.facetRequest.toString());
82       nl = "\n";
83     }
84     
85     // total facets
86     sb.append(nl).append(prefix).append("Num valid Descendants (up to specified depth): ").append(
87         this.numValidDescendants);
88     nl = "\n";
89     
90     // result node
91     if (this.rootNode != null) {
92       sb.append(nl).append(this.rootNode.toString(prefix + "\t"));
93     }
94     
95     return sb.toString();
96   }
97   
98   @Override
99   public String toString() {
100     return toString("");
101   }
102   
103 }