aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/result/HitGroupsLastComparator.java
blob: 0bd91c9821ea0571a6e1f8a54a4017c2590b7671 (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;

import java.util.Comparator;

/**
 * Ensures that HitGroups are placed last in the result.
 *
 * @author Tony Vaagenes
 */
public class HitGroupsLastComparator extends ChainableComparator {

    public HitGroupsLastComparator(Comparator<Hit> secondaryComparator) {
        super(secondaryComparator);
    }

    @Override
    public int compare(Hit left, Hit right) {
        if (isHitGroup(left) ^ isHitGroup(right)) {
            return isHitGroup(left) ? 1 : -1;
        } else {
            return super.compare(left, right);
        }
    }

    private boolean isHitGroup(Hit hit) {
        return hit instanceof HitGroup;
    }

    @Override
    public String toString() {
        return getSecondaryComparator().toString();
    }
}