Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
L
live-coding-fr
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Redmine
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
twitch
live-coding-fr
Merge requests
!93
Resolve "Java memoizer"
Code
Review changes
Check out branch
Download
Patches
Plain diff
Merged
Resolve "Java memoizer"
120-java-memoizer
into
master
Overview
0
Commits
1
Pipelines
0
Changes
7
Merged
Colin DAMON
requested to merge
120-java-memoizer
into
master
3 years ago
Overview
0
Commits
1
Pipelines
0
Changes
7
Expand
Closes
#120 (closed)
Edited
3 years ago
by
Colin DAMON
0
0
Merge request reports
Viewing commit
73b8ec6d
Show latest version
7 files
+
179
−
0
Side-by-side
Compare changes
Side-by-side
Inline
Show whitespace changes
Show one file at a time
Files
7
Search (e.g. *.vue) (Ctrl+P)
73b8ec6d
Java memoizers implementation
· 73b8ec6d
Colin DAMON
authored
3 years ago
java-memoizers/src/main/java/fr/craft/memoizer/Memoizers.java
0 → 100644
+
56
−
0
Options
package
fr.craft.memoizer
;
import
java.util.Map
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.function.Function
;
import
java.util.function.Supplier
;
public
final
class
Memoizers
{
private
Memoizers
()
{
}
public
static
<
Result
>
Supplier
<
Result
>
of
(
Supplier
<
Result
>
supplier
)
{
return
()
->
of
(
dummy
->
supplier
.
get
()).
apply
(
null
);
}
public
static
<
Input
,
Result
>
Function
<
Input
,
Result
>
of
(
Function
<
Input
,
Result
>
function
)
{
return
new
MemoizedFunction
<>(
function
);
}
private
static
class
MemoizedFunction
<
Input
,
Result
>
implements
Function
<
Input
,
Result
>
{
private
final
Function
<
Input
,
Result
>
function
;
private
final
Map
<
MemoizedInput
<
Input
>,
MemoizedResult
<
Result
>>
results
=
new
ConcurrentHashMap
<>();
public
MemoizedFunction
(
Function
<
Input
,
Result
>
function
)
{
this
.
function
=
function
;
}
@Override
public
Result
apply
(
Input
input
)
{
return
results
.
computeIfAbsent
(
new
MemoizedInput
<>(
input
),
this
::
toMemoizedResult
)
.
result
();
}
private
MemoizedResult
<
Result
>
toMemoizedResult
(
MemoizedInput
<
Input
>
input
)
{
return
new
MemoizedResult
<>(
function
.
apply
(
input
.
input
()));
}
private
static
record
MemoizedInput
<
Input
>
(
Input
input
)
{
}
private
static
record
MemoizedResult
<
Result
>
(
Result
result
)
{
}
}
}
Loading