Skip to content

Commit 8fad79b

Browse files
authored
Merge pull request nextgenhealthcare#4 from tonygermano/safe_get
Added code template for Thread-safe get or create from globalMap
2 parents 2d58b77 + 7220ef4 commit 8fad79b

File tree

5 files changed

+108
-0
lines changed

5 files changed

+108
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This Source Code Form is subject to the terms of the Mozilla Public
2+
License, v. 2.0. If a copy of the MPL was not distributed with this
3+
file, You can obtain one at http://mozilla.org/MPL/2.0/.
4+
5+
Files in this subfolder are Copyright Tony Germano 2019.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Thread-safe get or create from globalMap
2+
For a given key, retrieve the value from the globalMap. If the globalMap does not contain the key or the object returned is null produce and store a new object in a thread-safe manner.
3+
4+
### Parameters:
5+
6+
- **globalMapkey:** Key under which this object is stored in the globalMap. Also used as the key for the synchronization object in the globalMap.
7+
- **initializer:** Function which will be called when the value for globalMapKey is null. Returns the object to be stored as the value for globalMapKey.
8+
9+
### Examples
10+
Get synchronized SortedSet stored in the globalMap with key `mySet` or create it if it doesn't exist.
11+
12+
```javascript
13+
with (JavaImporter(java.util)) {
14+
var mySet = getInstance('mySet', function() {
15+
return Collections.synchronizedSortedSet(new TreeSet());
16+
});
17+
}
18+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
For a given key, retrieve the value from the globalMap. If the globalMap does not contain the key or
3+
the object returned is null produce and store a new object in a thread-safe manner.
4+
5+
@param {string} globalMapKey - Key under which this object is stored in the globalMap. Also used
6+
as the key for the synchronization object in the globalMap.
7+
@param {function():any} initializer - Function which will be called when the value for
8+
globalMapKey is null. Returns the object to be stored as the value for globalMapKey.
9+
@return {any} Object previously stored by globalMapKey or object created by initializer.
10+
11+
@author Tony Germano
12+
*/
13+
function getInstance(globalMapKey, initializer) {
14+
var val = globalMap.get(globalMapKey);
15+
if (val == null) {
16+
if (!globalMap.containsKeySync(globalMapKey)) {
17+
globalMap.putSync(globalMapKey,null);
18+
}
19+
try {
20+
globalMap.lock(globalMapKey);
21+
if ((val = globalMap.get(globalMapKey)) == null) {
22+
val = initializer();
23+
globalMap.put(globalMapKey, val);
24+
}
25+
}
26+
finally {
27+
globalMap.unlock(globalMapKey);
28+
}
29+
}
30+
return val;
31+
};
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<codeTemplate version="3.5.1">
2+
<id>d9c5b969-56ad-4a2f-bcec-e6708deea959</id>
3+
<name>getInstance</name>
4+
<revision>7</revision>
5+
<lastModified>
6+
<time>1571360939576</time>
7+
<timezone>GMT</timezone>
8+
</lastModified>
9+
<contextSet>
10+
<delegate>
11+
<contextType>DESTINATION_DISPATCHER</contextType>
12+
<contextType>DESTINATION_FILTER_TRANSFORMER</contextType>
13+
<contextType>DESTINATION_RESPONSE_TRANSFORMER</contextType>
14+
<contextType>SOURCE_RECEIVER</contextType>
15+
<contextType>SOURCE_FILTER_TRANSFORMER</contextType>
16+
</delegate>
17+
</contextSet>
18+
<properties class="com.mirth.connect.model.codetemplates.BasicCodeTemplateProperties">
19+
<type>FUNCTION</type>
20+
<code>/**
21+
For a given key, retrieve the value from the globalMap. If the globalMap does not contain the key or
22+
the object returned is null produce and store a new object in a thread-safe manner.
23+
24+
@param {string} globalMapKey - Key under which this object is stored in the globalMap. Also used
25+
as the key for the synchronization object in the globalMap.
26+
@param {function():any} initializer - Function which will be called when the value for
27+
globalMapKey is null. Returns the object to be stored as the value for globalMapKey.
28+
@return {any} Object previously stored by globalMapKey or object created by initializer.
29+
30+
@author Tony Germano
31+
*/
32+
function getInstance(globalMapKey, initializer) {
33+
var val = globalMap.get(globalMapKey);
34+
if (val == null) {
35+
if (!globalMap.containsKeySync(globalMapKey)) {
36+
globalMap.putSync(globalMapKey,null);
37+
}
38+
try {
39+
globalMap.lock(globalMapKey);
40+
if ((val = globalMap.get(globalMapKey)) == null) {
41+
val = initializer();
42+
globalMap.put(globalMapKey, val);
43+
}
44+
}
45+
finally {
46+
globalMap.unlock(globalMapKey);
47+
}
48+
}
49+
return val;
50+
};
51+
</code>
52+
</properties>
53+
</codeTemplate>

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ This is a repository of example channels, code templates, and other scripts you
2121
- Replace in All Descendant XML Nodes
2222
- Run on Startup
2323
- Strip Empty Nodes from XML
24+
- Thread-safe get or create from globalMap
2425
- Unescape `\Xdd..\` HL7 Sequences
2526

2627
#### Scripts

0 commit comments

Comments
 (0)