summaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/grouping/request/PredefinedFunction.java
diff options
context:
space:
mode:
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/grouping/request/PredefinedFunction.java')
-rw-r--r--container-search/src/main/java/com/yahoo/search/grouping/request/PredefinedFunction.java58
1 files changed, 58 insertions, 0 deletions
diff --git a/container-search/src/main/java/com/yahoo/search/grouping/request/PredefinedFunction.java b/container-search/src/main/java/com/yahoo/search/grouping/request/PredefinedFunction.java
new file mode 100644
index 00000000000..b00ee97452c
--- /dev/null
+++ b/container-search/src/main/java/com/yahoo/search/grouping/request/PredefinedFunction.java
@@ -0,0 +1,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;
+ }
+}
+