aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/result/HitSortOrderer.java
blob: ee3aad6922cc97f3374d9d871e1780c31f03ad47 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;

import com.yahoo.search.query.Sorting;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * A hit orderer which can be assigned to a HitGroup to keep that group's
 * hit sorted in accordance with the sorting specification given when this is created.
 *
 * @author Steinar Knutsen
 */
public class HitSortOrderer extends HitOrderer {

    private final Comparator<Hit> fieldComparator;

    /** Create a sort order from a sorting */
    public HitSortOrderer(Sorting sorting) {
        fieldComparator =
                new MetaHitsFirstComparator(
                        new HitGroupsLastComparator(
                                new FieldComparator(sorting)));
    }

    /**
     * Create a sort order from a comparator.
     * This will be appended to the standard comparators used by this.
     */
    public HitSortOrderer(Comparator<Hit> comparator) {
        fieldComparator = new MetaHitsFirstComparator(new HitGroupsLastComparator(comparator));
    }

    /**
     * Orders the given list of hits according to the sorting given at construction
     *
     * Meta hits are sorted before concrete hits, but have no internal
     * ordering. The sorting is stable.
     */
    public void order(List<Hit> hits) {
        Collections.sort(hits, fieldComparator);
    }

    public Comparator<Hit> getComparator() {
        return fieldComparator;
    }

}