aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/reading/models/Filter.scala
blob: d14ca63d6ebba221341d3c668ae5db7480d5f106 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package reading.models

trait Filter {
  def filter(book: Book): Boolean
  def kind: FilterKind
  def nonFormattedName: String
  def name: String
}

object Filter {
  def apply[T](in: T)(implicit filterFactory: FilterFactory[T]): Filter =
    filterFactory.create(in)

  def apply(kind: FilterKind, nonFormattedName: String): Option[Filter] =
    kind match {
      case FilterKind.Period => Period.withNameOption(nonFormattedName).map(apply[Period])
      case FilterKind.GroupedTheme => GroupedTheme.withNameOption(nonFormattedName).map(apply[GroupedTheme])
      case FilterKind.Theme => Theme.withNameOption(nonFormattedName).map(apply[Theme])
      case FilterKind.Genre => Genre.withNameOption(nonFormattedName).map(apply[Genre])
      case FilterKind.Level => Level.withNameOption(nonFormattedName).map(apply[Level])
      case FilterKind.Program => Program.withNameOption(nonFormattedName).map(apply[Program])
      case FilterKind.Grade => Grade.withNameOption(nonFormattedName).map(apply[Grade])
    }

  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

  def remove(fs: Seq[Filter], rf: Filter): Seq[Filter] =
    fs.filterNot { f =>
      (equals(f, rf)
        || rf.kind == FilterKind.Grade && f.kind == FilterKind.Program
        || rf.kind == FilterKind.GroupedTheme && f.kind == FilterKind.Theme)
    }
}