Skip to content

Commit 6f9ff34

Browse files
committed
Refactor Compute service class
- Rename startPageToken option to pageToken - Add static factory methods from functional objects fromPb functions - Add tests for get methods that return null
1 parent e56cb4b commit 6f9ff34

3 files changed

Lines changed: 165 additions & 71 deletions

File tree

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

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -799,11 +799,11 @@ class AddressFilter extends ListFilter {
799799
}
800800

801801
/**
802-
* Returns an equality filter for the given field and string value. For string fields,
802+
* Returns an equals filter for the given field and string value. For string fields,
803803
* {@code value} is interpreted as a regular expression using RE2 syntax. {@code value} must
804804
* match the entire field.
805805
*
806-
* @see <a href="https://github.com/google/re2">RE2</a>
806+
* @see <a href="https://github.com/google/re2/wiki/Syntax">RE2</a>
807807
*/
808808
public static AddressFilter equals(AddressField field, String value) {
809809
return new AddressFilter(checkNotNull(field), ComparisonOperator.EQ, checkNotNull(value));
@@ -855,7 +855,7 @@ private DiskTypeListOption(ComputeRpc.Option option, Object value) {
855855
}
856856

857857
/**
858-
* Returns an option to specify a filter to the disk types being listed.
858+
* Returns an option to specify a filter on the disk types being listed.
859859
*/
860860
public static DiskTypeListOption filter(DiskTypeFilter filter) {
861861
return new DiskTypeListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -871,7 +871,7 @@ public static DiskTypeListOption pageSize(long pageSize) {
871871
/**
872872
* Returns an option to specify the page token from which to start listing disk types.
873873
*/
874-
public static DiskTypeListOption startPageToken(String pageToken) {
874+
public static DiskTypeListOption pageToken(String pageToken) {
875875
return new DiskTypeListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
876876
}
877877

@@ -900,7 +900,7 @@ private DiskTypeAggregatedListOption(ComputeRpc.Option option, Object value) {
900900
}
901901

902902
/**
903-
* Returns an option to specify a filter to the disk types being listed.
903+
* Returns an option to specify a filter on the disk types being listed.
904904
*/
905905
public static DiskTypeAggregatedListOption filter(DiskTypeFilter filter) {
906906
return new DiskTypeAggregatedListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -916,7 +916,7 @@ public static DiskTypeAggregatedListOption pageSize(long pageSize) {
916916
/**
917917
* Returns an option to specify the page token from which to start listing disk types.
918918
*/
919-
public static DiskTypeAggregatedListOption startPageToken(String pageToken) {
919+
public static DiskTypeAggregatedListOption pageToken(String pageToken) {
920920
return new DiskTypeAggregatedListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
921921
}
922922
}
@@ -955,7 +955,7 @@ private MachineTypeListOption(ComputeRpc.Option option, Object value) {
955955
}
956956

957957
/**
958-
* Returns an option to specify a filter to the machine types being listed.
958+
* Returns an option to specify a filter on the machine types being listed.
959959
*/
960960
public static MachineTypeListOption filter(MachineTypeFilter filter) {
961961
return new MachineTypeListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -971,7 +971,7 @@ public static MachineTypeListOption pageSize(long pageSize) {
971971
/**
972972
* Returns an option to specify the page token from which to start listing machine types.
973973
*/
974-
public static MachineTypeListOption startPageToken(String pageToken) {
974+
public static MachineTypeListOption pageToken(String pageToken) {
975975
return new MachineTypeListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
976976
}
977977

@@ -1000,7 +1000,7 @@ private MachineTypeAggregatedListOption(ComputeRpc.Option option, Object value)
10001000
}
10011001

10021002
/**
1003-
* Returns an option to specify a filter to the machine types being listed.
1003+
* Returns an option to specify a filter on the machine types being listed.
10041004
*/
10051005
public static MachineTypeAggregatedListOption filter(MachineTypeFilter filter) {
10061006
return new MachineTypeAggregatedListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -1016,7 +1016,7 @@ public static MachineTypeAggregatedListOption pageSize(long pageSize) {
10161016
/**
10171017
* Returns an option to specify the page token from which to start listing machine types.
10181018
*/
1019-
public static MachineTypeAggregatedListOption startPageToken(String pageToken) {
1019+
public static MachineTypeAggregatedListOption pageToken(String pageToken) {
10201020
return new MachineTypeAggregatedListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
10211021
}
10221022
}
@@ -1055,7 +1055,7 @@ private RegionListOption(ComputeRpc.Option option, Object value) {
10551055
}
10561056

10571057
/**
1058-
* Returns an option to specify a filter to the regions being listed.
1058+
* Returns an option to specify a filter on the regions being listed.
10591059
*/
10601060
public static RegionListOption filter(RegionFilter filter) {
10611061
return new RegionListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -1071,7 +1071,7 @@ public static RegionListOption pageSize(long pageSize) {
10711071
/**
10721072
* Returns an option to specify the page token from which to start listing regions.
10731073
*/
1074-
public static RegionListOption startPageToken(String pageToken) {
1074+
public static RegionListOption pageToken(String pageToken) {
10751075
return new RegionListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
10761076
}
10771077

@@ -1122,7 +1122,7 @@ private ZoneListOption(ComputeRpc.Option option, Object value) {
11221122
}
11231123

11241124
/**
1125-
* Returns an option to specify a filter to the zones being listed.
1125+
* Returns an option to specify a filter on the zones being listed.
11261126
*/
11271127
public static ZoneListOption filter(ZoneFilter filter) {
11281128
return new ZoneListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -1138,7 +1138,7 @@ public static ZoneListOption pageSize(long pageSize) {
11381138
/**
11391139
* Returns an option to specify the page token from which to start listing zones.
11401140
*/
1141-
public static ZoneListOption startPageToken(String pageToken) {
1141+
public static ZoneListOption pageToken(String pageToken) {
11421142
return new ZoneListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
11431143
}
11441144

@@ -1211,7 +1211,7 @@ private OperationListOption(ComputeRpc.Option option, Object value) {
12111211
}
12121212

12131213
/**
1214-
* Returns an option to specify a filter to the operations being listed.
1214+
* Returns an option to specify a filter on the operations being listed.
12151215
*/
12161216
public static OperationListOption filter(OperationFilter filter) {
12171217
return new OperationListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -1227,7 +1227,7 @@ public static OperationListOption pageSize(long pageSize) {
12271227
/**
12281228
* Returns an option to specify the page token from which to start listing operations.
12291229
*/
1230-
public static OperationListOption startPageToken(String pageToken) {
1230+
public static OperationListOption pageToken(String pageToken) {
12311231
return new OperationListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
12321232
}
12331233

@@ -1278,7 +1278,7 @@ private AddressListOption(ComputeRpc.Option option, Object value) {
12781278
}
12791279

12801280
/**
1281-
* Returns an option to specify a filter to the addresses being listed.
1281+
* Returns an option to specify a filter on the addresses being listed.
12821282
*/
12831283
public static AddressListOption filter(AddressFilter filter) {
12841284
return new AddressListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -1294,7 +1294,7 @@ public static AddressListOption pageSize(long pageSize) {
12941294
/**
12951295
* Returns an option to specify the page token from which to start listing addresses.
12961296
*/
1297-
public static AddressListOption startPageToken(String pageToken) {
1297+
public static AddressListOption pageToken(String pageToken) {
12981298
return new AddressListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
12991299
}
13001300

@@ -1323,7 +1323,7 @@ private AddressAggregatedListOption(ComputeRpc.Option option, Object value) {
13231323
}
13241324

13251325
/**
1326-
* Returns an option to specify a filter to the addresses being listed.
1326+
* Returns an option to specify a filter on the addresses being listed.
13271327
*/
13281328
public static AddressAggregatedListOption filter(AddressFilter filter) {
13291329
return new AddressAggregatedListOption(ComputeRpc.Option.FILTER, filter.toPb());
@@ -1339,7 +1339,7 @@ public static AddressAggregatedListOption pageSize(long pageSize) {
13391339
/**
13401340
* Returns an option to specify the page token from which to start listing addresses.
13411341
*/
1342-
public static AddressAggregatedListOption startPageToken(String pageToken) {
1342+
public static AddressAggregatedListOption pageToken(String pageToken) {
13431343
return new AddressAggregatedListOption(ComputeRpc.Option.PAGE_TOKEN, pageToken);
13441344
}
13451345
}
@@ -1520,7 +1520,7 @@ public static AddressAggregatedListOption startPageToken(String pageToken) {
15201520
/**
15211521
* Deletes the requested address.
15221522
*
1523-
* @return an operation if request was issued correctly, {@code null} if the address was not
1523+
* @return an operation if the request was issued correctly, {@code null} if the address was not
15241524
* found
15251525
* @throws ComputeException upon failure
15261526
*/

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

Lines changed: 25 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,16 @@ public com.google.api.services.compute.model.Operation call() {
614614
}
615615
}
616616

617+
private static Function<com.google.api.services.compute.model.Operation, Operation>
618+
operationFromPb(final ComputeOptions serviceOptions) {
619+
return new Function<com.google.api.services.compute.model.Operation, Operation>() {
620+
@Override
621+
public Operation apply(com.google.api.services.compute.model.Operation operation) {
622+
return Operation.fromPb(serviceOptions.service(), operation);
623+
}
624+
};
625+
}
626+
617627
@Override
618628
public Page<Operation> listGlobalOperations(OperationListOption... options) {
619629
return listGlobalOperations(options(), optionMap(options));
@@ -634,13 +644,7 @@ Iterable<com.google.api.services.compute.model.Operation>> call() {
634644
String cursor = result.x();
635645
Iterable<Operation> operations = Iterables.transform(
636646
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of()
637-
: result.y(),
638-
new Function<com.google.api.services.compute.model.Operation, Operation>() {
639-
@Override
640-
public Operation apply(com.google.api.services.compute.model.Operation operation) {
641-
return Operation.fromPb(serviceOptions.service(), operation);
642-
}
643-
});
647+
: result.y(), operationFromPb(serviceOptions));
644648
return new PageImpl<>(new GlobalOperationPageFetcher(serviceOptions, cursor, optionsMap),
645649
cursor, operations);
646650
} catch (RetryHelper.RetryHelperException e) {
@@ -668,13 +672,7 @@ Iterable<com.google.api.services.compute.model.Operation>> call() {
668672
String cursor = result.x();
669673
Iterable<Operation> operations = Iterables.transform(
670674
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of()
671-
: result.y(),
672-
new Function<com.google.api.services.compute.model.Operation, Operation>() {
673-
@Override
674-
public Operation apply(com.google.api.services.compute.model.Operation operation) {
675-
return Operation.fromPb(serviceOptions.service(), operation);
676-
}
677-
});
675+
: result.y(), operationFromPb(serviceOptions));
678676
return new PageImpl<>(new RegionOperationPageFetcher(region, serviceOptions, cursor,
679677
optionsMap), cursor, operations);
680678
} catch (RetryHelper.RetryHelperException e) {
@@ -702,13 +700,7 @@ Iterable<com.google.api.services.compute.model.Operation>> call() {
702700
String cursor = result.x();
703701
Iterable<Operation> operations = Iterables.transform(
704702
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Operation>of()
705-
: result.y(),
706-
new Function<com.google.api.services.compute.model.Operation, Operation>() {
707-
@Override
708-
public Operation apply(com.google.api.services.compute.model.Operation operation) {
709-
return Operation.fromPb(serviceOptions.service(), operation);
710-
}
711-
});
703+
: result.y(), operationFromPb(serviceOptions));
712704
return new PageImpl<>(new ZoneOperationPageFetcher(zone, serviceOptions, cursor, optionsMap),
713705
cursor, operations);
714706
} catch (RetryHelper.RetryHelperException e) {
@@ -796,6 +788,16 @@ public com.google.api.services.compute.model.Operation call() {
796788
}
797789
}
798790

791+
private static Function<com.google.api.services.compute.model.Address, Address> addressFromPb(
792+
final ComputeOptions serviceOptions) {
793+
return new Function<com.google.api.services.compute.model.Address, Address>() {
794+
@Override
795+
public Address apply(com.google.api.services.compute.model.Address address) {
796+
return Address.fromPb(serviceOptions.service(), address);
797+
}
798+
};
799+
}
800+
799801
@Override
800802
public Page<Address> listGlobalAddresses(AddressListOption... options) {
801803
return listGlobalAddresses(options(), optionMap(options));
@@ -816,13 +818,7 @@ Iterable<com.google.api.services.compute.model.Address>> call() {
816818
String cursor = result.x();
817819
Iterable<Address> operations = Iterables.transform(
818820
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Address>of()
819-
: result.y(),
820-
new Function<com.google.api.services.compute.model.Address, Address>() {
821-
@Override
822-
public Address apply(com.google.api.services.compute.model.Address address) {
823-
return Address.fromPb(serviceOptions.service(), address);
824-
}
825-
});
821+
: result.y(), addressFromPb(serviceOptions));
826822
return new PageImpl<>(new GlobalAddressPageFetcher(serviceOptions, cursor, optionsMap),
827823
cursor, operations);
828824
} catch (RetryHelper.RetryHelperException e) {
@@ -850,13 +846,7 @@ Iterable<com.google.api.services.compute.model.Address>> call() {
850846
String cursor = result.x();
851847
Iterable<Address> operations = Iterables.transform(
852848
result.y() == null ? ImmutableList.<com.google.api.services.compute.model.Address>of()
853-
: result.y(),
854-
new Function<com.google.api.services.compute.model.Address, Address>() {
855-
@Override
856-
public Address apply(com.google.api.services.compute.model.Address address) {
857-
return Address.fromPb(serviceOptions.service(), address);
858-
}
859-
});
849+
: result.y(), addressFromPb(serviceOptions));
860850
return new PageImpl<>(new RegionAddressPageFetcher(region, serviceOptions, cursor,
861851
optionsMap), cursor, operations);
862852
} catch (RetryHelper.RetryHelperException e) {

0 commit comments

Comments
 (0)