Skip to content

Commit a5c9850

Browse files
authored
[Java] Change precondition checks to use lazy formatting (#36065)
1 parent 7b22d61 commit a5c9850

24 files changed

Lines changed: 61 additions & 49 deletions

File tree

examples/java/src/main/java/org/apache/beam/examples/complete/datatokenization/utils/SchemasUtils.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,14 +140,15 @@ public static String getGcsFileAsString(String filePath) {
140140
result = FileSystems.match(filePath);
141141
checkArgument(
142142
result.status() == MatchResult.Status.OK && !result.metadata().isEmpty(),
143-
"Failed to match any files with the pattern: " + filePath);
143+
"Failed to match any files with the pattern: %s",
144+
filePath);
144145

145146
List<ResourceId> rId =
146147
result.metadata().stream()
147148
.map(MatchResult.Metadata::resourceId)
148149
.collect(Collectors.toList());
149150

150-
checkArgument(rId.size() == 1, "Expected exactly 1 file, but got " + rId.size() + " files.");
151+
checkArgument(rId.size() == 1, "Expected exactly 1 file, but got %s files.", rId.size());
151152

152153
Reader reader =
153154
Channels.newReader(FileSystems.open(rId.get(0)), StandardCharsets.UTF_8.name());

runners/direct-java/src/main/java/org/apache/beam/runners/direct/SplittableProcessElementsEvaluatorFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ class SplittableProcessElementsEvaluatorFactory<
6565
public DoFnLifecycleManager load(final AppliedPTransform<?, ?, ?> application) {
6666
checkArgument(
6767
ProcessElements.class.isInstance(application.getTransform()),
68-
"No know extraction of the fn from " + application);
68+
"No know extraction of the fn from %s",
69+
application);
6970
final ProcessElements<
7071
InputT, OutputT, RestrictionT, PositionT, WatermarkEstimatorStateT>
7172
transform =

runners/google-cloud-dataflow-java/src/main/java/org/apache/beam/runners/dataflow/util/CloudObjectTranslators.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ public CloudObject toCloudObject(FullWindowedValueCoder target, SdkComponents sd
290290
@Override
291291
public FullWindowedValueCoder fromCloudObject(CloudObject object) {
292292
List<Coder<?>> components = getComponents(object);
293-
checkArgument(components.size() == 2, "Expecting 2 components, got " + components.size());
293+
checkArgument(components.size() == 2, "Expecting 2 components, got %s", components.size());
294294
@SuppressWarnings("unchecked")
295295
Coder<? extends BoundedWindow> window = (Coder<? extends BoundedWindow>) components.get(1);
296296
return FullWindowedValueCoder.of(components.get(0), window);

runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/GroupAlsoByWindowParDoFnFactory.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ public ParDoFn create(
101101
SerializableUtils.deserializeFromByteArray(serializedCombineFn, "serialized combine fn");
102102
checkArgument(
103103
combineFnObj instanceof AppliedCombineFn,
104-
"unexpected kind of AppliedCombineFn: " + combineFnObj.getClass().getName());
104+
"unexpected kind of AppliedCombineFn: %s",
105+
combineFnObj.getClass().getName());
105106
combineFn = (AppliedCombineFn<?, ?, ?, ?>) combineFnObj;
106107
}
107108

@@ -110,14 +111,16 @@ public ParDoFn create(
110111
Coder<?> inputCoder = CloudObjects.coderFromCloudObject(CloudObject.fromSpec(inputCoderObject));
111112
checkArgument(
112113
inputCoder instanceof WindowedValueCoder,
113-
"Expected WindowedValueCoder for inputCoder, got: " + inputCoder.getClass().getName());
114+
"Expected WindowedValueCoder for inputCoder, got: %s",
115+
inputCoder.getClass().getName());
114116
@SuppressWarnings("unchecked")
115117
WindowedValueCoder<?> windowedValueCoder = (WindowedValueCoder<?>) inputCoder;
116118

117119
Coder<?> elemCoder = windowedValueCoder.getValueCoder();
118120
checkArgument(
119121
elemCoder instanceof KvCoder,
120-
"Expected KvCoder for inputCoder, got: " + elemCoder.getClass().getName());
122+
"Expected KvCoder for inputCoder, got: %s",
123+
elemCoder.getClass().getName());
121124
@SuppressWarnings("unchecked")
122125
KvCoder<?, ?> kvCoder = (KvCoder<?, ?>) elemCoder;
123126

runners/google-cloud-dataflow-java/worker/src/main/java/org/apache/beam/runners/dataflow/worker/InMemoryReader.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,12 @@ public InMemoryReader(
6464
int maxIndex = encodedElements.size();
6565
this.startIndex = Math.min(maxIndex, firstNonNull(startIndex, 0));
6666
this.endIndex = Math.min(maxIndex, firstNonNull(endIndex, maxIndex));
67-
checkArgument(this.startIndex >= 0, "negative start index: " + startIndex);
67+
checkArgument(this.startIndex >= 0, "negative start index: %s", startIndex);
6868
checkArgument(
6969
this.endIndex >= this.startIndex,
70-
"end index before start: [" + this.startIndex + ", " + this.endIndex + ")");
70+
"end index before start: [%s, %s)",
71+
this.startIndex,
72+
this.endIndex);
7173
this.coder = coder;
7274
}
7375

runners/java-fn-execution/src/main/java/org/apache/beam/runners/fnexecution/environment/ProcessManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public RunningProcess startProcess(
169169
public void stopProcess(String id) {
170170
checkNotNull(id, "Process id must not be null");
171171
try {
172-
Process process = checkNotNull(processes.remove(id), "Process for id does not exist: " + id);
172+
Process process = checkNotNull(processes.remove(id), "Process for id does not exist: %s", id);
173173
stopProcess(id, process);
174174
} finally {
175175
synchronized (ALL_PROCESS_MANAGERS) {

runners/java-fn-execution/src/test/java/org/apache/beam/runners/fnexecution/wire/CommonCoderTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -519,11 +519,11 @@ public CompletableFuture<StateResponse> handle(StateRequest.Builder requestBuild
519519
ImmutableBiMap.copyOf(new ModelCoderRegistrar().getCoderURNs())
520520
.inverse()
521521
.get(coder.getUrn());
522-
checkNotNull(coderType, "Unknown coder URN: " + coder.getUrn());
522+
checkNotNull(coderType, "Unknown coder URN: %s", coder.getUrn());
523523

524524
CoderTranslator<?> translator = new ModelCoderRegistrar().getCoderTranslators().get(coderType);
525525
checkNotNull(
526-
translator, "No translator found for common coder class: " + coderType.getSimpleName());
526+
translator, "No translator found for common coder class: %s", coderType.getSimpleName());
527527

528528
return translator.fromComponents(components, coder.getPayload(), new TranslationContext() {});
529529
}

runners/spark/src/main/java/org/apache/beam/runners/spark/util/SparkSideInputReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public SparkSideInputReader(
6161
checkNotNull(view, "The PCollectionView passed to sideInput cannot be null ");
6262
KV<WindowingStrategy<?, ?>, SideInputBroadcast<?>> windowedBroadcastHelper =
6363
sideInputs.get(view.getTagInternal());
64-
checkNotNull(windowedBroadcastHelper, "SideInput for view " + view + " is not available.");
64+
checkNotNull(windowedBroadcastHelper, "SideInput for view %s is not available.", view);
6565

6666
// --- sideInput window
6767
final BoundedWindow sideInputWindow = view.getWindowMappingFn().getSideInputWindow(window);

runners/twister2/src/main/java/org/apache/beam/runners/twister2/utils/Twister2SideInputReader.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public Twister2SideInputReader(
6161
public <T> @Nullable T get(PCollectionView<T> view, BoundedWindow window) {
6262
checkNotNull(view, "View passed to sideInput cannot be null");
6363
TupleTag<?> tag = view.getTagInternal();
64-
checkNotNull(sideInputs.get(tag), "Side input for " + view + " not available.");
64+
checkNotNull(sideInputs.get(tag), "Side input for %s not available.", view);
6565
return getSideInput(view, window);
6666
}
6767

sdks/java/core/src/main/java/org/apache/beam/sdk/io/TFRecordReadSchemaTransformConfiguration.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,8 @@ public void validate() {
6363
if (errorHandling != null) {
6464
checkArgument(
6565
!Strings.isNullOrEmpty(errorHandling.getOutput()),
66-
invalidConfigMessage + "Output must not be empty if error handling specified.");
66+
"%sOutput must not be empty if error handling specified.",
67+
invalidConfigMessage);
6768
}
6869
}
6970

0 commit comments

Comments
 (0)