MCPcopy Create free account
hub / github.com/Snapchat/Valdi / InternedStringCPP

Class InternedStringCPP

valdi/src/java/com/snap/valdi/utils/InternedStringCPP.kt:5–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

3import com.snapchat.client.valdi.utils.NativeHandleWrapper
4
5internal class InternedStringCPP: NativeHandleWrapper, InternedString {
6
7 constructor(str: String): this(str, true)
8
9 constructor(str: String, createHandleLazily: Boolean): super(0) {
10 if (createHandleLazily) {
11 javaString = str
12 needsHandle = true
13 } else {
14 javaString = null
15 needsHandle = false
16 nativeHandle = nativeCreate(str)
17 }
18 }
19
20 constructor(handle: Long): super(handle) {
21 javaString = null
22 needsHandle = false
23 nativeRetain(handle)
24 }
25
26 // We keep a reference to the Java string until we actually need to create the interned string
27 private var javaString: String?
28 private var needsHandle: Boolean
29
30 override fun toString(): String {
31 return javaString ?: nativeToString(nativeHandle)
32 }
33
34 fun cacheJavaString() {
35 if (javaString == null) {
36 javaString = nativeToString(nativeHandle)
37 }
38 }
39
40 override fun destroyHandle(handle: Long) {
41 nativeRelease(handle)
42 }
43
44 override fun getNativeHandle(): Long {
45 if (needsHandle) {
46 synchronized(this) {
47 if (needsHandle) {
48 needsHandle = false
49 val javaString = this.javaString
50 if (javaString != null) {
51 this.javaString = null
52 nativeHandle = nativeCreate(javaString)
53 }
54 }
55 }
56 }
57
58 return super.getNativeHandle()
59 }
60
61 override fun hashCode(): Int {
62 return nativeHandle.toInt()

Callers 3

createMethod · 0.70
getInternedStringMethod · 0.70

Calls 1

nativeCreateFunction · 0.85

Tested by

no test coverage detected