From 7252256d0a8b1d5fe14bd2f9c785318f3bc72f04 Mon Sep 17 00:00:00 2001 From: Andrea Brighi Date: Wed, 10 May 2023 20:52:05 +0200 Subject: [PATCH] feat(desktop): create abstract class SchoolSupplyBuilder abstract builder for BookCopy and GeneralSchoolSupply --- .../entities/SchoolSupplyBuilder.kt | 97 +++++++++++++++++++ 1 file changed, 97 insertions(+) create mode 100644 desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/SchoolSupplyBuilder.kt diff --git a/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/SchoolSupplyBuilder.kt b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/SchoolSupplyBuilder.kt new file mode 100644 index 00000000..15741b31 --- /dev/null +++ b/desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/SchoolSupplyBuilder.kt @@ -0,0 +1,97 @@ +package com.intelligentbackpack.desktopdomain.entities + +import com.intelligentbackpack.desktopdomain.exception.RFIDFormatException +import com.intelligentbackpack.desktopdomain.exception.ReplaceException +import com.intelligentbackpack.desktopdomain.exception.SubjectException +import com.intelligentbackpack.desktopdomain.exception.TypeException +import com.intelligentbackpack.desktopdomain.policies.RFIDPolicy +import com.intelligentbackpack.desktopdomain.policies.ReplacePolicy + +/** + * Builder for a school supply. + * + * @param T The type of the school supply. + * @property types The types of the school supplies that can be built. + * @property checkSubjects The subjects that are available. + * + */ +abstract class SchoolSupplyBuilder( + private val types: List, + private val checkSubjects: Set, +) { + /** + * The type of the school supply. + */ + lateinit var type: SchoolSupplyType + + /** + * The RFID code of the school supply. + */ + lateinit var rfidCode: String + + /** + * The subjects of the school supply. + */ + lateinit var subjects: Set + + /** + * The school supplies that replace this one. + */ + var replacedBy: Set = setOf() + + /** + * The school supplies that this one replaces. + */ + var replace: Set = setOf() + + /** + * Builds the specific school supply. + */ + abstract fun specificBuilder(): T + + /** + * Builds the school supply. + * + * @return The school supply built. + * @throws IllegalStateException If not all properties are initialized. + * @throws TypeException If the type of the school supply is not valid. + * @throws RFIDFormatException If the RFID code of the school supply is not valid. + * @throws SubjectException If the subjects of the school supply are not valid. + * @throws ReplaceException If the school supplies that replace this one or the school supplies that this one replaces are not valid. + * @throws IllegalArgumentException for any other reason. + */ + @Throws( + IllegalStateException::class, + TypeException::class, + RFIDFormatException::class, + SubjectException::class, + ReplaceException::class, + IllegalArgumentException::class + ) + open fun build(): T = + if (this::type.isInitialized && + this::rfidCode.isInitialized && + this::subjects.isInitialized + ) + if (types.contains(type)) + if (RFIDPolicy.isValid(rfidCode)) + if (subjects.all { checkSubjects.contains(it) } || + (subjects.contains(Subjects.ALL) && subjects.size == 1) + ) { + val built = specificBuilder() + if (ReplacePolicy.isValid(setOf(built), replace) || replace.isEmpty()) + if (ReplacePolicy.isValid(replacedBy, setOf(built)) || replacedBy.isEmpty()) + built + else + throw ReplaceException() + else + throw ReplaceException() + } else + throw SubjectException(subjects) + else + throw RFIDFormatException() + else + throw TypeException(type) + else + throw IllegalStateException("Not all properties are initialized") +}