From ab1f54c8f4d170614c247af399bb2e218642452a Mon Sep 17 00:00:00 2001 From: YiXuan Ding <1328032567@qq.com> Date: Tue, 5 Nov 2024 15:27:56 +0800 Subject: [PATCH 1/3] : use `equals` to replace `==` to compare `Integer` variable. --- .../springframework/samples/petclinic/owner/PetController.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java index 781fb5805ca..b58c1ba0059 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/PetController.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/PetController.java @@ -138,7 +138,7 @@ public String processUpdateForm(@Valid Pet pet, BindingResult result, Owner owne // checking if the pet name already exist for the owner if (StringUtils.hasText(petName)) { Pet existingPet = owner.getPet(petName.toLowerCase(), false); - if (existingPet != null && existingPet.getId() != pet.getId()) { + if (existingPet != null && existingPet.getId().equals(pet.getId())) { result.rejectValue("name", "duplicate", "already exists"); } } From 7ba387219c1804a18b6020db382a815bcc3e7071 Mon Sep 17 00:00:00 2001 From: YiXuan Ding <1328032567@qq.com> Date: Tue, 5 Nov 2024 16:56:21 +0800 Subject: [PATCH 2/3] : remove redundant 'toLowerCase()' method and simplify pet lookup logic. --- .../java/org/springframework/samples/petclinic/owner/Owner.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java index f8f2871d691..c5667b04465 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -130,7 +130,6 @@ public Pet getPet(Integer id) { * @return a pet if pet name is already in use */ public Pet getPet(String name, boolean ignoreNew) { - name = name.toLowerCase(); for (Pet pet : getPets()) { String compName = pet.getName(); if (compName != null && compName.equalsIgnoreCase(name)) { From cf91405ecb501f7422d72c425f0ad768d677d33b Mon Sep 17 00:00:00 2001 From: YiXuan Ding <1328032567@qq.com> Date: Tue, 5 Nov 2024 17:13:34 +0800 Subject: [PATCH 3/3] : rewrite method `getName()` comments. --- .../org/springframework/samples/petclinic/owner/Owner.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java index c5667b04465..612d89d2dc6 100644 --- a/src/main/java/org/springframework/samples/petclinic/owner/Owner.java +++ b/src/main/java/org/springframework/samples/petclinic/owner/Owner.java @@ -101,7 +101,7 @@ public void addPet(Pet pet) { /** * Return the Pet with the given name, or null if none found for this Owner. * @param name to test - * @return a pet if pet name is already in use + * @return the Pet with the given name, or null if no such Pet exists for this Owner */ public Pet getPet(String name) { return getPet(name, false); @@ -110,7 +110,7 @@ public Pet getPet(String name) { /** * Return the Pet with the given id, or null if none found for this Owner. * @param id to test - * @return a pet if pet id is already in use + * @return the Pet with the given id, or null if no such Pet exists for this Owner */ public Pet getPet(Integer id) { for (Pet pet : getPets()) { @@ -127,7 +127,8 @@ public Pet getPet(Integer id) { /** * Return the Pet with the given name, or null if none found for this Owner. * @param name to test - * @return a pet if pet name is already in use + * @param ignoreNew whether to ignore new pets (pets that are not saved yet) + * @return the Pet with the given name, or null if no such Pet exists for this Owner */ public Pet getPet(String name, boolean ignoreNew) { for (Pet pet : getPets()) {