-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
48 additions
and
30 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -1,19 +1,18 @@ | ||
object PetsRepository { | ||
var pets = mutableListOf<Pet>() | ||
|
||
init { | ||
val rocket = Dog(1, "rocket") | ||
val sherlock = Cat(2, "sherlock") | ||
val max = Dog(3, "max") | ||
pets.add(rocket) | ||
pets.add(sherlock) | ||
pets.add(max) | ||
} | ||
|
||
fun findById(id: Int) = pets.first { it.id == id } | ||
fun findByIds(ids: List<Int>) = pets.filter { it.id in ids}.also { | ||
println("Another expensive call to get pets with ids: $ids") | ||
} | ||
|
||
fun allPets() = pets.toList() | ||
|
||
fun addPet(pet: Pet) = | ||
if (pets.find { it.id == pet.id } != null) | ||
throw IllegalArgumentException("Already exists") | ||
else | ||
pets.add(pet) | ||
|
||
fun nextId() = (pets.maxByOrNull { it.id }?.id ?: 0) + 1 | ||
} |
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