add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / contrib / highlighter / src / java / org / apache / lucene / search / vectorhighlight / ScoreOrderFragmentsBuilder.java
1 package org.apache.lucene.search.vectorhighlight;
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.Collections;
21 import java.util.Comparator;
22 import java.util.List;
23
24 import org.apache.lucene.search.vectorhighlight.FieldFragList.WeightedFragInfo;
25
26 /**
27  * An implementation of FragmentsBuilder that outputs score-order fragments.
28  */
29 public class ScoreOrderFragmentsBuilder extends BaseFragmentsBuilder {
30
31   /**
32    * a constructor.
33    */
34   public ScoreOrderFragmentsBuilder(){
35     super();
36   }
37
38   /**
39    * a constructor.
40    * 
41    * @param preTags array of pre-tags for markup terms.
42    * @param postTags array of post-tags for markup terms.
43    */
44   public ScoreOrderFragmentsBuilder( String[] preTags, String[] postTags ){
45     super( preTags, postTags );
46   }
47
48   /**
49    * Sort by score the list of WeightedFragInfo
50    */
51   @Override
52   public List<WeightedFragInfo> getWeightedFragInfoList( List<WeightedFragInfo> src ) {
53     Collections.sort( src, new ScoreComparator() );
54     return src;
55   }
56
57   public static class ScoreComparator implements Comparator<WeightedFragInfo> {
58
59     public int compare( WeightedFragInfo o1, WeightedFragInfo o2 ) {
60       if( o1.totalBoost > o2.totalBoost ) return -1;
61       else if( o1.totalBoost < o2.totalBoost ) return 1;
62       // if same score then check startOffset
63       else{
64         if( o1.startOffset < o2.startOffset ) return -1;
65         else if( o1.startOffset > o2.startOffset ) return 1;
66       }
67       return 0;
68     }
69   }
70 }