From 1f385ee6a7c9da7652fef3477c38b8dd89f33af4 Mon Sep 17 00:00:00 2001 From: Andrey Litvitski Date: Sat, 7 Dec 2024 22:32:35 +0300 Subject: [PATCH] Change the isPresent Option state to functional style --- .../inject/internal/InjectorBindingData.java | 44 +++++++------------ .../internal/InjectorJitBindingData.java | 4 +- 2 files changed, 17 insertions(+), 31 deletions(-) diff --git a/core/src/com/google/inject/internal/InjectorBindingData.java b/core/src/com/google/inject/internal/InjectorBindingData.java index 395022809e..ac09f3ddfb 100644 --- a/core/src/com/google/inject/internal/InjectorBindingData.java +++ b/core/src/com/google/inject/internal/InjectorBindingData.java @@ -178,13 +178,10 @@ public void addInterceptorBinding(InterceptorBinding interceptorBinding) { } public ImmutableList getInterceptorBindings() { - if (parent.isPresent()) { - return new ImmutableList.Builder() - .addAll(parent.get().getInterceptorBindings()) - .addAll(interceptorBindings) - .build(); - } - return ImmutableList.copyOf(interceptorBindings); + return parent.map(injectorBindingData -> new ImmutableList.Builder() + .addAll(injectorBindingData.getInterceptorBindings()) + .addAll(interceptorBindings) + .build()).orElseGet(() -> ImmutableList.copyOf(interceptorBindings)); } public ImmutableList getInterceptorBindingsThisLevel() { @@ -196,13 +193,10 @@ public void addTypeListener(TypeListenerBinding listenerBinding) { } public ImmutableList getTypeListenerBindings() { - if (parent.isPresent()) { - return new ImmutableList.Builder() - .addAll(parent.get().getTypeListenerBindings()) - .addAll(typeListenerBindings) - .build(); - } - return ImmutableList.copyOf(typeListenerBindings); + return parent.map(injectorBindingData -> new ImmutableList.Builder() + .addAll(injectorBindingData.getTypeListenerBindings()) + .addAll(typeListenerBindings) + .build()).orElseGet(() -> ImmutableList.copyOf(typeListenerBindings)); } public ImmutableList getTypeListenerBindingsThisLevel() { @@ -214,13 +208,10 @@ public void addProvisionListener(ProvisionListenerBinding listenerBinding) { } public ImmutableList getProvisionListenerBindings() { - if (parent.isPresent()) { - return new ImmutableList.Builder() - .addAll(parent.get().getProvisionListenerBindings()) - .addAll(provisionListenerBindings) - .build(); - } - return ImmutableList.copyOf(provisionListenerBindings); + return parent.map(injectorBindingData -> new ImmutableList.Builder() + .addAll(injectorBindingData.getProvisionListenerBindings()) + .addAll(provisionListenerBindings) + .build()).orElseGet(() -> ImmutableList.copyOf(provisionListenerBindings)); } public ImmutableList getProvisionListenerBindingsThisLevel() { @@ -232,13 +223,10 @@ public void addScanner(ModuleAnnotatedMethodScannerBinding scanner) { } public ImmutableList getScannerBindings() { - if (parent.isPresent()) { - return new ImmutableList.Builder() - .addAll(parent.get().getScannerBindings()) - .addAll(scannerBindings) - .build(); - } - return ImmutableList.copyOf(scannerBindings); + return parent.map(injectorBindingData -> new ImmutableList.Builder() + .addAll(injectorBindingData.getScannerBindings()) + .addAll(scannerBindings) + .build()).orElseGet(() -> ImmutableList.copyOf(scannerBindings)); } public ImmutableList getScannerBindingsThisLevel() { diff --git a/core/src/com/google/inject/internal/InjectorJitBindingData.java b/core/src/com/google/inject/internal/InjectorJitBindingData.java index 07a747a067..4e64963e20 100644 --- a/core/src/com/google/inject/internal/InjectorJitBindingData.java +++ b/core/src/com/google/inject/internal/InjectorJitBindingData.java @@ -82,9 +82,7 @@ void banKey(Key key, InjectorBindingData injectorBindingData, Object source) * overriding explicit bindings declared in a child injector. */ void banKeyInParent(Key key, InjectorBindingData injectorBindingData, Object source) { - if (parent.isPresent()) { - parent.get().banKey(key, injectorBindingData, source); - } + parent.ifPresent(injectorJitBindingData -> injectorJitBindingData.banKey(key, injectorBindingData, source)); } /**