KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > lucene > search > Explanation


1 package org.apache.lucene.search;
2
3 /**
4  * Copyright 2004 The Apache Software Foundation
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.ArrayList JavaDoc;
20
21 /** Expert: Describes the score computation for document and query. */
22 public class Explanation implements java.io.Serializable JavaDoc {
23   private float value; // the value of this node
24
private String JavaDoc description; // what it represents
25
private ArrayList JavaDoc details; // sub-explanations
26

27   public Explanation() {}
28
29   public Explanation(float value, String JavaDoc description) {
30     this.value = value;
31     this.description = description;
32   }
33
34   /** The value assigned to this explanation node. */
35   public float getValue() { return value; }
36   /** Sets the value assigned to this explanation node. */
37   public void setValue(float value) { this.value = value; }
38
39   /** A description of this explanation node. */
40   public String JavaDoc getDescription() { return description; }
41   /** Sets the description of this explanation node. */
42   public void setDescription(String JavaDoc description) {
43     this.description = description;
44   }
45
46   /** The sub-nodes of this explanation node. */
47   public Explanation[] getDetails() {
48     if (details == null)
49       return null;
50     return (Explanation[])details.toArray(new Explanation[0]);
51   }
52
53   /** Adds a sub-node to this explanation node. */
54   public void addDetail(Explanation detail) {
55     if (details == null)
56       details = new ArrayList JavaDoc();
57     details.add(detail);
58   }
59
60   /** Render an explanation as text. */
61   public String JavaDoc toString() {
62     return toString(0);
63   }
64   private String JavaDoc toString(int depth) {
65     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
66     for (int i = 0; i < depth; i++) {
67       buffer.append(" ");
68     }
69     buffer.append(getValue());
70     buffer.append(" = ");
71     buffer.append(getDescription());
72     buffer.append("\n");
73
74     Explanation[] details = getDetails();
75     if (details != null) {
76       for (int i = 0 ; i < details.length; i++) {
77         buffer.append(details[i].toString(depth+1));
78       }
79     }
80
81     return buffer.toString();
82   }
83
84
85   /** Render an explanation as HTML. */
86   public String JavaDoc toHtml() {
87     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
88     buffer.append("<ul>\n");
89
90     buffer.append("<li>");
91     buffer.append(getValue());
92     buffer.append(" = ");
93     buffer.append(getDescription());
94     buffer.append("</li>\n");
95
96     Explanation[] details = getDetails();
97     if (details != null) {
98       for (int i = 0 ; i < details.length; i++) {
99         buffer.append(details[i].toHtml());
100       }
101     }
102
103     buffer.append("</ul>\n");
104
105     return buffer.toString();
106   }
107 }
108
Popular Tags