Skip to content

Commit 3bf183a

Browse files
google-genai-botcopybara-github
authored andcommitted
refactor: reducing object creation by moving orElse() to orElseGet()
PiperOrigin-RevId: 856123195
1 parent 66bc30a commit 3bf183a

File tree

20 files changed

+42
-35
lines changed

20 files changed

+42
-35
lines changed

a2a/src/main/java/com/google/adk/a2a/converters/PartConverter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,9 @@ private static Optional<com.google.genai.types.Part> convertFilePartToGenAiPart(
123123
private static Optional<com.google.genai.types.Part> convertDataPartToGenAiPart(
124124
DataPart dataPart) {
125125
Map<String, Object> data =
126-
Optional.ofNullable(dataPart.getData()).map(HashMap::new).orElse(new HashMap<>());
126+
Optional.ofNullable(dataPart.getData()).map(HashMap::new).orElseGet(HashMap::new);
127127
Map<String, Object> metadata =
128-
Optional.ofNullable(dataPart.getMetadata()).map(HashMap::new).orElse(new HashMap<>());
128+
Optional.ofNullable(dataPart.getMetadata()).map(HashMap::new).orElseGet(HashMap::new);
129129

130130
String metadataType = metadata.getOrDefault(A2A_DATA_PART_METADATA_TYPE_KEY, "").toString();
131131

contrib/firestore-session-service/src/main/java/com/google/adk/sessions/FirestoreSessionService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ public Maybe<Session> getSession(
164164
}
165165

166166
// Fetch events based on config
167-
GetSessionConfig config = configOpt.orElse(GetSessionConfig.builder().build());
167+
GetSessionConfig config =
168+
configOpt.orElseGet(() -> GetSessionConfig.builder().build());
168169
CollectionReference eventsCollection =
169170
document.getReference().collection(EVENTS_SUBCOLLECTION_NAME);
170171
Query eventsQuery = eventsCollection.orderBy(TIMESTAMP_KEY);

core/src/main/java/com/google/adk/agents/LlmAgent.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -443,7 +443,7 @@ private void maybeSaveOutputToState(Event event) {
443443
// Concatenate text from all parts, excluding thoughts.
444444
Object output;
445445
String rawResult =
446-
event.content().flatMap(Content::parts).orElse(ImmutableList.of()).stream()
446+
event.content().flatMap(Content::parts).orElseGet(ImmutableList::of).stream()
447447
.filter(part -> !isThought(part))
448448
.map(part -> part.text().orElse(""))
449449
.collect(joining());

core/src/main/java/com/google/adk/codeexecutors/BuiltInCodeExecutor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public void processLlmRequest(LlmRequest.Builder llmRequestBuilder) {
4545
LlmRequest llmRequest = llmRequestBuilder.build();
4646
if (ModelNameUtils.isGemini2Model(llmRequest.model().orElse(null))) {
4747
GenerateContentConfig.Builder configBuilder =
48-
llmRequest.config().map(c -> c.toBuilder()).orElse(GenerateContentConfig.builder());
48+
llmRequest.config().map(c -> c.toBuilder()).orElseGet(GenerateContentConfig::builder);
4949
ImmutableList.Builder<Tool> toolsBuilder =
5050
ImmutableList.<Tool>builder()
5151
.addAll(configBuilder.build().tools().orElse(ImmutableList.<Tool>of()));

core/src/main/java/com/google/adk/events/Event.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,8 +605,8 @@ public Event build() {
605605
event.branch(branch);
606606
event.setGroundingMetadata(groundingMetadata);
607607
event.setModelVersion(modelVersion);
608-
event.setActions(actions().orElse(EventActions.builder().build()));
609-
event.setTimestamp(timestamp().orElse(Instant.now().toEpochMilli()));
608+
event.setActions(actions().orElseGet(() -> EventActions.builder().build()));
609+
event.setTimestamp(timestamp().orElseGet(() -> Instant.now().toEpochMilli()));
610610
return event;
611611
}
612612
}

core/src/main/java/com/google/adk/flows/llmflows/Basic.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ public Single<RequestProcessor.RequestProcessingResult> processRequest(
5454
LlmRequest.Builder builder =
5555
request.toBuilder()
5656
.model(modelName)
57-
.config(agent.generateContentConfig().orElse(GenerateContentConfig.builder().build()))
57+
.config(
58+
agent
59+
.generateContentConfig()
60+
.orElseGet(() -> GenerateContentConfig.builder().build()))
5861
.liveConnectConfig(liveConnectConfigBuilder.build());
5962

6063
agent.outputSchema().ifPresent(builder::outputSchema);

core/src/main/java/com/google/adk/flows/llmflows/CodeExecution.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,9 @@ private static Flowable<Event> runPreProcessor(
254254
llmRequest
255255
.contents()
256256
.add(
257-
executionResultEvent.content().orElse(Content.builder().build())))
257+
executionResultEvent
258+
.content()
259+
.orElseGet(() -> Content.builder().build())))
258260
.map(executionResultEvent -> ImmutableList.of(codeEvent, executionResultEvent))
259261
.flatMap(Flowable::fromIterable);
260262
});

core/src/main/java/com/google/adk/models/LlmRequest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ public ImmutableList<String> getSystemInstructions() {
109109
.map(Part::text)
110110
.flatMap(Optional::stream)
111111
.collect(toImmutableList()))
112-
.orElse(ImmutableList.of());
112+
.orElseGet(ImmutableList::of);
113113
}
114114

115115
public static Builder builder() {
@@ -223,7 +223,8 @@ public final Builder appendTools(List<BaseTool> tools) {
223223
*/
224224
@CanIgnoreReturnValue
225225
public final Builder outputSchema(Schema schema) {
226-
GenerateContentConfig config = config().orElse(GenerateContentConfig.builder().build());
226+
GenerateContentConfig config =
227+
config().orElseGet(() -> GenerateContentConfig.builder().build());
227228
return config(
228229
config.toBuilder().responseSchema(schema).responseMimeType("application/json").build());
229230
}

core/src/main/java/com/google/adk/plugins/PluginManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ public void registerPlugin(Plugin plugin) {
7575
"Plugin with name '" + plugin.getName() + "' already registered.");
7676
}
7777
plugins.add(plugin);
78-
logger.info("Plugin '{}' registered.", plugin.getName());
78+
logger.trace("Plugin '{}' registered.", plugin.getName());
7979
}
8080

8181
/**

core/src/main/java/com/google/adk/runner/Runner.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -554,14 +554,12 @@ private Completable compactEvents(Session session) {
554554
return Optional.ofNullable(eventsCompactionConfig)
555555
.map(SlidingWindowEventCompactor::new)
556556
.map(c -> c.compact(session, sessionService))
557-
.orElse(Completable.complete());
557+
.orElseGet(Completable::complete);
558558
}
559559

560560
private void copySessionStates(Session source, Session target) {
561561
// TODO: remove this hack when deprecating all runAsync with Session.
562-
for (var entry : source.state().entrySet()) {
563-
target.state().put(entry.getKey(), entry.getValue());
564-
}
562+
target.state().putAll(source.state());
565563
}
566564

567565
/**
@@ -621,7 +619,7 @@ private InvocationContext.Builder newInvocationContextBuilder(
621619
.pluginManager(this.pluginManager)
622620
.agent(rootAgent)
623621
.session(session)
624-
.userContent(newMessage.orElse(Content.fromParts()))
622+
.userContent(newMessage.orElseGet(() -> Content.fromParts()))
625623
.runConfig(runConfig)
626624
.resumabilityConfig(this.resumabilityConfig)
627625
.agent(this.findAgentToRun(session, rootAgent));

0 commit comments

Comments
 (0)