aboutsummaryrefslogtreecommitdiffstats
path: root/config-model/src/main/java/com/yahoo/schema/document/Sorting.java
blob: 2d0c9a5d27b24895cfafaf3ea6869f17aa76ece1 (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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.schema.document;

import java.io.Serializable;

/**
 * A search-time document attribute sort specification(per-document in-memory value).
 * This belongs to the attribute or field(implicitt attribute).
 *
 * @author baldersheim
 */
public final class Sorting implements Cloneable, Serializable {

    // Remember to change hashCode and equals when you add new fields
    public enum Function {UCA, RAW, LOWERCASE}
    public enum Strength {PRIMARY, SECONDARY, TERTIARY, QUATERNARY, IDENTICAL}
    private boolean ascending = true;
    private Function function = Function.UCA;
    private String locale = "";
    private Strength strength = Strength.PRIMARY;

    public boolean isAscending()       { return ascending; }
    public boolean isDescending()      { return ! ascending; }
    public String getLocale()          { return locale; }
    public Function getFunction()      { return function; }
    public Strength getStrength()      { return strength; }

    public void setAscending()                 { ascending = true; }
    public void setDescending()                { ascending = false; }
    public void setFunction(Function function) { this.function = function; }
    public void setLocale(String locale)       { this.locale = locale; }
    public void setStrength(Strength strength) { this.strength = strength; }

    public int hashCode() {
        return locale.hashCode() +
               strength.hashCode() +
               function.hashCode() +
               (isDescending() ? 13 : 0);
    }

    public boolean equals(Object object) {
        if (! (object instanceof Sorting)) return false;

        Sorting other=(Sorting)object;
        return this.locale.equals(other.locale) &&
               (ascending == other.ascending) &&
               (function == other.function) &&
               (strength == other.strength);
    }

    @Override
    public Sorting clone() {
        try {
            return (Sorting)super.clone();
        }
        catch (CloneNotSupportedException e) {
            throw new RuntimeException("Programming error");
        }
    }

    public String toString() {
        return "sorting '" + (isAscending() ? '+' : '-') + function.toString() + "(" + strength.toString() + ", " + locale + ")";
    }

}