Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created ConstructableWeakRef to refactor reconstructable weak caches.
- Loading branch information
Showing
5 changed files
with
54 additions
and
40 deletions.
There are no files selected for viewing
30 changes: 30 additions & 0 deletions
30
...dle-default-models/src/main/java/org/netbeans/gradle/model/util/ConstructableWeakRef.java
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,30 @@ | ||
package org.netbeans.gradle.model.util; | ||
|
||
import java.lang.ref.WeakReference; | ||
import java.util.concurrent.atomic.AtomicReference; | ||
|
||
public final class ConstructableWeakRef<T> implements NbSupplier5<T> { | ||
private final NbSupplier5<? extends T> objFactory; | ||
private final AtomicReference<WeakReference<T>> defaultRefRef; | ||
|
||
public ConstructableWeakRef(NbSupplier5<? extends T> objFactory) { | ||
if (objFactory == null) throw new NullPointerException("objFactory"); | ||
this.objFactory = objFactory; | ||
this.defaultRefRef = new AtomicReference<WeakReference<T>>(); | ||
} | ||
|
||
public T get() { | ||
while (true) { | ||
WeakReference<T> defaultRef = defaultRefRef.get(); | ||
T defaultCache = defaultRef != null ? defaultRef.get() : null; | ||
|
||
if (defaultCache == null) { | ||
defaultCache = objFactory.get(); | ||
if (!defaultRefRef.compareAndSet(defaultRef, new WeakReference<T>(defaultCache))) { | ||
continue; | ||
} | ||
} | ||
return defaultCache; | ||
} | ||
} | ||
} |
5 changes: 5 additions & 0 deletions
5
netbeans-gradle-default-models/src/main/java/org/netbeans/gradle/model/util/NbSupplier5.java
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,5 @@ | ||
package org.netbeans.gradle.model.util; | ||
|
||
public interface NbSupplier5<ResultType> { | ||
public ResultType get(); | ||
} |
26 changes: 6 additions & 20 deletions
26
...adle-default-models/src/main/java/org/netbeans/gradle/model/util/SerializationCaches.java
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
5 changes: 4 additions & 1 deletion
5
netbeans-gradle-plugin/src/main/java/org/netbeans/gradle/project/util/NbSupplier.java
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 |
---|---|---|
@@ -1,5 +1,8 @@ | ||
package org.netbeans.gradle.project.util; | ||
|
||
public interface NbSupplier<ResultType> { | ||
import org.netbeans.gradle.model.util.NbSupplier5; | ||
|
||
public interface NbSupplier<ResultType> extends NbSupplier5<ResultType> { | ||
@Override | ||
public ResultType get(); | ||
} |
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