From 5bbda9cf69ce62267d2abc2491dc0012c167b02f Mon Sep 17 00:00:00 2001 From: Ethan Atkins Date: Sun, 25 Nov 2018 13:02:21 -0800 Subject: [PATCH] Add Repository interface I am going to be introducing multiple caches throughout sbt and I am going to build these features out using this simple Repository interface. The idea is that we access data by some key through the Repository. This allows us to use the strategy pattern to easily switch the runtime implementation of how to get the data. --- .../main/scala/sbt/internal/Repository.scala | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 main/src/main/scala/sbt/internal/Repository.scala diff --git a/main/src/main/scala/sbt/internal/Repository.scala b/main/src/main/scala/sbt/internal/Repository.scala new file mode 100644 index 000000000..239dd02ef --- /dev/null +++ b/main/src/main/scala/sbt/internal/Repository.scala @@ -0,0 +1,34 @@ +/* + * sbt + * Copyright 2011 - 2018, Lightbend, Inc. + * Copyright 2008 - 2010, Mark Harrah + * Licensed under Apache License 2.0 (see LICENSE) + */ + +package sbt.internal + +/** + * Represents an abstract cache of values, accessible by a key. The interface is deliberately + * minimal to give maximum flexibility to the implementation classes. For example, one can construct + * a cache from a `sbt.io.FileTreeRepository` that automatically registers the paths with the + * cache (but does not clear the cache on close): + * {{{ + * val repository = sbt.io.FileTreeRepository.default(_.getPath) + * val fileCache = new Repository[Seq, (Path, Boolean), TypedPath] { + * override def get(key: (Path, Boolean)): Seq[TypedPath] = { + * val (path, recursive) = key + * val depth = if (recursive) Int.MaxValue else 0 + * repository.register(path, depth) + * repository.list(path, depth, AllPass) + * } + * override def close(): Unit = {} + * } + * }}} + * + * @tparam M the container type of the cache. This will most commonly be `Option` or `Seq`. + * @tparam K the key type + * @tparam V the value type + */ +trait Repository[M[_], K, V] extends AutoCloseable { + def get(key: K): M[V] +}