aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/processing/response/ArrayDataList.java
blob: 001fb303ea91f802c5613af10b9933b543dd190b (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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.processing.response;

import com.yahoo.collections.FreezableArrayList;
import com.yahoo.processing.Request;

import java.util.List;

/**
 * A data list backed by an array.
 * This implementation supports subclassing.
 *
 * @author bratseth
 */
public class ArrayDataList<DATATYPE extends Data> extends AbstractDataList<DATATYPE> {

    private final FreezableArrayList<DATATYPE> dataList = new FreezableArrayList<>(true);

    /**
     * Creates a simple data list which does not allow late incoming data
     *
     * @param request the request which created this data list
     */
    protected ArrayDataList(Request request) {
        super(request);
    }

    /**
     * Creates a simple data list which receives incoming data in the given instance
     *
     * @param request      the request which created this data list, never null
     * @param incomingData the recipient of incoming data to this list, never null
     */
    protected ArrayDataList(Request request, IncomingData<DATATYPE> incomingData) {
        this(request, incomingData, true, true);
    }

    /**
     * Creates a simple data list which receives incoming data in the given instance
     *
     * @param request      the request which created this data list, never null
     * @param incomingData the recipient of incoming data to this list, never null
     */
    protected ArrayDataList(Request request, IncomingData<DATATYPE> incomingData, boolean ordered, boolean streamed) {
        super(request, incomingData, ordered, streamed);
    }

    /**
     * Creates a simple data list which does not allow late incoming data
     *
     * @param request the request which created this data list
     */
    public static <DATATYPE extends Data> ArrayDataList<DATATYPE> create(Request request) {
        return new ArrayDataList<>(request);
    }

    /**
     * Creates an instance of this which supports incoming data through the default mechanism (DefaultIncomingData)
     */
    public static <DATATYPE extends Data> ArrayDataList<DATATYPE> createAsync(Request request) {
        DefaultIncomingData<DATATYPE> incomingData = new DefaultIncomingData<>();
        ArrayDataList<DATATYPE> dataList = new ArrayDataList<>(request, incomingData);
        incomingData.assignOwner(dataList);
        return dataList;
    }

    /**
     * Creates an instance of this which supports incoming data through the default mechanism (DefaultIncomingData),
     * and where this data can be rendered in any order.
     */
    public static <DATATYPE extends Data> ArrayDataList<DATATYPE> createAsyncUnordered(Request request) {
        DefaultIncomingData<DATATYPE> incomingData = new DefaultIncomingData<>();
        ArrayDataList<DATATYPE> dataList = new ArrayDataList<>(request, incomingData, false, true);
        incomingData.assignOwner(dataList);
        return dataList;
    }

    /**
     * Creates an instance of this which supports incoming data through the default mechanism (DefaultIncomingData)
     * and where this data cannot be returned to clients until this is completed.
     */
    public static <DATATYPE extends Data> ArrayDataList<DATATYPE> createAsyncNonstreamed(Request request) {
        DefaultIncomingData<DATATYPE> incomingData = new DefaultIncomingData<>();
        ArrayDataList<DATATYPE> dataList = new ArrayDataList<>(request, incomingData, true, false);
        incomingData.assignOwner(dataList);
        return dataList;
    }

    public DATATYPE add(DATATYPE data) {
        dataList.add(data);
        return data;
    }

    /**
     * Returns the data element at index
     */
    public DATATYPE get(int index) {
        return dataList.get(index);
    }

    /**
     * Returns a reference to the list backing this. The list may be modified freely,
     * unless this is frozen. If frozen, the only permissible write operations are those that
     * add new items to the end of the list.
     */
    public List<DATATYPE> asList() {
        return dataList;
    }

    @Override
    public void addDataListener(Runnable runnable) {
        dataList.addListener(runnable);
    }

    /**
     * Irreversibly prevent further changes to the items of this.
     * This allows the processing engine to start streaming the current content of this list back to the
     * client (if applicable).
     * <p>
     * Adding new items to the end of this list is permitted even after freeze.
     * If frozen, those items may be streamed back to the client immediately on add.
     * <p>
     * Calling this on a frozen list has no effect.
     */
    public void freeze() {
        super.freeze();
        dataList.freeze();
    }

}