Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
mio-19 committed Dec 30, 2024
1 parent a958c0f commit c9104df
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions utils/shared/src/main/scala/chester/utils/Parameter.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package chester.utils

import java.util.function.Supplier

class Parameter[T](default: => (T | Null) = null) {
val tl: ThreadLocal[T | Null] = ThreadLocal.withInitial(() => default)

def withValue[U](value: T)(block: => U): U = {
val previousValue = tl.get()
try {
tl.set(value)
block
} finally {
tl.set(previousValue)
}
}
def get: T = tl.get() match {
case null => throw new IllegalStateException("Value is null")
case value => value.asInstanceOf[T]
}
}

0 comments on commit c9104df

Please sign in to comment.