/
DefaultColumnQueryBuilder.java
162 lines (136 loc) · 5.01 KB
/
DefaultColumnQueryBuilder.java
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
*
* Copyright (c) 2022 Contributors to the Eclipse Foundation
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Apache License v2.0 which accompanies this distribution.
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Apache License v2.0 is available at http://www.opensource.org/licenses/apache2.0.php.
*
* You may elect to redistribute this code under either of these licenses.
*
* Contributors:
*
* Otavio Santana
*
*/
package org.eclipse.jnosql.communication.column;
import jakarta.data.Sort;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.stream.Stream;
import static java.util.Objects.requireNonNull;
class DefaultColumnQueryBuilder implements ColumnQuery.ColumnQueryBuilder {
private final List<String> columns = new ArrayList<>();
private final List<Sort<?>> sorts = new ArrayList<>();
private String documentCollection;
private ColumnCondition condition;
private long skip;
private long limit;
@Override
public ColumnQuery.ColumnQueryBuilder select(String column) {
Objects.requireNonNull(column, "column is required");
this.columns.add(column);
return this;
}
@Override
public ColumnQuery.ColumnQueryBuilder select(String... columns) {
Consumer<String> validNull = d -> requireNonNull(d, "there is null column in the query");
Consumer<String> consume = this.columns::add;
Stream.of(columns).forEach(validNull.andThen(consume));
return this;
}
@Override
public ColumnQuery.ColumnQueryBuilder sort(Sort sort) {
Objects.requireNonNull(sort, "sort is required");
this.sorts.add(sort);
return this;
}
@Override
public ColumnQuery.ColumnQueryBuilder sort(Sort... sorts) {
Consumer<Sort> validNull = d -> requireNonNull(d, "there is null document in the query");
Consumer<Sort> consume = this.sorts::add;
Stream.of(sorts).forEach(validNull.andThen(consume));
return this;
}
@Override
public ColumnQuery.ColumnQueryBuilder from(String documentCollection) {
Objects.requireNonNull(documentCollection, "documentCollection is required");
this.documentCollection = documentCollection;
return this;
}
@Override
public ColumnQuery.ColumnQueryBuilder where(ColumnCondition condition) {
Objects.requireNonNull(condition, "condition is required");
this.condition = condition;
return this;
}
@Override
public ColumnQuery.ColumnQueryBuilder skip(long skip) {
if (skip < 0) {
throw new IllegalArgumentException("The skip should not be negative, skip: " + skip);
}
this.skip = skip;
return this;
}
@Override
public ColumnQuery.ColumnQueryBuilder limit(long limit) {
if (limit < 0) {
throw new IllegalArgumentException("The limit should not be negative, limit: " + limit);
}
this.limit = limit;
return this;
}
@Override
public ColumnQuery build() {
if (Objects.isNull(documentCollection)) {
throw new IllegalArgumentException("The document collection is mandatory to build");
}
return new DefaultColumnQuery(limit, skip, documentCollection,
columns, sorts, condition);
}
@Override
public Stream<ColumnEntity> getResult(ColumnManager manager) {
Objects.requireNonNull(manager, "manager is required");
return manager.select(build());
}
@Override
public Optional<ColumnEntity> getSingleResult(ColumnManager manager) {
Objects.requireNonNull(manager, "manager is required");
return manager.singleResult(build());
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
DefaultColumnQueryBuilder that = (DefaultColumnQueryBuilder) o;
return skip == that.skip
&& limit == that.limit
&& Objects.equals(columns, that.columns)
&& Objects.equals(sorts, that.sorts)
&& Objects.equals(documentCollection, that.documentCollection)
&& Objects.equals(condition, that.condition);
}
@Override
public int hashCode() {
return Objects.hash(columns, sorts, documentCollection, condition, skip, limit);
}
@Override
public String toString() {
return "DefaultColumnQueryBuilder{" +
"columns=" + columns +
", sorts=" + sorts +
", documentCollection='" + documentCollection + '\'' +
", condition=" + condition +
", skip=" + skip +
", limit=" + limit +
'}';
}
}