-
Notifications
You must be signed in to change notification settings - Fork 753
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reduce repository access #2447
Merged
+116
−35
Merged
Reduce repository access #2447
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21c1945
remove redundant repo access to the designPath
joerghoh 7617496
delay resolution of values until it is needed
joerghoh 0133eef
resolve more values lazily
joerghoh 6f61687
fix PR remarks, add more documentation
joerghoh 2201e86
Merge branch 'main' into improvement/reduce-repo-access
joerghoh ec5d13f
Merge branch 'main' into improvement/reduce-repo-access
LSantha a23011a
Merge branch 'main' into improvement/reduce-repo-access
LSantha adde00b
Merge branch 'main' into improvement/reduce-repo-access
LSantha File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
68 changes: 68 additions & 0 deletions
68
bundles/core/src/main/java/com/adobe/cq/wcm/core/components/internal/LazyValue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
~ Copyright 2023 Adobe | ||
~ | ||
~ Licensed under the Apache License, Version 2.0 (the "License"); | ||
~ you may not use this file except in compliance with the License. | ||
~ You may obtain a copy of the License at | ||
~ | ||
~ http://www.apache.org/licenses/LICENSE-2.0 | ||
~ | ||
~ Unless required by applicable law or agreed to in writing, software | ||
~ distributed under the License is distributed on an "AS IS" BASIS, | ||
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
~ See the License for the specific language governing permissions and | ||
~ limitations under the License. | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/ | ||
package com.adobe.cq.wcm.core.components.internal; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
|
||
/** | ||
* Resolve a value lazily. | ||
* | ||
* In SlingModels, not all values computed in the @PostConstruct method are always | ||
* used. That means that there might be values which are only consumed in certain | ||
* circumstances, but which have overhead to calculate. In case this value is not used | ||
* at all, this overhead is wasted time. | ||
* | ||
* Wrapping them into the LazyValue type will do the calculation of the value only | ||
* when required. | ||
* | ||
* | ||
* | ||
* @param <T> the type of the value | ||
*/ | ||
|
||
public class LazyValue<T> { | ||
|
||
private Optional<T> value; | ||
private Supplier<T> supplier; | ||
|
||
/** | ||
* | ||
* @param supplier to provide the value | ||
*/ | ||
public LazyValue(@NotNull Supplier<T> supplier) { | ||
if (supplier == null) { | ||
throw new IllegalArgumentException("supplier must not be null"); | ||
} | ||
this.supplier = supplier; | ||
} | ||
|
||
@Nullable | ||
public T get() { | ||
if (value == null) { | ||
value = Optional.ofNullable(supplier.get()); | ||
} | ||
return value.orElse(null); | ||
} | ||
|
||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can this be moved outside of internal so that it can also be used in our proxy classes?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, although this would be an extension of the public API, and so I would like to have it at least discussed here. Will raise that point when this PR has been merged.