Skip to content
This repository has been archived by the owner on Oct 23, 2023. It is now read-only.

Commit

Permalink
refactor(desktop): remove logic of classes removed
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreaBrighi committed May 11, 2023
1 parent fd5c2d1 commit 68c099d
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 344 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ package com.intelligentbackpack.desktopdomain.entities

import com.intelligentbackpack.desktopdomain.entities.implementations.BookCopyImpl
import com.intelligentbackpack.desktopdomain.exception.ISBNException
import com.intelligentbackpack.desktopdomain.exception.RFIDFormatException
import com.intelligentbackpack.desktopdomain.exception.TypeException
import com.intelligentbackpack.desktopdomain.policies.ISBNPolicy
import com.intelligentbackpack.desktopdomain.policies.RFIDPolicy
import com.intelligentbackpack.desktopdomain.policies.ReplacePolicy
import java.lang.IllegalArgumentException

/**
* Interface for a book copy.
*/
interface BookCopy : MutableSchoolSupply<BookCopy> {
interface BookCopy : SchoolSupply {
/**
* The ISBN of the book.
*/
Expand All @@ -31,20 +31,16 @@ interface BookCopy : MutableSchoolSupply<BookCopy> {
/**
* Builds a book copy.
*
* @param checkSubjects The subjects that are available.
* @param block The builder block.
* @return The book copy built.
* @throws IllegalArgumentException If the book copy is invalid
* ( the [isbn] doesn't match with the [ISBNPolicy],
* the [rfidCode] doesn't match with the [RFIDPolicy],
* the [subjects] aren't in the [checkSubjects],
* [replace] or [replacedBy] doesn't respect the [ReplacePolicy]).
* @throws IllegalStateException If not all the properties are initialized.
*/
inline fun build(
checkSubjects: Set<Subject>,
block: Builder.() -> Unit
): BookCopy = Builder(checkSubjects).apply(block).build()
): BookCopy = Builder().apply(block).build()
}

/**
Expand All @@ -54,9 +50,13 @@ interface BookCopy : MutableSchoolSupply<BookCopy> {
* @property checkSubjects The subjects that are available.
*
*/
class Builder(
checkSubjects: Set<Subject>
) : SchoolSupplyBuilder<BookCopy>(listOf(SchoolSupplyTypes.BOOK), checkSubjects) {
class Builder {

/**
* The rfid of the school supplies.
*/
lateinit var rfidCode: String

/**
* The ISBN of the book.
*/
Expand All @@ -72,47 +72,56 @@ interface BookCopy : MutableSchoolSupply<BookCopy> {
*/
lateinit var authors: List<String>

override fun build(): BookCopy {
type = SchoolSupplyTypes.BOOK
return super.build()
}
/**
* The type of the school supply.
*/
var type: SchoolSupplyType = SchoolSupplyTypes.BOOK

/**
* Builds the book copy.
* Builds the school supply.
*
* @return The book copy built.
* @throws IllegalArgumentException If the book copy is invalid
* @throws ISBNException If the ISBN is not valid.
* @throws IllegalStateException If not all the properties are initialized.
* @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 IllegalArgumentException for any other reason.
* @throws ISBNException If the ISBN of the book is not valid.
*/
@Throws(IllegalArgumentException::class, IllegalStateException::class, ISBNException::class)
override fun specificBuilder(): BookCopy =
if (this::isbn.isInitialized &&
@Throws(
IllegalStateException::class,
TypeException::class,
RFIDFormatException::class,
IllegalArgumentException::class
)
fun build(): BookCopy =
if (this::rfidCode.isInitialized &&
this::isbn.isInitialized &&
this::title.isInitialized &&
this::authors.isInitialized
)
if (title.isNotBlank() &&
authors.isNotEmpty() &&
authors.all { it.isNotBlank() }
)
if (ISBNPolicy.isValid(isbn)) {
if (type == SchoolSupplyTypes.BOOK)
if (RFIDPolicy.isValid(rfidCode))

with(this) {
BookCopyImpl(
rfidCode = rfidCode,
subjects = subjects,
isbn = isbn,
title = title,
authors = authors,
replacedBy = replacedBy,
replace = replace
)
}
} else
throw ISBNException()
if (ISBNPolicy.isValid(isbn)) {
BookCopyImpl(
rfidCode = rfidCode,
isbn = isbn,
title = title,
authors = authors
)
} else
throw ISBNException()
else
throw RFIDFormatException()
else
throw TypeException(type)
else
throw IllegalArgumentException("Not all required fields are initialized")
throw IllegalStateException("Not all properties are initialized")
else
throw IllegalStateException("Not all required fields are initialized")
throw IllegalStateException("Not all properties are initialized")
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,13 @@
package com.intelligentbackpack.desktopdomain.entities

import com.intelligentbackpack.desktopdomain.entities.implementations.DesktopImpl
import com.intelligentbackpack.desktopdomain.exception.AlreadyPresentType
import com.intelligentbackpack.desktopdomain.exception.DuplicateBackpack
import com.intelligentbackpack.desktopdomain.exception.DuplicateRFIDException
import com.intelligentbackpack.desktopdomain.exception.ReplaceException
import com.intelligentbackpack.desktopdomain.exception.TypeException

/**
* Interface for a desktop.
*/
interface Desktop {
/**
* The backpacks of the desktop.
*/
val backpacks: Set<Backpack>

/**
* The school supplies of the desktop.
Expand All @@ -26,22 +19,6 @@ interface Desktop {
*/
val schoolSupplyTypes: Set<SchoolSupplyType>

/**
* The subjects of the school supplies of the desktop.
*/
val subjects: Set<Subject>

/**
* Adds a backpack to the desktop.
*
* @param backpack The backpack to add.
* @return The desktop with the backpack added.
*
* @throws DuplicateBackpack If the backpack is already present.
*/
@Throws(DuplicateBackpack::class)
fun addBackpack(backpack: Backpack): Desktop

/**
* Adds a school supply to the desktop.
*
Expand All @@ -54,61 +31,19 @@ interface Desktop {
@Throws(TypeException::class, DuplicateRFIDException::class)
fun addSchoolSupply(schoolSupply: SchoolSupply): Desktop

/**
* Adds a school supply type to the desktop.
*
* @param schoolSupplyType The school supply type to add.
* @return The desktop with the school supply type added.
*
* @throws AlreadyPresentType If the school supply type is already present.
*/
@Throws(AlreadyPresentType::class)
fun addSchoolSupplyType(schoolSupplyType: SchoolSupplyType): Desktop

/**
* Adds a subject to the desktop.
*
* @param subject The subject to add.
* @return The desktop with the subject added.
*/
fun addSubject(subject: Subject): Desktop

/**
* The school supplies that replace the given school supplies.
* Replaced means that the given school supplies are replaced as default material by the returned school supplies.
*
* @param T The type of the school supplies.
* @param newSchoolSupplies The new school supplies.
* @param oldSchoolSupplies The old school supplies that are replaced.
* @return The school supplies that replace the given school supplies.
* @throws ReplaceException If the old school supplies can not be replace the new ones.
*/
@Throws(ReplaceException::class)
fun <T : MutableSchoolSupply<T>> replaceSchoolSupplies(
newSchoolSupplies: Set<T>,
oldSchoolSupplies: Set<T>
): Desktop

companion object {
/**
* Builds a desktop.
*
* @param backpacks The backpacks of the desktop.
* @param schoolSupplies The school supplies of the desktop.
* @param schoolSupplyTypes The types of the school supplies of the desktop.
* @return The desktop built.
*/
fun create(
backpacks: Set<Backpack>,
schoolSupplies: Set<SchoolSupply>,
schoolSupplyTypes: Set<SchoolSupplyType>,
subjects: Set<Subject>
schoolSupplies: Set<SchoolSupply> = emptySet()
): Desktop =
DesktopImpl(
backpacks,
schoolSupplies,
schoolSupplyTypes + SchoolSupplyTypes.BOOK,
(subjects + Subjects.ALL).toSet()
setOf(SchoolSupplyTypes.BOOK),
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,4 @@ interface SchoolSupply {
* The RFID code of the school supply.
*/
val rfidCode: String

/**
* The subjects of the school supply.
*/
val subjects: Set<Subject>

/**
* Adds the given subjects to the school supply.
* @param subjects The subjects to add.
* @return The school supply with the given subjects added.
*/
fun addSubjects(subjects: Set<Subject>): SchoolSupply
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,37 @@ package com.intelligentbackpack.desktopdomain.entities.implementations
import com.intelligentbackpack.desktopdomain.entities.BookCopy
import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyType
import com.intelligentbackpack.desktopdomain.entities.SchoolSupplyTypes
import com.intelligentbackpack.desktopdomain.entities.Subject

/**
* Implementation of a book copy.
*
* @property rfidCode The RFID code of the book copy.
* @property subjects The subjects of the book copy.
* @property isbn The ISBN of the book copy.
* @property title The title of the book copy.
* @property authors The authors of the book copy.
* @property replacedBy The book copies that replace this one.
* @property replace The book copies that this one replaces.
* @property type is automatically set to [SchoolSupplyTypes.BOOK].
*/
internal data class BookCopyImpl(
override val rfidCode: String,
) : AbstractSchoolSupply<BookCopy>(), BookCopy {
) : BookCopy {

override lateinit var subjects: Set<Subject>
private set
override lateinit var isbn: String
private set
override lateinit var title: String
private set
override lateinit var authors: List<String>
private set
override var replacedBy: Set<BookCopy> = setOf()
private set
override var replace: Set<BookCopy> = setOf()
private set

constructor(
rfidCode: String,
subjects: Set<Subject>,
isbn: String,
title: String,
authors: List<String>,
replacedBy: Set<BookCopy>,
replace: Set<BookCopy>
) : this(rfidCode) {
this.subjects = subjects
this.isbn = isbn
this.title = title
this.authors = authors
this.replacedBy = replacedBy
this.replace = replace
}

override val type: SchoolSupplyType = SchoolSupplyTypes.BOOK
override fun copy(
type: SchoolSupplyType,
rfidCode: String,
subjects: Set<Subject>,
replacedBy: Set<BookCopy>,
replace: Set<BookCopy>
): BookCopy =
BookCopyImpl(
rfidCode = rfidCode,
subjects = subjects,
isbn = isbn,
title = title,
authors = authors,
replacedBy = replacedBy,
replace = replace
)
}
Loading

0 comments on commit 68c099d

Please sign in to comment.