-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MemoizerTest: Test for FastCodeExceptionAnalyzer.formatCatch().
- Loading branch information
nbauma109
committed
Dec 19, 2022
1 parent
580a260
commit d5606f9
Showing
2 changed files
with
177 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package jd.core.test; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.junit.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
|
||
/* | ||
* Test for FastCodeExceptionAnalyzer.formatCatch(). | ||
*/ | ||
public class MemoizerTest extends AbstractTestCase { | ||
@Test | ||
public void test() throws Exception { | ||
String output = decompile("org/apache/commons/lang3/concurrent/Memoizer"); | ||
assertEquals(IOUtils.toString(getClass().getResource("Memoizer.txt"), StandardCharsets.UTF_8), output); | ||
} | ||
} |
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,158 @@ | ||
/* */ package org.apache.commons.lang3.concurrent; | ||
/* */ | ||
/* */ import java.util.concurrent.Callable; | ||
/* */ import java.util.concurrent.CancellationException; | ||
/* */ import java.util.concurrent.ConcurrentHashMap; | ||
/* */ import java.util.concurrent.ConcurrentMap; | ||
/* */ import java.util.concurrent.ExecutionException; | ||
/* */ import java.util.concurrent.Future; | ||
/* */ import java.util.concurrent.FutureTask; | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ public class Memoizer<I, O> | ||
/* */ implements Computable<I, O> | ||
/* */ { | ||
/* 56 */ private final ConcurrentMap<I, Future<O>> cache = new ConcurrentHashMap(); | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ private final Computable<I, O> computable; | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ private final boolean recalculate; | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ public Memoizer(Computable<I, O> computable) | ||
/* */ { | ||
/* 74 */ this(computable, false); | ||
/* */ } | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ public Memoizer(Computable<I, O> computable, boolean recalculate) | ||
/* */ { | ||
/* 91 */ this.computable = computable; | ||
/* 92 */ this.recalculate = recalculate; | ||
/* */ } | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ public O compute(I arg) | ||
/* */ throws InterruptedException | ||
/* */ { | ||
/* */ for (;;) | ||
/* */ { | ||
/* 116 */ Future<O> future = (Future)this.cache.get(arg); | ||
/* 117 */ if (future == null) { | ||
/* 118 */ Callable<O> eval = () -> { return this.computable.compute(arg); } ; | ||
/* 119 */ FutureTask<O> futureTask = new FutureTask(eval); | ||
/* 120 */ future = (Future)this.cache.putIfAbsent(arg, futureTask); | ||
/* 121 */ if (future == null) { | ||
/* 122 */ future = futureTask; | ||
/* 123 */ futureTask.run(); | ||
/* */ } | ||
/* */ } | ||
/* */ try { | ||
/* 127 */ return (O)future.get(); | ||
/* */ } catch (CancellationException e) { | ||
/* 129 */ this.cache.remove(arg, future); | ||
/* */ } catch (ExecutionException e) { | ||
/* 131 */ if (this.recalculate) { | ||
/* 132 */ this.cache.remove(arg, future); | ||
/* */ } | ||
/* */ | ||
/* 135 */ throw launderException(e.getCause()); | ||
/* */ } | ||
/* */ } | ||
/* */ } | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ | ||
/* */ private RuntimeException launderException(Throwable throwable) | ||
/* */ { | ||
/* 151 */ if ((throwable instanceof RuntimeException)) | ||
/* 152 */ return (RuntimeException)throwable; | ||
/* 153 */ if ((throwable instanceof Error)) { | ||
/* 154 */ throw ((Error)throwable); | ||
/* */ } | ||
/* 156 */ throw new IllegalStateException("Unchecked exception", throwable); | ||
/* */ } | ||
/* */ } |