All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mle.play.io.FileBackedList.scala Maven / Gradle / Ivy

The newest version!
package com.mle.play.io

import java.nio.file.{Files, Path}

import com.mle.file.FileUtilities
import play.api.libs.json.Format
import play.api.libs.json.Json._

/**
 * Persists the list to `file` by serializing at every modification.
 *
 * @param file backing storage
 * @param jsonFormat serialization instructions
 * @tparam T type of element
 */
class FileBackedList[T](file: Path)(implicit val jsonFormat: Format[T]) extends PersistentList[T] {
  override protected def persist(items: Seq[T]): Unit = {
    this.synchronized(FileUtilities.stringToFile(stringify(toJson(items)), file))
  }

  override protected def load(): Seq[T] =
    if (Files.isReadable(file)) {
      parse(this.synchronized(FileUtilities.fileToString(file)))
        .asOpt[Vector[T]]
        .getOrElse(Vector.empty)
    } else {
      Vector.empty
    }
}

class FileBackedSet[T](file: Path)(implicit jsonFormat: Format[T]) extends FileBackedList[T](file) with Distinctness[T]




© 2015 - 2025 Weber Informatics LLC | Privacy Policy