aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/reading/models/Filter.scala
diff options
context:
space:
mode:
Diffstat (limited to 'src/main/scala/reading/models/Filter.scala')
-rw-r--r--src/main/scala/reading/models/Filter.scala38
1 files changed, 33 insertions, 5 deletions
diff --git a/src/main/scala/reading/models/Filter.scala b/src/main/scala/reading/models/Filter.scala
index d14ca63..7ec6340 100644
--- a/src/main/scala/reading/models/Filter.scala
+++ b/src/main/scala/reading/models/Filter.scala
@@ -5,6 +5,16 @@ trait Filter {
def kind: FilterKind
def nonFormattedName: String
def name: String
+
+ override def equals(that: Any): Boolean =
+ that match {
+ case that: Filter =>
+ this.kind == that.kind && this.name == that.name
+ case _ =>
+ false
+ }
+
+ override def hashCode: Int = this.kind.hashCode + this.nonFormattedName.hashCode
}
object Filter {
@@ -23,15 +33,33 @@ object Filter {
}
def contains(filters: Seq[Filter], filter: Filter): Boolean =
- filters.find(equals(_, filter)).nonEmpty
-
- def equals(f1: Filter, f2: Filter): Boolean =
- f1.kind == f2.kind && f1.name == f2.name
+ filters.find(_ == filter).nonEmpty
def remove(fs: Seq[Filter], rf: Filter): Seq[Filter] =
fs.filterNot { f =>
- (equals(f, rf)
+ (f == rf
|| rf.kind == FilterKind.Grade && f.kind == FilterKind.Program
|| rf.kind == FilterKind.GroupedTheme && f.kind == FilterKind.Theme)
}
+
+ val onBooks: Map[Filter, Seq[Book]] =
+ Seq(
+ Grade.values.map(Filter.apply(_)),
+ Program.values.map(Filter.apply(_)),
+ Theme.values.map(Filter.apply(_)),
+ GroupedTheme.values.map(Filter.apply(_)),
+ Genre.values.map(Filter.apply(_)),
+ Level.values.map(Filter.apply(_)),
+ Period.values.map(Filter.apply(_))
+ )
+ .flatten
+ .map(f => (f, Books().filter(f.filter)))
+ .toMap
+
+ def add(books: Seq[Book], filters: Seq[Filter]): Seq[Book] =
+ filters.foldLeft(books)(add)
+
+ def add(books: Seq[Book], filter: Filter): Seq[Book] =
+ books.intersect(onBooks.getOrElse(filter, Nil))
+
}