aboutsummaryrefslogtreecommitdiff
path: root/src/uk/ac/ox/cs/pagoda/util/tuples/TupleBuilder.java
blob: 172e249e666dba7675dbdc63457381f27f4e3e30 (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
package uk.ac.ox.cs.pagoda.util.tuples;

import java.util.Collections;

/**
 * Allows to create an immutable <tt>Tuple</tt> in a non-atomic way.
 * It can create only one <tt>Tuple</tt>.
 * */
public class TupleBuilder<T> {

    private Tuple<T> tuple = new Tuple<T>();

    private boolean building = true;

    public TupleBuilder<T> append(T t) {
        if(building) {
            tuple.elements.add(t);
            return this;
        }
        return null;
    }

    public TupleBuilder<T> append(T[] t) {
        if(building) {
            Collections.addAll(tuple.elements, t);
            return this;
        }
        return null;
    }

    public Tuple<T> build() {
        if(building) {
            building = false;
            return tuple;
        }
        return null;
    }
}