Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
Aparapi Examples
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
1
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
Show more breadcrumbs
Aparapi
Aparapi Examples
Commits
68a17e4b
Commit
68a17e4b
authored
10 years ago
by
log2
Browse files
Options
Downloads
Patches
Plain Diff
Added thread-safe memoizer (with inner Optional class)
parent
af57a2e9
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/Memoizer.java
+84
-0
84 additions, 0 deletions
...api/src/java/com/amd/aparapi/internal/model/Memoizer.java
with
84 additions
and
0 deletions
com.amd.aparapi/src/java/com/amd/aparapi/internal/model/Memoizer.java
0 → 100644
+
84
−
0
View file @
68a17e4b
package
com.amd.aparapi.internal.model
;
import
java.util.NoSuchElementException
;
import
java.util.concurrent.atomic.AtomicReference
;
interface
Optional
<
E
>
{
final
class
Some
<
E
>
implements
Optional
<
E
>{
private
final
E
value
;
static
final
<
E
>
Optional
<
E
>
of
(
E
value
)
{
return
new
Some
<>(
value
);
}
private
Some
(
E
value
)
{
this
.
value
=
value
;
}
@Override
public
E
get
()
{
return
value
;
}
@Override
public
boolean
isPresent
()
{
return
true
;
}
}
final
class
None
<
E
>
implements
Optional
<
E
>{
@SuppressWarnings
(
"unchecked"
)
static
<
E
>
Optional
<
E
>
none
()
{
return
none
;
}
@SuppressWarnings
(
"rawtypes"
)
private
static
final
None
none
=
new
None
();
private
None
()
{
// Do nothing
}
@Override
public
E
get
()
{
throw
new
NoSuchElementException
(
"No value present"
);
}
@Override
public
boolean
isPresent
()
{
return
false
;
}
}
E
get
();
boolean
isPresent
();
}
public
interface
Memoizer
<
T
>
extends
Supplier
<
T
>{
public
final
class
Impl
<
T
>
implements
Memoizer
<
T
>{
private
final
Supplier
<
T
>
supplier
;
private
final
AtomicReference
<
Optional
<
T
>>
valueRef
=
new
AtomicReference
<>(
Optional
.
None
.<
T
>
none
());
Impl
(
Supplier
<
T
>
supplier
)
{
this
.
supplier
=
supplier
;
}
@Override
public
T
get
()
{
Optional
<
T
>
value
=
valueRef
.
get
();
while
(!
value
.
isPresent
())
{
Optional
<
T
>
newValue
=
Optional
.
Some
.
of
(
supplier
.
get
());
if
(
valueRef
.
compareAndSet
(
value
,
newValue
))
{
value
=
newValue
;
break
;
}
value
=
valueRef
.
get
();
}
return
value
.
get
();
}
public
static
<
T
>
Memoizer
<
T
>
of
(
Supplier
<
T
>
supplier
)
{
return
new
Impl
<>(
supplier
);
}
}
// static <T> Memoizer<T> of(Supplier<T> supplier)
// {
// return new Impl<>(supplier);
// }
}
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