-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
64 additions
and
19 deletions.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
modules/logging/api/src/main/scala/sharry/logging/LazyMap.scala
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,40 @@ | ||
package sharry.logging | ||
|
||
import sharry.logging.LazyMap.Val | ||
|
||
final class LazyMap[A, B]( | ||
private val values: Map[A, Val[B]] | ||
) { | ||
lazy val toMap: Map[A, B] = values.view.mapValues(_.value).toMap | ||
|
||
def updated(key: A, value: => B): LazyMap[A, B] = | ||
new LazyMap(values.updated(key, Val(value))) | ||
|
||
def get(key: A): Option[() => B] = | ||
values.get(key).map(e => () => e.value) | ||
|
||
def ++(lm: LazyMap[A, B]): LazyMap[A, B] = | ||
new LazyMap(values ++ lm.values) | ||
|
||
def addMap(m: Map[A, B]): LazyMap[A, B] = | ||
this ++ LazyMap.fromMap(m) | ||
|
||
def toDeferred: Map[A, () => B] = | ||
values.view.mapValues(e => () => e.value).toMap | ||
} | ||
|
||
object LazyMap { | ||
private[this] val emptyMap = new LazyMap[Any, Any](Map.empty) | ||
|
||
def empty[A, B]: LazyMap[A, B] = emptyMap.asInstanceOf[LazyMap[A, B]] | ||
|
||
def fromMap[A, B](m: Map[A, B]): LazyMap[A, B] = | ||
new LazyMap(m.view.mapValues(a => Val(a)).toMap) | ||
|
||
final private class Val[B](v: => B) { | ||
lazy val value = v | ||
} | ||
private object Val { | ||
def apply[B](v: => B): Val[B] = new Val(v) | ||
} | ||
} |
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
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