Skip to content

Commit c41ad14

Browse files
committed
Refactor compute operations
- Add Type enum to OperationId and type() getter - Replace instanceof with switch on type() - Add better javadoc to Operation - Remove final from Operation, make hashCode and equals final
1 parent ef98269 commit c41ad14

File tree

10 files changed

+128
-76
lines changed

10 files changed

+128
-76
lines changed

gcloud-java-compute/src/main/java/com/google/gcloud/compute/Compute.java

Lines changed: 30 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ abstract class ListFilter implements Serializable {
288288

289289
enum ComparisonOperator {
290290
/**
291-
* Defines an equality filter.
291+
* Defines an equals filter.
292292
*/
293293
EQ,
294294

@@ -340,11 +340,11 @@ class DiskTypeFilter extends ListFilter {
340340
}
341341

342342
/**
343-
* Returns an equality filter for the given field and string value. For string fields,
343+
* Returns an equals filter for the given field and string value. For string fields,
344344
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
345345
* match the entire field.
346346
*
347-
* @see <a href="https://github.com/google/re2">RE2</a>
347+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
348348
*/
349349
public static DiskTypeFilter equals(DiskTypeField field, String value) {
350350
return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -355,14 +355,14 @@ public static DiskTypeFilter equals(DiskTypeField field, String value) {
355355
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
356356
* match the entire field.
357357
*
358-
* @see <a href="https://github.com/google/re2">RE2</a>
358+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
359359
*/
360360
public static DiskTypeFilter notEquals(DiskTypeField field, String value) {
361361
return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
362362
}
363363

364364
/**
365-
* Returns an equality filter for the given field and long value.
365+
* Returns an equals filter for the given field and long value.
366366
*/
367367
public static DiskTypeFilter equals(DiskTypeField field, long value) {
368368
return new DiskTypeFilter(checkNotNull(field), ComparisonOperator.EQ, value);
@@ -388,11 +388,11 @@ class MachineTypeFilter extends ListFilter {
388388
}
389389

390390
/**
391-
* Returns an equality filter for the given field and string value. For string fields,
391+
* Returns an equals filter for the given field and string value. For string fields,
392392
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
393393
* match the entire field.
394394
*
395-
* @see <a href="https://github.com/google/re2">RE2</a>
395+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
396396
*/
397397
public static MachineTypeFilter equals(MachineTypeField field, String value) {
398398
return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -403,14 +403,14 @@ public static MachineTypeFilter equals(MachineTypeField field, String value) {
403403
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
404404
* match the entire field.
405405
*
406-
* @see <a href="https://github.com/google/re2">RE2</a>
406+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
407407
*/
408408
public static MachineTypeFilter notEquals(MachineTypeField field, String value) {
409409
return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
410410
}
411411

412412
/**
413-
* Returns an equality filter for the given field and long value.
413+
* Returns an equals filter for the given field and long value.
414414
*/
415415
public static MachineTypeFilter equals(MachineTypeField field, long value) {
416416
return new MachineTypeFilter(checkNotNull(field), ComparisonOperator.EQ, value);
@@ -436,11 +436,11 @@ class RegionFilter extends ListFilter {
436436
}
437437

438438
/**
439-
* Returns an equality filter for the given field and string value. For string fields,
439+
* Returns an equals filter for the given field and string value. For string fields,
440440
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
441441
* match the entire field.
442442
*
443-
* @see <a href="https://github.com/google/re2">RE2</a>
443+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
444444
*/
445445
public static RegionFilter equals(RegionField field, String value) {
446446
return new RegionFilter(checkNotNull(field), ComparisonOperator.EQ, value);
@@ -451,7 +451,7 @@ public static RegionFilter equals(RegionField field, String value) {
451451
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
452452
* match the entire field.
453453
*
454-
* @see <a href="https://github.com/google/re2">RE2</a>
454+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
455455
*/
456456
public static RegionFilter notEquals(RegionField field, String value) {
457457
return new RegionFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
@@ -470,11 +470,11 @@ class ZoneFilter extends ListFilter {
470470
}
471471

472472
/**
473-
* Returns an equality filter for the given field and string value. For string fields,
473+
* Returns an equals filter for the given field and string value. For string fields,
474474
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
475475
* match the entire field.
476476
*
477-
* @see <a href="https://github.com/google/re2">RE2</a>
477+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
478478
*/
479479
public static ZoneFilter equals(ZoneField field, String value) {
480480
return new ZoneFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -485,7 +485,7 @@ public static ZoneFilter equals(ZoneField field, String value) {
485485
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
486486
* match the entire field.
487487
*
488-
* @see <a href="https://github.com/google/re2">RE2</a>
488+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
489489
*/
490490
public static ZoneFilter notEquals(ZoneField field, String value) {
491491
return new ZoneFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
@@ -504,11 +504,11 @@ class OperationFilter extends ListFilter {
504504
}
505505

506506
/**
507-
* Returns an equality filter for the given field and string value. For string fields,
507+
* Returns an equals filter for the given field and string value. For string fields,
508508
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
509509
* match the entire field.
510510
*
511-
* @see <a href="https://github.com/google/re2">RE2</a>
511+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
512512
*/
513513
public static OperationFilter equals(OperationField field, String value) {
514514
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -519,14 +519,14 @@ public static OperationFilter equals(OperationField field, String value) {
519519
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
520520
* match the entire field.
521521
*
522-
* @see <a href="https://github.com/google/re2">RE2</a>
522+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
523523
*/
524524
public static OperationFilter notEquals(OperationField field, String value) {
525525
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, checkNotNull(value));
526526
}
527527

528528
/**
529-
* Returns an equality filter for the given field and long value.
529+
* Returns an equals filter for the given field and long value.
530530
*/
531531
public static OperationFilter equals(OperationField field, long value) {
532532
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, value);
@@ -538,20 +538,6 @@ public static OperationFilter equals(OperationField field, long value) {
538538
public static OperationFilter notEquals(OperationField field, long value) {
539539
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, value);
540540
}
541-
542-
/**
543-
* Returns an equality filter for the given field and integer value.
544-
*/
545-
public static OperationFilter equals(OperationField field, int value) {
546-
return new OperationFilter(checkNotNull(field), ComparisonOperator.EQ, value);
547-
}
548-
549-
/**
550-
* Returns a not-equals filter for the given field and integer value.
551-
*/
552-
public static OperationFilter notEquals(OperationField field, int value) {
553-
return new OperationFilter(checkNotNull(field), ComparisonOperator.NE, value);
554-
}
555541
}
556542

557543
/**
@@ -595,7 +581,7 @@ public static DiskTypeListOption filter(DiskTypeFilter filter) {
595581
}
596582

597583
/**
598-
* Returns an option to specify the maximum number of disk types to be returned.
584+
* Returns an option to specify the maximum number of disk types returned per page.
599585
*/
600586
public static DiskTypeListOption pageSize(long pageSize) {
601587
return new DiskTypeListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -640,7 +626,7 @@ public static DiskTypeAggregatedListOption filter(DiskTypeFilter filter) {
640626
}
641627

642628
/**
643-
* Returns an option to specify the maximum number of disk types to be returned.
629+
* Returns an option to specify the maximum number of disk types returned per page.
644630
*/
645631
public static DiskTypeAggregatedListOption pageSize(long pageSize) {
646632
return new DiskTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -695,7 +681,7 @@ public static MachineTypeListOption filter(MachineTypeFilter filter) {
695681
}
696682

697683
/**
698-
* Returns an option to specify the maximum number of machine types to be returned.
684+
* Returns an option to specify the maximum number of machine types returned per page.
699685
*/
700686
public static MachineTypeListOption pageSize(long pageSize) {
701687
return new MachineTypeListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -740,7 +726,7 @@ public static MachineTypeAggregatedListOption filter(MachineTypeFilter filter) {
740726
}
741727

742728
/**
743-
* Returns an option to specify the maximum number of machine types to be returned.
729+
* Returns an option to specify the maximum number of machine types returned per page.
744730
*/
745731
public static MachineTypeAggregatedListOption pageSize(long pageSize) {
746732
return new MachineTypeAggregatedListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -795,7 +781,7 @@ public static RegionListOption filter(RegionFilter filter) {
795781
}
796782

797783
/**
798-
* Returns an option to specify the maximum number of regions to be returned.
784+
* Returns an option to specify the maximum number of regions returned per page.
799785
*/
800786
public static RegionListOption pageSize(long pageSize) {
801787
return new RegionListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -862,7 +848,7 @@ public static ZoneListOption filter(ZoneFilter filter) {
862848
}
863849

864850
/**
865-
* Returns an option to specify the maximum number of zones to be returned.
851+
* Returns an option to specify the maximum number of zones returned per page.
866852
*/
867853
public static ZoneListOption pageSize(long pageSize) {
868854
return new ZoneListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -951,7 +937,7 @@ public static OperationListOption filter(OperationFilter filter) {
951937
}
952938

953939
/**
954-
* Returns an option to specify the maximum number of operations to be returned.
940+
* Returns an option to specify the maximum number of operations returned per page.
955941
*/
956942
public static OperationListOption pageSize(long pageSize) {
957943
return new OperationListOption(ComputeRpc.Option.MAX_RESULTS, pageSize);
@@ -1090,14 +1076,16 @@ public static OperationListOption fields(OperationField... fields) {
10901076
Page<Operation> listGlobalOperations(OperationListOption... options);
10911077

10921078
/**
1093-
* Lists the operations in the provided region.
1079+
* Lists the operations for the provided region. These are operations that create/modify/delete
1080+
* resources that live in a region (e.g. subnetworks).
10941081
*
10951082
* @throws ComputeException upon failure
10961083
*/
10971084
Page<Operation> listRegionOperations(String region, OperationListOption... options);
10981085

10991086
/**
1100-
* Lists the operations in the provided zone.
1087+
* Lists the operations for the provided zone. These are operations that create/modify/delete
1088+
* resources that live in a zone (e.g. instances).
11011089
*
11021090
* @throws ComputeException upon failure
11031091
*/

gcloud-java-compute/src/main/java/com/google/gcloud/compute/ComputeImpl.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -533,16 +533,17 @@ public Operation get(final OperationId operationId, OperationOption... options)
533533
runWithRetries(new Callable<com.google.api.services.compute.model.Operation>() {
534534
@Override
535535
public com.google.api.services.compute.model.Operation call() {
536-
if (operationId instanceof RegionOperationId) {
537-
RegionOperationId regionOperationId = (RegionOperationId) operationId;
538-
return computeRpc.getRegionOperation(regionOperationId.region(),
539-
regionOperationId.operation(), optionsMap);
540-
} else if (operationId instanceof ZoneOperationId) {
541-
ZoneOperationId zoneOperationId = (ZoneOperationId) operationId;
542-
return computeRpc.getZoneOperation(zoneOperationId.zone(),
543-
zoneOperationId.operation(), optionsMap);
544-
} else {
545-
return computeRpc.getGlobalOperation(operationId.operation(), optionsMap);
536+
switch (operationId.type()) {
537+
case REGION:
538+
RegionOperationId regionOperationId = (RegionOperationId) operationId;
539+
return computeRpc.getRegionOperation(regionOperationId.region(),
540+
regionOperationId.operation(), optionsMap);
541+
case ZONE:
542+
ZoneOperationId zoneOperationId = (ZoneOperationId) operationId;
543+
return computeRpc.getZoneOperation(zoneOperationId.zone(),
544+
zoneOperationId.operation(), optionsMap);
545+
default:
546+
return computeRpc.getGlobalOperation(operationId.operation(), optionsMap);
546547
}
547548
}
548549
}, options().retryParams(), EXCEPTION_HANDLER);
@@ -660,16 +661,17 @@ public boolean delete(final OperationId operation) {
660661
return runWithRetries(new Callable<Boolean>() {
661662
@Override
662663
public Boolean call() {
663-
if (operation instanceof RegionOperationId) {
664-
RegionOperationId regionOperationId = (RegionOperationId) operation;
665-
return computeRpc.deleteRegionOperation(regionOperationId.region(),
666-
regionOperationId.operation());
667-
} else if (operation instanceof ZoneOperationId) {
668-
ZoneOperationId zoneOperationId = (ZoneOperationId) operation;
669-
return computeRpc.deleteZoneOperation(zoneOperationId.zone(),
670-
zoneOperationId.operation());
671-
} else {
672-
return computeRpc.deleteGlobalOperation(operation.operation());
664+
switch (operation.type()) {
665+
case REGION:
666+
RegionOperationId regionOperationId = (RegionOperationId) operation;
667+
return computeRpc.deleteRegionOperation(regionOperationId.region(),
668+
regionOperationId.operation());
669+
case ZONE:
670+
ZoneOperationId zoneOperationId = (ZoneOperationId) operation;
671+
return computeRpc.deleteZoneOperation(zoneOperationId.zone(),
672+
zoneOperationId.operation());
673+
default:
674+
return computeRpc.deleteGlobalOperation(operation.operation());
673675
}
674676
}
675677
}, options().retryParams(), EXCEPTION_HANDLER);

gcloud-java-compute/src/main/java/com/google/gcloud/compute/GlobalOperationId.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ private GlobalOperationId(String project, String operation) {
4040
this.operation = checkNotNull(operation);
4141
}
4242

43+
@Override
44+
public Type type() {
45+
return Type.GLOBAL;
46+
}
47+
4348
@Override
4449
public String operation() {
4550
return operation;

0 commit comments

Comments
 (0)