Skip to content

Commit 0eae6aa

Browse files
committed
Use a new version of Preconditions.checkNotNull() to avoid String computations on the happy path
1 parent 12bbef3 commit 0eae6aa

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

firebase-firestore/src/main/java/com/google/firebase/firestore/local/MemoryDocumentOverlayCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ private void saveOverlay(int largestBatchId, Mutation mutation) {
7171
@Override
7272
public void saveOverlays(int largestBatchId, Map<DocumentKey, Mutation> overlays) {
7373
for (Map.Entry<DocumentKey, Mutation> entry : overlays.entrySet()) {
74-
Mutation overlay = checkNotNull(entry.getValue(), "null value for key: " + entry.getKey());
74+
Mutation overlay = checkNotNull(entry.getValue(), "null value for key: %s", entry.getKey());
7575
saveOverlay(largestBatchId, overlay);
7676
}
7777
}

firebase-firestore/src/main/java/com/google/firebase/firestore/local/SQLiteDocumentOverlayCache.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ private void saveOverlay(int largestBatchId, DocumentKey key, Mutation mutation)
127127
public void saveOverlays(int largestBatchId, Map<DocumentKey, Mutation> overlays) {
128128
for (Map.Entry<DocumentKey, Mutation> entry : overlays.entrySet()) {
129129
DocumentKey key = entry.getKey();
130-
Mutation overlay = checkNotNull(entry.getValue(), "null value for key: " + key);
130+
Mutation overlay = checkNotNull(entry.getValue(), "null value for key: %s", key);
131131
saveOverlay(largestBatchId, key, overlay);
132132
}
133133
}

firebase-firestore/src/main/java/com/google/firebase/firestore/util/Preconditions.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
package com.google.firebase.firestore.util;
1616

17+
import java.util.Locale;
1718
import javax.annotation.Nonnull;
1819
import javax.annotation.Nullable;
1920

@@ -149,6 +150,24 @@ public static <T extends Object> T checkNotNull(
149150
return reference;
150151
}
151152

153+
/**
154+
* Ensures that an object reference passed as a parameter to the calling method is not null.
155+
*
156+
* @param reference an object reference
157+
* @param errorFormatString the exception message to use if the check fails; will be formatted
158+
* using {@link String#format}.
159+
* @param formatArgs the args to specify when formatting the given {@code errorFormatString}.
160+
* @return the non-null reference that was validated
161+
* @throws NullPointerException if {@code reference} is null
162+
*/
163+
public static <T extends Object> T checkNotNull(
164+
@Nonnull T reference, String errorFormatString, @Nullable Object... formatArgs) {
165+
if (reference == null) {
166+
throw new NullPointerException(String.format(Locale.US, errorFormatString, formatArgs));
167+
}
168+
return reference;
169+
}
170+
152171
/**
153172
* Ensures the truth of an expression involving the state of the calling instance, but not
154173
* involving any parameters to the calling method.

0 commit comments

Comments
 (0)