sbt/cache/SeparatedCache.scala

67 lines
1.7 KiB
Scala
Raw Normal View History

/* sbt -- Simple Build Tool
* Copyright 2009 Mark Harrah
*/
package sbt
2009-08-16 20:29:08 +02:00
import Types.:+:
import sbinary.{DefaultProtocol, Format, Input, Output => Out}
import DefaultProtocol.ByteFormat
2009-08-16 20:29:08 +02:00
import java.io.{File, InputStream, OutputStream}
trait InputCache[I]
2009-08-16 20:29:08 +02:00
{
type Internal
def convert(i: I): Internal
def read(from: Input): Internal
def write(to: Out, j: Internal): Unit
def equiv: Equiv[Internal]
2009-08-16 20:29:08 +02:00
}
object InputCache
2009-08-31 16:41:59 +02:00
{
implicit def basicInputCache[I](implicit fmt: Format[I], eqv: Equiv[I]): InputCache[I] =
new InputCache[I]
{
type Internal = I
def convert(i: I) = i
def read(from: Input): I = fmt.reads(from)
def write(to: Out, i: I) = fmt.writes(to, i)
def equiv = eqv
}
2011-06-20 21:25:23 +02:00
def lzy[I](mkIn: => InputCache[I]): InputCache[I] =
new InputCache[I]
{
lazy val ic = mkIn
type Internal = ic.Internal
def convert(i: I) = ic convert i
def read(from: Input): ic.Internal = ic.read(from)
def write(to: Out, i: ic.Internal) = ic.write(to, i)
def equiv = ic.equiv
}
2009-08-16 20:29:08 +02:00
}
class BasicCache[I,O](implicit input: InputCache[I], outFormat: Format[O]) extends Cache[I,O]
2009-08-16 20:29:08 +02:00
{
def apply(file: File)(in: I) =
{
val j = input.convert(in)
try { applyImpl(file, j) }
catch { case e: Exception => Right(update(file)(j)) }
}
protected def applyImpl(file: File, in: input.Internal) =
2009-08-16 20:29:08 +02:00
{
Using.fileInputStream(file) { stream =>
val previousIn = input.read(stream)
if(input.equiv.equiv(in, previousIn))
Left(outFormat.reads(stream))
2009-08-16 20:29:08 +02:00
else
Right(update(file)(in))
2009-08-16 20:29:08 +02:00
}
}
protected def update(file: File)(in: input.Internal) = (out: O) =>
2009-08-16 20:29:08 +02:00
{
Using.fileOutputStream(false)(file) { stream =>
input.write(stream, in)
outFormat.writes(stream, out)
2009-08-16 20:29:08 +02:00
}
}
}