blob: c3d81c64471809d94cc98a9672286aa932a47c66 (
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
|
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.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
}
}
|