-
-
Notifications
You must be signed in to change notification settings - Fork 435
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added LazyEvaluator to evaluate a function lazily
AndroidOptionsInitializer.installDefaultIntegrations now evaluate cache dir lazily
- Loading branch information
1 parent
3f5a679
commit 32134b8
Showing
7 changed files
with
123 additions
and
8 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.sentry.util; | ||
|
||
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
/** | ||
* Class that evaluates a function lazily. It means the evaluator function is called only when | ||
* getValue is called, and it's cached. | ||
*/ | ||
@ApiStatus.Internal | ||
public final class LazyEvaluator<T> { | ||
private @Nullable T value = null; | ||
private final @NotNull Evaluator<T> evaluator; | ||
|
||
/** | ||
* Class that evaluates a function lazily. It means the evaluator function is called only when | ||
* getValue is called, and it's cached. | ||
* | ||
* @param evaluator The function to evaluate. | ||
*/ | ||
public LazyEvaluator(final @NotNull Evaluator<T> evaluator) { | ||
this.evaluator = evaluator; | ||
} | ||
|
||
/** | ||
* Executes the evaluator function and caches its result, so that it's called only once. | ||
* | ||
* @return The result of the evaluator function. | ||
*/ | ||
public synchronized @NotNull T getValue() { | ||
if (value == null) { | ||
value = evaluator.evaluate(); | ||
} | ||
return value; | ||
} | ||
|
||
public interface Evaluator<T> { | ||
@NotNull | ||
T evaluate(); | ||
} | ||
} |
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,41 @@ | ||
package io.sentry.util | ||
|
||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
|
||
class LazyEvaluatorTest { | ||
|
||
class Fixture { | ||
var count = 0 | ||
|
||
fun getSut(): LazyEvaluator<Int> { | ||
count = 0 | ||
return LazyEvaluator<Int> { ++count } | ||
} | ||
} | ||
|
||
private val fixture = Fixture() | ||
|
||
@Test | ||
fun `does not evaluate on instantiation`() { | ||
fixture.getSut() | ||
assertEquals(0, fixture.count) | ||
} | ||
|
||
@Test | ||
fun `evaluator is called on getValue`() { | ||
val evaluator = fixture.getSut() | ||
assertEquals(0, fixture.count) | ||
assertEquals(1, evaluator.value) | ||
assertEquals(1, fixture.count) | ||
} | ||
|
||
@Test | ||
fun `evaluates only once`() { | ||
val evaluator = fixture.getSut() | ||
assertEquals(0, fixture.count) | ||
assertEquals(1, evaluator.value) | ||
assertEquals(1, evaluator.value) | ||
assertEquals(1, fixture.count) | ||
} | ||
} |