Skip to content

Commit b5249ea

Browse files
author
Ajay Kannan
committed
Change StructuredQuery builder structure
1 parent c4e01cd commit b5249ea

File tree

5 files changed

+450
-50
lines changed

5 files changed

+450
-50
lines changed

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/EntityQuery.java

Lines changed: 81 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,35 +16,109 @@
1616

1717
package com.google.gcloud.datastore;
1818

19+
import com.google.api.services.datastore.DatastoreV1;
20+
1921
/**
2022
* An implementation of a Google Cloud Datastore entity query that can be constructed by providing
2123
* all the specific query elements.
2224
*
2325
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
2426
* queries</a>
2527
*/
26-
public class EntityQuery extends StructuredQuery<Entity> {
28+
public final class EntityQuery extends StructuredQuery<Entity> {
29+
30+
private static final long serialVersionUID = 2990565454831019471L;
2731

2832
/**
29-
* A StructuredQuery builder for queries that return Entity results.
33+
* A {@code EntityQuery} builder for queries that return Entity results.
3034
*/
31-
public static final class Builder extends StructuredQuery.BaseBuilder<Entity, Builder> {
35+
public static final class Builder extends StructuredQuery.BaseBuilder<Entity> {
36+
37+
private StructuredQuery.BuilderImpl<Entity, Builder> builder;
3238

3339
Builder() {
34-
super(ResultType.ENTITY);
40+
builder = new StructuredQuery.BuilderImpl<>(ResultType.ENTITY);
3541
}
3642

3743
Builder(EntityQuery query) {
38-
super(query);
44+
builder = new StructuredQuery.BuilderImpl<>(query);
45+
}
46+
47+
@Override
48+
Builder mergeFrom(DatastoreV1.Query queryPb) {
49+
builder.mergeFrom(queryPb);
50+
builder.clearProjection();
51+
builder.clearGroupBy();
52+
return this;
53+
}
54+
55+
@Override
56+
public Builder namespace(String namespace) {
57+
builder.namespace(namespace);
58+
return this;
59+
}
60+
61+
@Override
62+
public Builder kind(String kind) {
63+
builder.kind(kind);
64+
return this;
65+
}
66+
67+
@Override
68+
public Builder startCursor(Cursor startCursor) {
69+
builder.startCursor(startCursor);
70+
return this;
71+
}
72+
73+
@Override
74+
public Builder endCursor(Cursor endCursor) {
75+
builder.endCursor(endCursor);
76+
return this;
77+
}
78+
79+
@Override
80+
public Builder offset(int offset) {
81+
builder.offset(offset);
82+
return this;
83+
}
84+
85+
@Override
86+
public Builder limit(Integer limit) {
87+
builder.limit(limit);
88+
return this;
89+
}
90+
91+
@Override
92+
public Builder filter(Filter filter) {
93+
builder.filter(filter);
94+
return this;
95+
}
96+
97+
@Override
98+
public Builder clearOrderBy() {
99+
builder.clearOrderBy();
100+
return this;
101+
}
102+
103+
@Override
104+
public Builder orderBy(OrderBy orderBy, OrderBy... others) {
105+
builder.orderBy(orderBy, others);
106+
return this;
107+
}
108+
109+
@Override
110+
public Builder addOrderBy(OrderBy orderBy, OrderBy... others) {
111+
builder.addOrderBy(orderBy, others);
112+
return this;
39113
}
40114

41115
@Override
42116
public EntityQuery build() {
43-
return new EntityQuery(this);
117+
return new EntityQuery(builder);
44118
}
45119
}
46120

47-
EntityQuery(Builder builder) {
121+
EntityQuery(BuilderImpl<Entity, Builder> builder) {
48122
super(builder);
49123
}
50124

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/KeyQuery.java

Lines changed: 75 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,37 +25,101 @@
2525
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
2626
* queries</a>
2727
*/
28-
public class KeyQuery extends StructuredQuery<Key> {
28+
public final class KeyQuery extends StructuredQuery<Key> {
29+
30+
private static final long serialVersionUID = -746768461459070045L;
2931

3032
/**
31-
* A StructuredQuery builder for queries that return Key results.
33+
* A {@code KeyQuery} builder for queries that return Key results.
3234
*/
33-
public static final class Builder extends StructuredQuery.BaseBuilder<Key, Builder> {
35+
public static final class Builder extends StructuredQuery.BaseBuilder<Key> {
36+
37+
private StructuredQuery.BuilderImpl<Key, Builder> builder;
3438

3539
Builder() {
36-
super(ResultType.KEY);
37-
projection(Projection.property(KEY_PROPERTY_NAME));
40+
builder = new StructuredQuery.BuilderImpl<>(ResultType.KEY);
41+
builder.projection(Projection.property(KEY_PROPERTY_NAME));
3842
}
3943

4044
Builder(KeyQuery query) {
41-
super(query);
45+
builder = new StructuredQuery.BuilderImpl<>(query);
4246
}
4347

4448
@Override
4549
Builder mergeFrom(DatastoreV1.Query queryPb) {
46-
super.mergeFrom(queryPb);
47-
projection(Projection.property(KEY_PROPERTY_NAME));
48-
clearGroupBy();
50+
builder.mergeFrom(queryPb);
51+
builder.projection(Projection.property(KEY_PROPERTY_NAME));
52+
builder.clearGroupBy();
53+
return this;
54+
}
55+
56+
@Override
57+
public Builder namespace(String namespace) {
58+
builder.namespace(namespace);
59+
return this;
60+
}
61+
62+
@Override
63+
public Builder kind(String kind) {
64+
builder.kind(kind);
65+
return this;
66+
}
67+
68+
@Override
69+
public Builder startCursor(Cursor startCursor) {
70+
builder.startCursor(startCursor);
71+
return this;
72+
}
73+
74+
@Override
75+
public Builder endCursor(Cursor endCursor) {
76+
builder.endCursor(endCursor);
77+
return this;
78+
}
79+
80+
@Override
81+
public Builder offset(int offset) {
82+
builder.offset(offset);
83+
return this;
84+
}
85+
86+
@Override
87+
public Builder limit(Integer limit) {
88+
builder.limit(limit);
89+
return this;
90+
}
91+
92+
@Override
93+
public Builder filter(Filter filter) {
94+
builder.filter(filter);
95+
return this;
96+
}
97+
98+
@Override
99+
public Builder clearOrderBy() {
100+
builder.clearOrderBy();
101+
return this;
102+
}
103+
104+
@Override
105+
public Builder orderBy(OrderBy orderBy, OrderBy... others) {
106+
builder.orderBy(orderBy, others);
107+
return this;
108+
}
109+
110+
@Override
111+
public Builder addOrderBy(OrderBy orderBy, OrderBy... others) {
112+
builder.addOrderBy(orderBy, others);
49113
return this;
50114
}
51115

52116
@Override
53117
public KeyQuery build() {
54-
return new KeyQuery(this);
118+
return new KeyQuery(builder);
55119
}
56120
}
57121

58-
KeyQuery(Builder builder) {
122+
KeyQuery(BuilderImpl<Key, Builder> builder) {
59123
super(builder);
60124
}
61125

gcloud-java-datastore/src/main/java/com/google/gcloud/datastore/ProjectionEntityQuery.java

Lines changed: 90 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,77 +16,149 @@
1616

1717
package com.google.gcloud.datastore;
1818

19+
import com.google.api.services.datastore.DatastoreV1.Query;
20+
1921
/**
2022
* An implementation of a Google Cloud Datastore projection entity query that can be constructed by
2123
* providing all the specific query elements.
2224
*
2325
* @see <a href="https://cloud.google.com/appengine/docs/java/datastore/queries">Datastore
2426
* queries</a>
2527
*/
26-
public class ProjectionEntityQuery extends StructuredQuery<ProjectionEntity> {
28+
public final class ProjectionEntityQuery extends StructuredQuery<ProjectionEntity> {
29+
30+
private static final long serialVersionUID = 5488451194542425391L;
2731

2832
/**
29-
* A StructuredQuery builder for queries that return Key results.
33+
* A {@code ProjectionEntityQuery} builder for queries that return Key results.
3034
*/
31-
public static final class Builder extends StructuredQuery.BaseBuilder<ProjectionEntity, Builder> {
35+
public static final class Builder extends StructuredQuery.BaseBuilder<ProjectionEntity> {
36+
37+
private StructuredQuery.BuilderImpl<ProjectionEntity, Builder> builder;
3238

3339
Builder() {
34-
super(ResultType.PROJECTION_ENTITY);
40+
builder = new StructuredQuery.BuilderImpl<>(ResultType.PROJECTION_ENTITY);
3541
}
3642

3743
Builder(ProjectionEntityQuery query) {
38-
super(query);
44+
builder = new StructuredQuery.BuilderImpl<>(query);
45+
}
46+
47+
@Override
48+
Builder mergeFrom(Query queryPb) {
49+
builder.mergeFrom(queryPb);
50+
return this;
51+
}
52+
53+
@Override
54+
public Builder namespace(String namespace) {
55+
builder.namespace(namespace);
56+
return this;
57+
}
58+
59+
@Override
60+
public Builder kind(String kind) {
61+
builder.kind(kind);
62+
return this;
63+
}
64+
65+
@Override
66+
public Builder startCursor(Cursor startCursor) {
67+
builder.startCursor(startCursor);
68+
return this;
69+
}
70+
71+
@Override
72+
public Builder endCursor(Cursor endCursor) {
73+
builder.endCursor(endCursor);
74+
return this;
75+
}
76+
77+
@Override
78+
public Builder offset(int offset) {
79+
builder.offset(offset);
80+
return this;
81+
}
82+
83+
@Override
84+
public Builder limit(Integer limit) {
85+
builder.limit(limit);
86+
return this;
87+
}
88+
89+
@Override
90+
public Builder filter(Filter filter) {
91+
builder.filter(filter);
92+
return this;
93+
}
94+
95+
@Override
96+
public Builder clearOrderBy() {
97+
builder.clearOrderBy();
98+
return this;
3999
}
40100

41101
@Override
102+
public Builder orderBy(OrderBy orderBy, OrderBy... others) {
103+
builder.orderBy(orderBy, others);
104+
return this;
105+
}
106+
107+
@Override
108+
public Builder addOrderBy(OrderBy orderBy, OrderBy... others) {
109+
builder.addOrderBy(orderBy, others);
110+
return this;
111+
}
112+
42113
public Builder clearProjection() {
43-
return super.clearProjection();
114+
builder.clearProjection();
115+
return this;
44116
}
45117

46118
/**
47119
* Sets the query's projection clause (clearing any previously specified Projection settings).
48120
*/
49-
@Override
50121
public Builder projection(Projection projection, Projection... others) {
51-
return super.projection(projection, others);
122+
builder.projection(projection, others);
123+
return this;
52124
}
53125

54126
/**
55127
* Adds one or more projections to the existing projection clause.
56128
*/
57-
@Override
58129
public Builder addProjection(Projection projection, Projection... others) {
59-
return super.addProjection(projection, others);
130+
builder.addProjection(projection, others);
131+
return this;
60132
}
61133

62-
@Override
63134
public Builder clearGroupBy() {
64-
return super.clearGroupBy();
135+
builder.clearGroupBy();
136+
return this;
65137
}
66138

67139
/**
68140
* Sets the query's group by clause (clearing any previously specified GroupBy settings).
69141
*/
70-
@Override
71142
public Builder groupBy(String property, String... others) {
72-
return super.groupBy(property, others);
143+
builder.groupBy(property, others);
144+
return this;
73145
}
74146

75147
/**
76148
* Adds one or more properties to the existing group by clause.
77149
*/
78-
@Override
79150
public Builder addGroupBy(String property, String... others) {
80-
return super.addGroupBy(property, others);
151+
builder.addGroupBy(property, others);
152+
return this;
81153
}
82154

83155
@Override
84156
public ProjectionEntityQuery build() {
85-
return new ProjectionEntityQuery(this);
157+
return new ProjectionEntityQuery(builder);
86158
}
87159
}
88160

89-
ProjectionEntityQuery(BaseBuilder<ProjectionEntity, ?> builder) {
161+
ProjectionEntityQuery(BuilderImpl<ProjectionEntity, Builder> builder) {
90162
super(builder);
91163
}
92164

0 commit comments

Comments
 (0)