This repository has been archived by the owner on Oct 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(desktop): Add interface Book and its implementation BookImpl
- Loading branch information
1 parent
c26e9b0
commit ff38d6c
Showing
4 changed files
with
126 additions
and
70 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
desktopDomain/src/main/kotlin/com/intelligentbackpack/desktopdomain/entities/Book.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package com.intelligentbackpack.desktopdomain.entities | ||
|
||
import com.intelligentbackpack.desktopdomain.entities.implementations.BookImpl | ||
import com.intelligentbackpack.desktopdomain.exception.ISBNException | ||
import com.intelligentbackpack.desktopdomain.policies.ISBNPolicy | ||
|
||
interface Book { | ||
/** | ||
* The ISBN of the book. | ||
*/ | ||
val isbn: String | ||
|
||
/** | ||
* The title of the book. | ||
*/ | ||
val title: String | ||
|
||
/** | ||
* The authors of the book. | ||
*/ | ||
val authors: Set<String> | ||
|
||
companion object { | ||
|
||
/** | ||
* Builds a book copy. | ||
* | ||
* @param block The builder block. | ||
* @return The book copy built. | ||
* @throws IllegalArgumentException If the book title is black or the authors are blanck. | ||
* @throws ISBNException If the ISBN of the book is not valid. | ||
* @throws IllegalStateException If not all the properties are initialized. | ||
*/ | ||
inline fun build( | ||
block: Builder.() -> Unit | ||
): Book = Builder().apply(block).build() | ||
} | ||
|
||
class Builder { | ||
|
||
/** | ||
* The ISBN of the book. | ||
*/ | ||
lateinit var isbn: String | ||
|
||
/** | ||
* The title of the book. | ||
*/ | ||
lateinit var title: String | ||
|
||
/** | ||
* The authors of the book. | ||
*/ | ||
lateinit var authors: Set<String> | ||
|
||
/** | ||
* Builds the school supply. | ||
* | ||
* @return The school supply built. | ||
* @throws IllegalStateException If not all properties are initialized. | ||
* @throws IllegalArgumentException for any other reason. | ||
* @throws ISBNException If the ISBN of the book is not valid. | ||
*/ | ||
@Throws( | ||
IllegalStateException::class, | ||
ISBNException::class, | ||
IllegalArgumentException::class | ||
) | ||
fun build(): Book = | ||
if (this::isbn.isInitialized && | ||
this::title.isInitialized && | ||
this::authors.isInitialized | ||
) | ||
if (title.isNotBlank() && | ||
authors.all { it.isNotBlank() } | ||
) | ||
if (ISBNPolicy.isValid(isbn)) { | ||
BookImpl( | ||
isbn = isbn, | ||
title = title, | ||
authors = authors | ||
) | ||
} else | ||
throw ISBNException() | ||
else | ||
throw IllegalStateException("Not all properties are initialized") | ||
else | ||
throw IllegalStateException("Not all properties are initialized") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
...rc/main/kotlin/com/intelligentbackpack/desktopdomain/entities/implementations/BookImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.intelligentbackpack.desktopdomain.entities.implementations | ||
|
||
import com.intelligentbackpack.desktopdomain.entities.Book | ||
|
||
data class BookImpl( | ||
override val isbn: String | ||
) : Book { | ||
override lateinit var title: String | ||
private set | ||
|
||
override lateinit var authors: Set<String> | ||
private set | ||
|
||
constructor(isbn: String, title: String, authors: Set<String>) : this(isbn) { | ||
this.title = title | ||
this.authors = authors | ||
} | ||
} |