-
Notifications
You must be signed in to change notification settings - Fork 11.1k
Various simplifications to code and comments. #6015
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -97,7 +97,7 @@ | |
| * <li>accumulation of cache access statistics | ||
| * </ul> | ||
| * | ||
| * <p>These features are all optional; caches can be created using all or none of them. By default | ||
| * <p>These features are all optional; caches can be created using all or none of them. By default, | ||
| * cache instances created by {@code CacheBuilder} will not perform any type of eviction. | ||
| * | ||
| * <p>Usage example: | ||
|
|
@@ -231,13 +231,16 @@ public CacheStats snapshot() { | |
| }); | ||
| static final CacheStats EMPTY_STATS = new CacheStats(0, 0, 0, 0, 0, 0); | ||
|
|
||
| static final Supplier<StatsCounter> CACHE_STATS_COUNTER = | ||
| new Supplier<StatsCounter>() { | ||
| @Override | ||
| public StatsCounter get() { | ||
| return new SimpleStatsCounter(); | ||
| } | ||
| }; | ||
| /* | ||
| * We avoid using a method reference here for now: Inside Google, CacheBuilder is used from the | ||
| * implementation of a custom ClassLoader that is sometimes used as a system classloader. That's a | ||
| * problem because method-reference linking tries to look up the system classloader, and it fails | ||
| * because there isn't one yet. | ||
| * | ||
| * I would have guessed that a lambda would produce the same problem, but maybe it's safe because | ||
| * the lambda implementation is generated as a method in the _same class_ as the usage? | ||
| */ | ||
| static final Supplier<StatsCounter> CACHE_STATS_COUNTER = () -> new SimpleStatsCounter(); | ||
|
Comment on lines
231
to
+243
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scalability Issue: Changing from an anonymous class to a lambda for the 'CACHE_STATS_COUNTER' initialization could potentially impact scalability, depending on how the JVM optimizes lambda expressions vs. anonymous classes. Lambdas in Java 8 are generally more efficient than anonymous classes, especially when they are stateless or effectively final, as they can be optimized better by the JVM.
Comment on lines
+234
to
+243
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Optimization Issue: The replacement of an anonymous class with a lambda expression for CACHE_STATS_COUNTER improves readability and conciseness. This change also potentially enhances performance by reducing the amount of generated bytecode, as lambda expressions are more efficiently handled by the JVM compared to anonymous classes. However, the detailed comment explains a specific scenario where method references could introduce issues due to class loading order. This thoughtful consideration of context and potential issues is commendable, ensuring that the change does not inadvertently introduce bugs. |
||
|
|
||
| enum NullListener implements RemovalListener<Object, Object> { | ||
| INSTANCE; | ||
|
|
@@ -825,11 +828,11 @@ Ticker getTicker(boolean recordsTime) { | |
| * | ||
| * <p><b>Warning:</b> after invoking this method, do not continue to use <i>this</i> cache builder | ||
| * reference; instead use the reference this method <i>returns</i>. At runtime, these point to the | ||
| * same instance, but only the returned reference has the correct generic type information so as | ||
| * to ensure type safety. For best results, use the standard method-chaining idiom illustrated in | ||
| * the class documentation above, configuring a builder and building your cache in a single | ||
| * statement. Failure to heed this advice can result in a {@link ClassCastException} being thrown | ||
| * by a cache operation at some <i>undefined</i> point in the future. | ||
| * same instance, but only the returned reference has the correct generic type information to | ||
| * ensure type safety. For best results, use the standard method-chaining idiom illustrated in the | ||
| * class documentation above, configuring a builder and building your cache in a single statement. | ||
| * Failure to heed this advice can result in a {@link ClassCastException} being thrown by a cache | ||
| * operation at some <i>undefined</i> point in the future. | ||
| * | ||
| * <p><b>Warning:</b> any exception thrown by {@code listener} will <i>not</i> be propagated to | ||
| * the {@code Cache} user, only logged via a {@link Logger}. | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,7 +26,6 @@ | |
| import com.google.errorprone.annotations.CheckReturnValue; | ||
| import java.io.Serializable; | ||
| import java.util.Map; | ||
| import java.util.concurrent.Callable; | ||
| import java.util.concurrent.Executor; | ||
|
|
||
| /** | ||
|
|
@@ -154,7 +153,7 @@ public static <K, V> CacheLoader<K, V> from(Function<K, V> function) { | |
| */ | ||
| @CheckReturnValue | ||
| public static <V> CacheLoader<Object, V> from(Supplier<V> supplier) { | ||
| return new SupplierToCacheLoader<V>(supplier); | ||
| return new SupplierToCacheLoader<>(supplier); | ||
| } | ||
|
|
||
| private static final class FunctionToCacheLoader<K, V> extends CacheLoader<K, V> | ||
|
|
@@ -195,15 +194,9 @@ public V load(K key) throws Exception { | |
| } | ||
|
|
||
| @Override | ||
| public ListenableFuture<V> reload(final K key, final V oldValue) throws Exception { | ||
| public ListenableFuture<V> reload(final K key, final V oldValue) { | ||
| ListenableFutureTask<V> task = | ||
| ListenableFutureTask.create( | ||
| new Callable<V>() { | ||
| @Override | ||
| public V call() throws Exception { | ||
| return loader.reload(key, oldValue).get(); | ||
| } | ||
| }); | ||
| ListenableFutureTask.create(() -> loader.reload(key, oldValue).get()); | ||
| executor.execute(task); | ||
| return task; | ||
| } | ||
|
Comment on lines
195
to
202
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Code Structure Issue: The method 'reload' has been refactored to use a lambda expression instead of an anonymous class, which simplifies the code and improves readability. Additionally, the 'throws Exception' declaration has been removed from the method signature, aligning with the lambda expression's inability to declare checked exceptions. |
||
|
|
||
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.
Optimization Issue: Utilizing diamond syntax for the instantiation of generic classes such as ConcurrentLinkedQueue enhances code readability and conciseness. This change leverages the compiler's ability to infer type parameters, reducing redundancy and improving the overall clarity of the code. It's a straightforward yet effective way to maintain cleaner code, adhering to modern Java development practices.
Fix: The refactor to use diamond syntax is correctly applied and should be used as a standard practice for similar instantiations throughout the codebase. It's a minor but impactful change that aligns with contemporary Java standards, promoting more maintainable and readable code. No additional modifications are needed for these specific lines. Developers should ensure that this practice is consistently applied in all applicable scenarios.
Code Suggestion: