pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / contrib / queryparser / src / java / org / apache / lucene / queryParser / core / nodes / BoostQueryNode.java
1 package org.apache.lucene.queryParser.core.nodes;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import java.util.List;
21
22 import org.apache.lucene.messages.MessageImpl;
23 import org.apache.lucene.queryParser.core.QueryNodeError;
24 import org.apache.lucene.queryParser.core.QueryNodeException;
25 import org.apache.lucene.queryParser.core.messages.QueryParserMessages;
26 import org.apache.lucene.queryParser.core.parser.EscapeQuerySyntax;
27
28 /**
29  * A {@link BoostQueryNode} boosts the QueryNode tree which is under this node.
30  * So, it must only and always have one child.
31  * 
32  * The boost value may vary from 0.0 to 1.0.
33  * 
34  */
35 public class BoostQueryNode extends QueryNodeImpl {
36
37   private static final long serialVersionUID = -3929082630855807593L;
38
39   private float value = 0;
40
41   /**
42    * Constructs a boost node
43    * 
44    * @param query
45    *          the query to be boosted
46    * @param value
47    *          the boost value, it may vary from 0.0 to 1.0
48    * 
49    * @throws QueryNodeException
50    */
51   public BoostQueryNode(QueryNode query, float value) throws QueryNodeException {
52     if (query == null) {
53       throw new QueryNodeError(new MessageImpl(
54           QueryParserMessages.NODE_ACTION_NOT_SUPPORTED, "query", "null"));
55     }
56
57     this.value = value;
58     setLeaf(false);
59     allocate();
60     add(query);
61   }
62
63   /**
64    * Returns the single child which this node boosts.
65    * 
66    * @return the single child which this node boosts
67    */
68   public QueryNode getChild() {
69     List<QueryNode> children = getChildren();
70
71     if (children == null || children.size() == 0) {
72       return null;
73     }
74
75     return children.get(0);
76
77   }
78
79   /**
80    * Returns the boost value. It may vary from 0.0 to 1.0.
81    * 
82    * @return the boost value
83    */
84   public float getValue() {
85     return this.value;
86   }
87
88   /**
89    * Returns the boost value parsed to a string.
90    * 
91    * @return the parsed value
92    */
93   private CharSequence getValueString() {
94     Float f = Float.valueOf(this.value);
95     if (f == f.longValue())
96       return "" + f.longValue();
97     else
98       return "" + f;
99
100   }
101
102   @Override
103   public String toString() {
104     return "<boost value='" + getValueString() + "'>" + "\n"
105         + getChild().toString() + "\n</boost>";
106   }
107
108   public CharSequence toQueryString(EscapeQuerySyntax escapeSyntaxParser) {
109     if (getChild() == null)
110       return "";
111     return getChild().toQueryString(escapeSyntaxParser) + "^"
112         + getValueString();
113   }
114
115   @Override
116   public QueryNode cloneTree() throws CloneNotSupportedException {
117     BoostQueryNode clone = (BoostQueryNode) super.cloneTree();
118
119     clone.value = this.value;
120
121     return clone;
122   }
123
124 }