aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/request/TimeFunctions.java
blob: 577ac72a56bd836134f6491765863a1fce46f559 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.request;

/**
 * This abstract class is a factory for timestamp functions in a {@link GroupingExpression}. Apart from offering
 * per-function factory methods, this class also contains a {@link #newInstance(com.yahoo.search.grouping.request.TimeFunctions.Type,
 * GroupingExpression)} method which is useful for runtime construction of grouping requests.
 *
 * @author Simon Thoresen Hult
 */
public abstract class TimeFunctions {

    /**
     * Defines the different types of timestamps-functions that are available.
     */
    public enum Type {
        DATE,
        DAY_OF_MONTH,
        DAY_OF_WEEK,
        DAY_OF_YEAR,
        HOUR_OF_DAY,
        MINUTE_OF_HOUR,
        MONTH_OF_YEAR,
        SECOND_OF_MINUTE,
        YEAR
    }

    /**
     * Creates a new timestamp-function of the specified type for the given {@link GroupingExpression}.
     *
     * @param type The type of function to create.
     * @param exp  The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static FunctionNode newInstance(Type type, GroupingExpression exp) {
        switch (type) {
        case DATE:
            return newDate(exp);
        case DAY_OF_MONTH:
            return newDayOfMonth(exp);
        case DAY_OF_WEEK:
            return newDayOfWeek(exp);
        case DAY_OF_YEAR:
            return newDayOfYear(exp);
        case HOUR_OF_DAY:
            return newHourOfDay(exp);
        case MINUTE_OF_HOUR:
            return newMinuteOfHour(exp);
        case MONTH_OF_YEAR:
            return newMonthOfYear(exp);
        case SECOND_OF_MINUTE:
            return newSecondOfMinute(exp);
        case YEAR:
            return newYear(exp);
        }
        throw new UnsupportedOperationException("Time function '" + type + "' not supported.");
    }

    /**
     * Creates a new instance of {@link DateFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static DateFunction newDate(GroupingExpression exp) {
        return new DateFunction(exp);
    }

    /**
     * Creates a new instance of {@link DayOfMonthFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static DayOfMonthFunction newDayOfMonth(GroupingExpression exp) {
        return new DayOfMonthFunction(exp);
    }

    /**
     * Creates a new instance of {@link DayOfWeekFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static DayOfWeekFunction newDayOfWeek(GroupingExpression exp) {
        return new DayOfWeekFunction(exp);
    }

    /**
     * Creates a new instance of {@link DayOfYearFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static DayOfYearFunction newDayOfYear(GroupingExpression exp) {
        return new DayOfYearFunction(exp);
    }

    /**
     * Creates a new instance of {@link HourOfDayFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static HourOfDayFunction newHourOfDay(GroupingExpression exp) {
        return new HourOfDayFunction(exp);
    }

    /**
     * Creates a new instance of {@link MinuteOfHourFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static MinuteOfHourFunction newMinuteOfHour(GroupingExpression exp) {
        return new MinuteOfHourFunction(exp);
    }

    /**
     * Creates a new instance of {@link MonthOfYearFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static MonthOfYearFunction newMonthOfYear(GroupingExpression exp) {
        return new MonthOfYearFunction(exp);
    }

    /**
     * Creates a new instance of {@link SecondOfMinuteFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static SecondOfMinuteFunction newSecondOfMinute(GroupingExpression exp) {
        return new SecondOfMinuteFunction(exp);
    }

    /**
     * Creates a new instance of {@link YearFunction} for the given {@link GroupingExpression}.
     *
     * @param exp The expression to evaluate, must evaluate to a long.
     * @return The created function node.
     */
    public static YearFunction newYear(GroupingExpression exp) {
        return new YearFunction(exp);
    }
}