Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Aparapi Examples
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
We are moving to Forgejo!
You are on a read-only GitLab instance.
Show more breadcrumbs
Aparapi
Aparapi Examples
Commits
13015300
Commit
13015300
authored
10 years ago
by
log2
Browse files
Options
Downloads
Patches
Plain Diff
Added cache with three generic parameters: key type, value type and
thrown exceptions (if any)
parent
68a17e4b
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
com.amd.aparapi/src/java/com/amd/aparapi/internal/model/ValueCache.java
+46
-0
46 additions, 0 deletions
...i/src/java/com/amd/aparapi/internal/model/ValueCache.java
with
46 additions
and
0 deletions
com.amd.aparapi/src/java/com/amd/aparapi/internal/model/ValueCache.java
0 → 100644
+
46
−
0
View file @
13015300
package
com.amd.aparapi.internal.model
;
import
java.lang.ref.Reference
;
import
java.lang.ref.SoftReference
;
import
java.util.concurrent.ConcurrentHashMap
;
import
java.util.concurrent.ConcurrentMap
;
//import java.util.function.Supplier;
public
final
class
ValueCache
<
K
,
V
,
T
extends
Throwable
>
{
// @FunctionalInterface
public
interface
ThrowingValueComputer
<
K
,
V
,
T
extends
Throwable
>
{
V
compute
(
K
key
)
throws
T
;
}
// @FunctionalInterface
public
interface
ValueComputer
<
K
,
V
>
extends
ThrowingValueComputer
<
K
,
V
,
RuntimeException
>{
// Marker interface
}
public
static
<
K
,
V
,
T
extends
Throwable
>
ValueCache
<
K
,
V
,
T
>
on
(
ThrowingValueComputer
<
K
,
V
,
T
>
computer
)
{
return
new
ValueCache
<
K
,
V
,
T
>(
computer
);
}
private
final
ConcurrentMap
<
K
,
SoftReference
<
V
>>
map
=
new
ConcurrentHashMap
<>();
private
final
ThrowingValueComputer
<
K
,
V
,
T
>
computer
;
private
ValueCache
(
ThrowingValueComputer
<
K
,
V
,
T
>
computer
)
{
this
.
computer
=
computer
;
}
public
V
computeIfAbsent
(
K
key
)
throws
T
{
Reference
<
V
>
reference
=
map
.
get
(
key
);
V
value
=
reference
==
null
?
null
:
reference
.
get
();
if
(
value
==
null
)
{
value
=
computer
.
compute
(
key
);
map
.
put
(
key
,
new
SoftReference
<>(
value
));
}
return
value
;
}
public
void
invalidate
()
{
map
.
clear
();
}
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment