-
Notifications
You must be signed in to change notification settings - Fork 274
/
Copy pathThreadLocalOptionsExtension.java
64 lines (54 loc) · 2 KB
/
ThreadLocalOptionsExtension.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package com.vladsch.flexmark.java.samples;
import com.vladsch.flexmark.html.RendererBuilder;
import com.vladsch.flexmark.html.RendererExtension;
import com.vladsch.flexmark.parser.Parser;
import com.vladsch.flexmark.util.data.DataSet;
import com.vladsch.flexmark.util.data.MutableDataHolder;
import org.jetbrains.annotations.NotNull;
public class ThreadLocalOptionsExtension implements
Parser.ParserExtension,
RendererExtension
{
final private static ThreadLocal<MutableDataHolder> threadOptions = new ThreadLocal<>();
private ThreadLocalOptionsExtension() {
}
/**
* Set the per thread options for flexmark-java.
* <p>
* Must be called from the thread to set options to be set for
* all flexmark-java code which uses this extension.
* <p>
* CAUTION: Do not try to make mutable data values shared.
* setAll() copies values of keys so immutable data valued
* keys: Boolean, String, Integer, enums, etc., are good to
* go as is.
* <p>
* For mutable values you will need to add extra code to address
* these or you will be hunting down overwrites of data from
* another thread.
*
* @param perThreadOptions data set of the per thread options
*/
public static void setThreadOptions(DataSet perThreadOptions) {
threadOptions.get().setAll(perThreadOptions);
}
public static ThreadLocalOptionsExtension create() {
return new ThreadLocalOptionsExtension();
}
@Override
public void extend(Parser.Builder parserBuilder) {
}
@Override
public void parserOptions(MutableDataHolder options) {
// copy thread local options to builder
options.setAll(threadOptions.get());
}
@Override
public void rendererOptions(@NotNull MutableDataHolder options) {
// copy thread local options to builder
options.setAll(threadOptions.get());
}
@Override
public void extend(@NotNull RendererBuilder rendererBuilder, @NotNull String rendererType) {
}
}