-
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.
feat: move DoubleDestructionProtection into separate class
- Loading branch information
1 parent
755c624
commit b484368
Showing
4 changed files
with
47 additions
and
38 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
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
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
31 changes: 31 additions & 0 deletions
31
src/main/java/dev/silenium/compose/gl/util/DoubleDestructionProtection.kt
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,31 @@ | ||
package dev.silenium.compose.gl.util | ||
|
||
import org.slf4j.LoggerFactory | ||
import java.util.concurrent.atomic.AtomicBoolean | ||
|
||
abstract class DoubleDestructionProtection<ID> { | ||
abstract val id: ID | ||
|
||
private val destroyed = AtomicBoolean(false) | ||
private var destructionPoint: Throwable? = null | ||
fun destroy() { | ||
if (destroyed.compareAndSet(false, true)) { | ||
destroyInternal() | ||
destructionPoint = Exception() | ||
} else { | ||
logger.trace( | ||
"{} {} was already destroyed at: {}", | ||
javaClass.simpleName, | ||
id, | ||
destroyed.get(), | ||
Exception(), | ||
) | ||
} | ||
} | ||
|
||
protected abstract fun destroyInternal() | ||
|
||
companion object { | ||
private val logger = LoggerFactory.getLogger(DoubleDestructionProtection::class.java) | ||
} | ||
} |