m-chrzan.xyz
aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/pl/edu/mimuw/cloudatlas/interpreter/ResultList.java
blob: c79a98476f52a815c49674491b1d380a33cc331b (plain)
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
131
132
133
134
135
136
137
138
139
package pl.edu.mimuw.cloudatlas.interpreter;

import java.util.ArrayList;
import java.util.List;

import pl.edu.mimuw.cloudatlas.model.Type;
import pl.edu.mimuw.cloudatlas.model.TypePrimitive;
import pl.edu.mimuw.cloudatlas.model.TypeCollection;
import pl.edu.mimuw.cloudatlas.model.Value;
import pl.edu.mimuw.cloudatlas.model.ValueBoolean;
import pl.edu.mimuw.cloudatlas.model.ValueList;

class ResultList extends Result {
    private final List<Value> list;

    public ResultList(List<Value> list) {
        this.list = list;
    }

    @Override
    protected ResultList binaryOperationTyped(BinaryOperation operation, ResultSingle right) {
        List<Value> results = new ArrayList<Value>();

        for (Value value : list) {
            results.add(operation.perform(value, right.getValue()));
        }

        return new ResultList(results);
    }

    protected Result binaryOperationTyped(BinaryOperation operation, ResultList right) {
        throw new UnsupportedOperationException("Binary operation not supported on two ResultLists");
    }

    protected Result binaryOperationTyped(BinaryOperation operation, ResultColumn right) {
        throw new UnsupportedOperationException("Binary operation not supported on ResultList and ResultColumn");
    }

    @Override
    public ResultList unaryOperation(UnaryOperation operation) {
        List<Value> results = new ArrayList<Value>();

        for (Value value : list) {
            results.add(operation.perform(value));
        }
        return new ResultList(results);
    }

    @Override
    protected Result callMe(BinaryOperation operation, Result left) {
        return left.binaryOperationTyped(operation, this);
    }

    @Override
    public Value getValue() {
        throw new UnsupportedOperationException("ResultList: Not a ResultSingle.");
    }

    @Override
    public ValueList getList() {
        return new ValueList(list, TypeCollection.computeElementType(list));
    }

    @Override
    public ValueList getColumn() {
        throw new UnsupportedOperationException("Not a ResultColumn.");
    }

    @Override
    public ResultSingle aggregationOperation(AggregationOperation operation) {
        return new ResultSingle(operation.perform(getList()));
    }

    @Override
    public Result transformOperation(TransformOperation operation) {
        return new ResultList(operation.perform(getList()));
    }

    @Override
    public Result filterNulls() {
        throw new UnsupportedOperationException("Operation filterNulls not supported yet.");
    }

    @Override
    public Result first(int size) {
        List<Value> subList = list.subList(0, Math.min(size, list.size()));
        return new ResultSingle(new ValueList(subList, TypeCollection.computeElementType(subList)));
    }

    @Override
    public Result last(int size) {
        List<Value> subList = list.subList(
                Math.max(0, list.size() - size),
                list.size()
        );
        return new ResultSingle(new ValueList(subList, TypeCollection.computeElementType(subList)));
    }

    @Override
    public Result random(int size) {
        return new ResultSingle(
            randomList(
                new ValueList(
                    list,
                    TypeCollection.computeElementType(list)
                ),
                size
            )
        );
    }

    @Override
    public ResultList convertTo(Type to) {
        List<Value> results = new ArrayList<Value>();

        for (Value value : list) {
            results.add(value.convertTo(to));
        }

        return new ResultList(results);
    }

    @Override
    public ResultSingle isNull() {
        return new ResultSingle(new ValueBoolean(true));
    }

    @Override
    public Type getType() {
        Type type = TypePrimitive.NULL;
        for (Value value : list) {
            if (value.getType() != TypePrimitive.NULL) {
                type = value.getType();
            }
        }

        return type;
    }
}