Skip to content

Commit b96cae1

Browse files
committed
Don't use String.format and string concatenation in precondition checks.
Change on 2017/12/14 by idv <[email protected]> ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=179092962
1 parent 943eed2 commit b96cae1

File tree

6 files changed

+66
-57
lines changed

6 files changed

+66
-57
lines changed

bigquery/src/main/java/com/google/cloud/hadoop/io/bigquery/NoopFederatedExportToCloudStorage.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,18 @@ configuration, getCommaSeparatedGcsPathList(table), fileFormat, bigQueryHelper,
3737
String inputType = fileFormat.getFormatIdentifier();
3838
String tableType = table.getExternalDataConfiguration().getSourceFormat();
3939
Preconditions.checkArgument(
40-
inputType.equals(tableType), String.format(
41-
"MapReduce fileFormat '%s' does not match BigQuery sourceFormat '%s'. Use the "
42-
+ "appropriate InputFormat.", inputType, tableType));
40+
inputType.equals(tableType),
41+
"MapReduce fileFormat '%s' does not match BigQuery sourceFormat '%s'. Use the "
42+
+ "appropriate InputFormat.",
43+
inputType, tableType);
4344
gcsPaths = table.getExternalDataConfiguration().getSourceUris();
4445
}
4546

4647
@VisibleForTesting
4748
static String getCommaSeparatedGcsPathList(final Table table) {
4849
Preconditions.checkNotNull(table.getExternalDataConfiguration());
4950
for (String uri : table.getExternalDataConfiguration().getSourceUris()) {
50-
Preconditions.checkArgument(
51-
uri.startsWith("gs://"), String.format("Invalid GCS resource: '%s'", uri));
51+
Preconditions.checkArgument(uri.startsWith("gs://"), "Invalid GCS resource: '%s'", uri);
5252
}
5353
// FileInputFormat accepts a comma separated list of potentially globbed paths.
5454
return Joiner.on(",").join(table.getExternalDataConfiguration().getSourceUris());

bigquery/src/test/java/com/google/cloud/hadoop/io/bigquery/AbstractBigQueryIoIntegrationTestBase.java

+1-2
Original file line numberDiff line numberDiff line change
@@ -168,8 +168,7 @@ public void setUp()
168168
}
169169

170170
Preconditions.checkArgument(
171-
!Strings.isNullOrEmpty(projectIdvalue),
172-
String.format("Must provide %s", BIGQUERY_PROJECT_ID_ENVVARNAME));
171+
!Strings.isNullOrEmpty(projectIdvalue), "Must provide %s", BIGQUERY_PROJECT_ID_ENVVARNAME);
173172
testDataset = testId + "_dataset";
174173
testBucket = testId + "_bucket";
175174

gcsio/src/main/java/com/google/cloud/hadoop/gcsio/CacheEntry.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import com.google.api.client.util.Clock;
2020
import com.google.common.annotations.VisibleForTesting;
2121
import com.google.common.base.Preconditions;
22-
2322
import org.slf4j.Logger;
2423
import org.slf4j.LoggerFactory;
2524

@@ -178,9 +177,10 @@ public synchronized void clearItemInfo() {
178177
public synchronized GoogleCloudStorageItemInfo setItemInfo(
179178
GoogleCloudStorageItemInfo newItemInfo) {
180179
validateItemInfo(newItemInfo);
181-
Preconditions.checkArgument(newItemInfo.getResourceId().equals(resourceId),
182-
String.format("newItemInfo's resourceId (%s) doesn't match existing resourceId (%s)!",
183-
newItemInfo.getResourceId(), resourceId));
180+
Preconditions.checkArgument(
181+
newItemInfo.getResourceId().equals(resourceId),
182+
"newItemInfo's resourceId (%s) doesn't match existing resourceId (%s)!",
183+
newItemInfo.getResourceId(), resourceId);
184184

185185
if (itemInfo != null) {
186186
// TODO(user): Maybe skip the update if the newItemInfo has an older creationTime than

gcsio/src/main/java/com/google/cloud/hadoop/gcsio/CachedBucket.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ public String getName() {
7272
*/
7373
private void validateStorageObjectId(StorageResourceId resourceId) {
7474
Preconditions.checkArgument(resourceId != null, "resourceId must not be null.");
75-
Preconditions.checkArgument(resourceId.isStorageObject(),
76-
String.format("resourceId must be a StorageObject, got: %s", resourceId));
75+
Preconditions.checkArgument(
76+
resourceId.isStorageObject(), "resourceId must be a StorageObject, got: %s", resourceId);
7777
Preconditions.checkArgument(
7878
resourceId.getBucketName().equals(getName()),
79-
String.format("resourceId.getBucketName() (%s) doesn't match this.getName() (%s)",
80-
resourceId.getBucketName(), getName()));
79+
"resourceId.getBucketName() (%s) doesn't match this.getName() (%s)",
80+
resourceId.getBucketName(), getName());
8181
}
8282

8383
/**

gcsio/src/main/java/com/google/cloud/hadoop/gcsio/GoogleCloudStorageFileSystem.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -314,8 +314,8 @@ public SeekableByteChannel open(URI path, GoogleCloudStorageReadOptions readOpti
314314
throws IOException {
315315
LOG.debug("open({}, {})", path, readOptions);
316316
Preconditions.checkNotNull(path);
317-
Preconditions.checkArgument(!FileInfo.isDirectoryPath(path),
318-
"Cannot open a directory for reading: " + path);
317+
Preconditions.checkArgument(
318+
!FileInfo.isDirectoryPath(path), "Cannot open a directory for reading: %s", path);
319319

320320
// Validate the given path. false == do not allow empty object name.
321321
StorageResourceId resourceId = pathCodec.validatePathAndGetId(path, false);

gcsio/src/main/java/com/google/cloud/hadoop/gcsio/GoogleCloudStorageImpl.java

+50-40
Original file line numberDiff line numberDiff line change
@@ -298,8 +298,8 @@ void setBackOffFactory(BackOffFactory factory) {
298298
public WritableByteChannel create(StorageResourceId resourceId, CreateObjectOptions options)
299299
throws IOException {
300300
LOG.debug("create({})", resourceId);
301-
Preconditions.checkArgument(resourceId.isStorageObject(),
302-
"Expected full StorageObject id, got " + resourceId);
301+
Preconditions.checkArgument(
302+
resourceId.isStorageObject(), "Expected full StorageObject id, got %s", resourceId);
303303

304304
/*
305305
* When performing mutations in GCS, even when we aren't concerned with parallel writers,
@@ -405,17 +405,17 @@ public WritableByteChannel create(StorageResourceId resourceId, CreateObjectOpti
405405
public WritableByteChannel create(StorageResourceId resourceId)
406406
throws IOException {
407407
LOG.debug("create({})", resourceId);
408-
Preconditions.checkArgument(resourceId.isStorageObject(),
409-
"Expected full StorageObject id, got " + resourceId);
408+
Preconditions.checkArgument(
409+
resourceId.isStorageObject(), "Expected full StorageObject id, got %s", resourceId);
410410

411411
return create(resourceId, CreateObjectOptions.DEFAULT);
412412
}
413413

414414
@Override
415415
public void createEmptyObject(StorageResourceId resourceId, CreateObjectOptions options)
416416
throws IOException {
417-
Preconditions.checkArgument(resourceId.isStorageObject(),
418-
"Expected full StorageObject id, got " + resourceId);
417+
Preconditions.checkArgument(
418+
resourceId.isStorageObject(), "Expected full StorageObject id, got %s", resourceId);
419419

420420
Storage.Objects.Insert insertObject = prepareEmptyInsert(resourceId, options);
421421
try {
@@ -438,8 +438,8 @@ public void createEmptyObject(StorageResourceId resourceId, CreateObjectOptions
438438
public void createEmptyObject(StorageResourceId resourceId)
439439
throws IOException {
440440
LOG.debug("createEmptyObject({})", resourceId);
441-
Preconditions.checkArgument(resourceId.isStorageObject(),
442-
"Expected full StorageObject id, got " + resourceId);
441+
Preconditions.checkArgument(
442+
resourceId.isStorageObject(), "Expected full StorageObject id, got %s", resourceId);
443443
createEmptyObject(resourceId, CreateObjectOptions.DEFAULT);
444444
}
445445

@@ -552,8 +552,8 @@ public SeekableByteChannel open(
552552
StorageResourceId resourceId, GoogleCloudStorageReadOptions readOptions)
553553
throws IOException {
554554
LOG.debug("open({}, {})", resourceId, readOptions);
555-
Preconditions.checkArgument(resourceId.isStorageObject(),
556-
"Expected full StorageObject id, got " + resourceId);
555+
Preconditions.checkArgument(
556+
resourceId.isStorageObject(), "Expected full StorageObject id, got %s", resourceId);
557557

558558
// The underlying channel doesn't initially read data, which means that we won't see a
559559
// FileNotFoundException until read is called. As a result, in order to find out if the object
@@ -674,8 +674,10 @@ public void deleteObjects(List<StorageResourceId> fullObjectNames)
674674

675675
// Validate that all the elements represent StorageObjects.
676676
for (StorageResourceId fullObjectName : fullObjectNames) {
677-
Preconditions.checkArgument(fullObjectName.isStorageObject(),
678-
"Expected full StorageObject names only, got: " + fullObjectName.toString());
677+
Preconditions.checkArgument(
678+
fullObjectName.isStorageObject(),
679+
"Expected full StorageObject names only, got: %s",
680+
fullObjectName);
679681
}
680682

681683
// Gather exceptions to wrap in a composite exception at the end.
@@ -1335,11 +1337,12 @@ public static GoogleCloudStorageItemInfo createItemInfoForBucket(
13351337
StorageResourceId resourceId, Bucket bucket) {
13361338
Preconditions.checkArgument(resourceId != null, "resourceId must not be null");
13371339
Preconditions.checkArgument(bucket != null, "bucket must not be null");
1338-
Preconditions.checkArgument(resourceId.isBucket(),
1339-
String.format("resourceId must be a Bucket. resourceId: %s", resourceId));
1340-
Preconditions.checkArgument(resourceId.getBucketName().equals(bucket.getName()),
1341-
String.format("resourceId.getBucketName() must equal bucket.getName(): '%s' vs '%s'",
1342-
resourceId.getBucketName(), bucket.getName()));
1340+
Preconditions.checkArgument(
1341+
resourceId.isBucket(), "resourceId must be a Bucket. resourceId: %s", resourceId);
1342+
Preconditions.checkArgument(
1343+
resourceId.getBucketName().equals(bucket.getName()),
1344+
"resourceId.getBucketName() must equal bucket.getName(): '%s' vs '%s'",
1345+
resourceId.getBucketName(), bucket.getName());
13431346

13441347
// For buckets, size is 0.
13451348
return new GoogleCloudStorageItemInfo(resourceId, bucket.getTimeCreated().getValue(),
@@ -1353,14 +1356,18 @@ public static GoogleCloudStorageItemInfo createItemInfoForStorageObject(
13531356
StorageResourceId resourceId, StorageObject object) {
13541357
Preconditions.checkArgument(resourceId != null, "resourceId must not be null");
13551358
Preconditions.checkArgument(object != null, "object must not be null");
1356-
Preconditions.checkArgument(resourceId.isStorageObject(),
1357-
String.format("resourceId must be a StorageObject. resourceId: %s", resourceId));
1358-
Preconditions.checkArgument(resourceId.getBucketName().equals(object.getBucket()),
1359-
String.format("resourceId.getBucketName() must equal object.getBucket(): '%s' vs '%s'",
1360-
resourceId.getBucketName(), object.getBucket()));
1361-
Preconditions.checkArgument(resourceId.getObjectName().equals(object.getName()),
1362-
String.format("resourceId.getObjectName() must equal object.getName(): '%s' vs '%s'",
1363-
resourceId.getObjectName(), object.getName()));
1359+
Preconditions.checkArgument(
1360+
resourceId.isStorageObject(),
1361+
"resourceId must be a StorageObject. resourceId: %s",
1362+
resourceId);
1363+
Preconditions.checkArgument(
1364+
resourceId.getBucketName().equals(object.getBucket()),
1365+
"resourceId.getBucketName() must equal object.getBucket(): '%s' vs '%s'",
1366+
resourceId.getBucketName(), object.getBucket());
1367+
Preconditions.checkArgument(
1368+
resourceId.getObjectName().equals(object.getName()),
1369+
"resourceId.getObjectName() must equal object.getName(): '%s' vs '%s'",
1370+
resourceId.getObjectName(), object.getName());
13641371

13651372
Map<String, byte[]> decodedMetadata =
13661373
object.getMetadata() == null ? null : decodeMetadata(object.getMetadata());
@@ -1515,15 +1522,18 @@ public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
15151522
// Assemble the return list in the same order as the input arguments.
15161523
List<GoogleCloudStorageItemInfo> sortedItemInfos = new ArrayList<>();
15171524
for (StorageResourceId resourceId : resourceIds) {
1518-
Preconditions.checkState(itemInfos.containsKey(resourceId),
1519-
String.format("Somehow missing resourceId '%s' from map: %s", resourceId, itemInfos));
1525+
Preconditions.checkState(
1526+
itemInfos.containsKey(resourceId),
1527+
"Somehow missing resourceId '%s' from map: %s",
1528+
resourceId, itemInfos);
15201529
sortedItemInfos.add(itemInfos.get(resourceId));
15211530
}
15221531

15231532
// We expect the return list to be the same size, even if some entries were "not found".
1524-
Preconditions.checkState(sortedItemInfos.size() == resourceIds.size(), String.format(
1525-
"sortedItemInfos.size() (%d) != resourceIds.size() (%d). infos: %s, ids: %s",
1526-
sortedItemInfos.size(), resourceIds.size(), sortedItemInfos, resourceIds));
1533+
Preconditions.checkState(
1534+
sortedItemInfos.size() == resourceIds.size(),
1535+
"sortedItemInfos.size() (%s) != resourceIds.size() (%s). infos: %s, ids: %s",
1536+
sortedItemInfos.size(), resourceIds.size(), sortedItemInfos, resourceIds);
15271537
return sortedItemInfos;
15281538
}
15291539

@@ -1589,18 +1599,18 @@ public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) {
15891599
// Assemble the return list in the same order as the input arguments.
15901600
List<GoogleCloudStorageItemInfo> sortedItemInfos = new ArrayList<>();
15911601
for (UpdatableItemInfo itemInfo : itemInfoList) {
1592-
Preconditions.checkState(resultItemInfos.containsKey(itemInfo.getStorageResourceId()),
1593-
String.format(
1594-
"Missing resourceId '%s' from map: %s",
1595-
itemInfo.getStorageResourceId(),
1596-
resultItemInfos));
1602+
Preconditions.checkState(
1603+
resultItemInfos.containsKey(itemInfo.getStorageResourceId()),
1604+
"Missing resourceId '%s' from map: %s",
1605+
itemInfo.getStorageResourceId(), resultItemInfos);
15971606
sortedItemInfos.add(resultItemInfos.get(itemInfo.getStorageResourceId()));
15981607
}
15991608

16001609
// We expect the return list to be the same size, even if some entries were "not found".
1601-
Preconditions.checkState(sortedItemInfos.size() == itemInfoList.size(), String.format(
1602-
"sortedItemInfos.size() (%d) != resourceIds.size() (%d). infos: %s, updateItemInfos: %s",
1603-
sortedItemInfos.size(), itemInfoList.size(), sortedItemInfos, itemInfoList));
1610+
Preconditions.checkState(
1611+
sortedItemInfos.size() == itemInfoList.size(),
1612+
"sortedItemInfos.size() (%s) != resourceIds.size() (%s). infos: %s, updateItemInfos: %s",
1613+
sortedItemInfos.size(), itemInfoList.size(), sortedItemInfos, itemInfoList);
16041614
return sortedItemInfos;
16051615
}
16061616

@@ -1733,8 +1743,8 @@ IOException wrapException(IOException e, String message,
17331743
private StorageObject getObject(StorageResourceId resourceId)
17341744
throws IOException {
17351745
LOG.debug("getObject({})", resourceId);
1736-
Preconditions.checkArgument(resourceId.isStorageObject(),
1737-
"Expected full StorageObject id, got " + resourceId);
1746+
Preconditions.checkArgument(
1747+
resourceId.isStorageObject(), "Expected full StorageObject id, got %s", resourceId);
17381748
String bucketName = resourceId.getBucketName();
17391749
String objectName = resourceId.getObjectName();
17401750
StorageObject object = null;

0 commit comments

Comments
 (0)