summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/com/yahoo/tensor/MapTensorBuilder.java
blob: f46f000d1ee470623ec2da13b5a7888b51ef53bd (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
// 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);
    }
}