Skip to content

Commit 5b5f670

Browse files
Converting all Datastore deserialized enum types to StringEnumValue
1 parent 67af4bf commit 5b5f670

File tree

2 files changed

+140
-16
lines changed

2 files changed

+140
-16
lines changed

google-cloud-core/src/main/java/com/google/cloud/StringEnumValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public boolean equals(Object that) {
5353
if (this == that) {
5454
return true;
5555
}
56-
if (!(that instanceof StringEnumValue)) {
56+
if (!(getClass().equals(that.getClass()))) {
5757
return false;
5858
}
5959
StringEnumValue thatEnumValue = (StringEnumValue) that;

google-cloud-datastore/src/main/java/com/google/cloud/datastore/StructuredQuery.java

Lines changed: 139 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
import static com.google.cloud.datastore.StringValue.of;
2626
import static com.google.common.base.Preconditions.checkNotNull;
2727

28+
import com.google.api.core.ApiFunction;
29+
import com.google.cloud.StringEnumType;
30+
import com.google.cloud.StringEnumValue;
2831
import com.google.common.base.MoreObjects;
2932
import com.google.common.base.MoreObjects.ToStringHelper;
3033
import com.google.common.base.Preconditions;
3134
import com.google.common.collect.ImmutableList;
32-
3335
import java.io.Serializable;
3436
import java.util.Arrays;
3537
import java.util.Collections;
@@ -124,8 +126,26 @@ public static final class CompositeFilter extends Filter {
124126
private final Operator operator;
125127
private final ImmutableList<Filter> filters;
126128

127-
enum Operator {
128-
AND;
129+
static final class Operator extends StringEnumValue {
130+
private static final long serialVersionUID = -4806600805752138487L;
131+
132+
private Operator(String constant) {
133+
super(constant);
134+
}
135+
136+
private static final ApiFunction<String, Operator> CONSTRUCTOR =
137+
new ApiFunction<String, Operator>() {
138+
@Override
139+
public Operator apply(String constant) {
140+
return new Operator(constant);
141+
}
142+
};
143+
144+
private static final StringEnumType<Operator> type = new StringEnumType(
145+
Operator.class,
146+
CONSTRUCTOR);
147+
148+
static final Operator AND = type.createAndRegister("AND");
129149

130150
com.google.datastore.v1.CompositeFilter.Operator toPb() {
131151
return com.google.datastore.v1.CompositeFilter.Operator.valueOf(name());
@@ -134,6 +154,28 @@ com.google.datastore.v1.CompositeFilter.Operator toPb() {
134154
static Operator fromPb(com.google.datastore.v1.CompositeFilter.Operator operatorPb) {
135155
return valueOf(operatorPb.name());
136156
}
157+
158+
/**
159+
* Get the Operator for the given String constant, and throw an exception if the constant is
160+
* not recognized.
161+
*/
162+
static Operator valueOfStrict(String constant) {
163+
return type.valueOfStrict(constant);
164+
}
165+
166+
/**
167+
* Get the Operator for the given String constant, and allow unrecognized values.
168+
*/
169+
static Operator valueOf(String constant) {
170+
return type.valueOf(constant);
171+
}
172+
173+
/**
174+
* Return the known values for Operator.
175+
*/
176+
static Operator[] values() {
177+
return type.values();
178+
}
137179
}
138180

139181
private CompositeFilter(Operator operator, Filter first, Filter... other) {
@@ -170,7 +212,7 @@ public boolean equals(Object obj) {
170212
return false;
171213
}
172214
CompositeFilter other = (CompositeFilter) obj;
173-
return operator == other.operator
215+
return operator.equals(other.operator)
174216
&& filters.equals(other.filters);
175217
}
176218

@@ -214,13 +256,33 @@ public static final class PropertyFilter extends Filter {
214256
private final Operator operator;
215257
private final Value<?> value;
216258

217-
enum Operator {
218-
LESS_THAN,
219-
LESS_THAN_OR_EQUAL,
220-
GREATER_THAN,
221-
GREATER_THAN_OR_EQUAL,
222-
EQUAL,
223-
HAS_ANCESTOR;
259+
static final class Operator extends StringEnumValue {
260+
private static final long serialVersionUID = 4105765859141068029L;
261+
262+
private Operator(String constant) {
263+
super(constant);
264+
}
265+
266+
private static final ApiFunction<String, Operator> CONSTRUCTOR =
267+
new ApiFunction<String, Operator>() {
268+
@Override
269+
public Operator apply(String constant) {
270+
return new Operator(constant);
271+
}
272+
};
273+
274+
private static final StringEnumType<Operator> type = new StringEnumType(
275+
Operator.class,
276+
CONSTRUCTOR);
277+
278+
static final Operator LESS_THAN = type.createAndRegister("LESS_THAN");
279+
static final Operator LESS_THAN_OR_EQUAL = type
280+
.createAndRegister("LESS_THAN_OR_EQUAL");
281+
static final Operator GREATER_THAN = type.createAndRegister("GREATER_THAN");
282+
static final Operator GREATER_THAN_OR_EQUAL = type
283+
.createAndRegister("GREATER_THAN_OR_EQUAL");
284+
static final Operator EQUAL = type.createAndRegister("EQUAL");
285+
static final Operator HAS_ANCESTOR = type.createAndRegister("HAS_ANCESTOR");
224286

225287
com.google.datastore.v1.PropertyFilter.Operator toPb() {
226288
return com.google.datastore.v1.PropertyFilter.Operator.valueOf(name());
@@ -229,6 +291,28 @@ com.google.datastore.v1.PropertyFilter.Operator toPb() {
229291
static Operator fromPb(com.google.datastore.v1.PropertyFilter.Operator operatorPb) {
230292
return valueOf(operatorPb.name());
231293
}
294+
295+
/**
296+
* Get the Operator for the given String constant, and throw an exception if the constant is
297+
* not recognized.
298+
*/
299+
static Operator valueOfStrict(String constant) {
300+
return type.valueOfStrict(constant);
301+
}
302+
303+
/**
304+
* Get the Operator for the given String constant, and allow unrecognized values.
305+
*/
306+
static Operator valueOf(String constant) {
307+
return type.valueOf(constant);
308+
}
309+
310+
/**
311+
* Return the known values for Operator.
312+
*/
313+
static Operator[] values() {
314+
return type.values();
315+
}
232316
}
233317

234318
private PropertyFilter(String property, Operator operator, Value<?> value) {
@@ -268,7 +352,7 @@ public boolean equals(Object obj) {
268352
}
269353
PropertyFilter other = (PropertyFilter) obj;
270354
return property.equals(other.property)
271-
&& operator == other.operator
355+
&& operator.equals(other.operator)
272356
&& Objects.equals(value, other.value);
273357
}
274358

@@ -461,9 +545,27 @@ public static final class OrderBy implements Serializable {
461545
private final String property;
462546
private final Direction direction;
463547

464-
public enum Direction {
548+
public static final class Direction extends StringEnumValue {
549+
private static final long serialVersionUID = -6938125060419556331L;
550+
551+
private Direction(String constant) {
552+
super(constant);
553+
}
554+
555+
private static final ApiFunction<String, Direction> CONSTRUCTOR =
556+
new ApiFunction<String, Direction>() {
557+
@Override
558+
public Direction apply(String constant) {
559+
return new Direction(constant);
560+
}
561+
};
465562

466-
ASCENDING, DESCENDING;
563+
private static final StringEnumType<Direction> type = new StringEnumType(
564+
Direction.class,
565+
CONSTRUCTOR);
566+
567+
public static final Direction ASCENDING = type.createAndRegister("ASCENDING");
568+
public static final Direction DESCENDING = type.createAndRegister("DESCENDING");
467569

468570
com.google.datastore.v1.PropertyOrder.Direction toPb() {
469571
return com.google.datastore.v1.PropertyOrder.Direction.valueOf(name());
@@ -472,6 +574,28 @@ com.google.datastore.v1.PropertyOrder.Direction toPb() {
472574
static Direction fromPb(com.google.datastore.v1.PropertyOrder.Direction directionPb) {
473575
return valueOf(directionPb.name());
474576
}
577+
578+
/**
579+
* Get the Direction for the given String constant, and throw an exception if the constant is
580+
* not recognized.
581+
*/
582+
static Direction valueOfStrict(String constant) {
583+
return type.valueOfStrict(constant);
584+
}
585+
586+
/**
587+
* Get the Direction for the given String constant, and allow unrecognized values.
588+
*/
589+
static Direction valueOf(String constant) {
590+
return type.valueOf(constant);
591+
}
592+
593+
/**
594+
* Return the known values for Direction.
595+
*/
596+
static Direction[] values() {
597+
return type.values();
598+
}
475599
}
476600

477601
public OrderBy(String property, Direction direction) {
@@ -494,7 +618,7 @@ public boolean equals(Object obj) {
494618
}
495619
OrderBy other = (OrderBy) obj;
496620
return property.equals(other.property)
497-
&& direction == other.direction;
621+
&& direction.equals(other.direction);
498622
}
499623

500624

0 commit comments

Comments
 (0)