Memoria
Why should an object care about where to store their bytes?
Examples
Basics
RAM can used as a memory storage:
val ram: BytesMemory = RamMemory()
Also a file could:
val cache: BytesMemory = FileMemory(File("//path"))
Restrictions
If you do not want your object to suddenly take to much space:
BoundedBytesMemory(
min = 0, // might be omitted (0 by default)
max = 128,
RamMemory()
)
Thus, once the limit is overflown you'll see IllegalArgumentExcepton
Useful
Primitives
There is a number of adapters to kotlin 'primitives'. These use BytesMemory
to store the value, but acts as memory of their type:
StringMemory
implementsMutableMemory<String>
, so you canwrite("Hello, World")
instread of working with bytes.IntMemory
.
TwinMemory
TwinMemory
class allows you to have two different (or the same... why not) memory sources:
val twin = StringMemory(
TwinMemory(
RamMemory(),
FileMemory(
File("twin.txt")
)
)
)
twin.write("twin me!") // -> will write the value to both sources, ram and file
twin.value() // -> returns value stored in ram, the first (origin) argument of the constructor