mirror of
https://github.com/spring-projects/spring-petclinic.git
synced 2025-07-25 01:05:48 +00:00
Refactoring Owner getPets function to use lambda functions
This commit is contained in:
parent
10e3dc9376
commit
ede6d3866d
1 changed files with 14 additions and 22 deletions
|
@ -112,16 +112,13 @@ public class Owner extends Person {
|
||||||
* @param id to test
|
* @param id to test
|
||||||
* @return a pet if pet id is already in use
|
* @return a pet if pet id is already in use
|
||||||
*/
|
*/
|
||||||
public Pet getPet(Integer id) {
|
public Pet getPet(final Integer id) {
|
||||||
for (Pet pet : getPets()) {
|
return getPets().stream()
|
||||||
if (!pet.isNew()) {
|
.filter(pet -> !pet.isNew())
|
||||||
Integer compId = pet.getId();
|
.filter(pet -> pet.getId().equals(id))
|
||||||
if (compId.equals(id)) {
|
.findFirst()
|
||||||
return pet;
|
.orElse(null);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -129,18 +126,13 @@ public class Owner extends Person {
|
||||||
* @param name to test
|
* @param name to test
|
||||||
* @return a pet if pet name is already in use
|
* @return a pet if pet name is already in use
|
||||||
*/
|
*/
|
||||||
public Pet getPet(String name, boolean ignoreNew) {
|
public Pet getPet(final String name, final boolean ignoreNew) {
|
||||||
name = name.toLowerCase();
|
return getPets().stream()
|
||||||
for (Pet pet : getPets()) {
|
.filter(pet -> !ignoreNew && !pet.isNew())
|
||||||
if (!ignoreNew || !pet.isNew()) {
|
.filter(pet -> pet.getName().equals(name.toLowerCase()))
|
||||||
String compName = pet.getName();
|
.findFirst()
|
||||||
compName = compName == null ? "" : compName.toLowerCase();
|
.orElse(null);
|
||||||
if (compName.equals(name)) {
|
|
||||||
return pet;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
Loading…
Reference in a new issue