aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java
diff options
context:
space:
mode:
Diffstat (limited to 'vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java')
-rw-r--r--vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java56
1 files changed, 56 insertions, 0 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java b/vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java
new file mode 100644
index 00000000000..f46f000d1ee
--- /dev/null
+++ b/vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java
@@ -0,0 +1,56 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.tensor;
+
+import com.google.common.annotations.Beta;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Builder class for a MapTensor.
+ *
+ * The set of dimensions of the resulting tensor is the union of
+ * the dimensions specified explicitly and the ones specified in the
+ * tensor cell addresses.
+ *
+ * @author <a href="mailto:geirst@yahoo-inc.com">Geir Storli</a>
+ */
+@Beta
+public class MapTensorBuilder {
+
+ private final Set<String> dimensions = new HashSet<>();
+ private final Map<TensorAddress, Double> cells = new HashMap<>();
+
+ public class CellBuilder {
+
+ private final TensorAddress.Builder addressBuilder = new TensorAddress.Builder();
+
+ public CellBuilder label(String dimension, String label) {
+ dimensions.add(dimension);
+ addressBuilder.add(dimension, label);
+ return this;
+ }
+ public MapTensorBuilder value(double cellValue) {
+ cells.put(addressBuilder.build(), cellValue);
+ return MapTensorBuilder.this;
+ }
+ }
+
+ public MapTensorBuilder() {
+ }
+
+ public MapTensorBuilder dimension(String dimension) {
+ dimensions.add(dimension);
+ return this;
+ }
+
+ public CellBuilder cell() {
+ return new CellBuilder();
+ }
+
+ public Tensor build() {
+ return new MapTensor(dimensions, cells);
+ }
+}