blob: b00ee97452c202032c01a524beb02e84424198f6 (
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
|
// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.grouping.request;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
/**
* This class represents a predefined bucket-function in a {@link GroupingExpression}. It maps the input into one of the
* given buckets by the result of the argument expression.
*
* @author <a href="mailto:simon@yahoo-inc.com">Simon Thoresen</a>
*/
public abstract class PredefinedFunction extends FunctionNode {
protected PredefinedFunction(GroupingExpression exp, List<? extends BucketValue> args) {
super("predefined", asList(exp, args));
Iterator<? extends BucketValue> it = args.iterator();
BucketValue prev = it.next();
while (it.hasNext()) {
BucketValue arg = it.next();
if (prev.compareTo(arg) >= 0) {
throw new IllegalArgumentException("Buckets must be monotonically increasing, got " + prev +
" before " + arg + ".");
}
prev = arg;
}
}
/**
* Returns the number of buckets to divide the result into.
*
* @return The bucket count.
*/
public int getNumBuckets() {
return getNumArgs() - 1;
}
/**
* Returns the bucket at the given index.
*
* @param i The index of the bucket to return.
* @return The bucket at the given index.
* @throws IndexOutOfBoundsException If the index is out of range.
*/
public BucketValue getBucket(int i) {
return (BucketValue)getArg(i + 1);
}
private static
List<GroupingExpression> asList(GroupingExpression exp, List<? extends BucketValue> args) {
List<GroupingExpression> ret = new LinkedList<>();
ret.add(exp);
ret.addAll(args);
return ret;
}
}
|