Simplify signature of InventoryMutateForm::of

This complicates the subclasses’ implementation, but it avoids having
to pass an unnecessary parameter.
This commit is contained in:
Simon Bruder 2024-01-08 14:18:44 +01:00
parent 7365b384e3
commit 8ab01fd65f
Signed by: simon
GPG key ID: 8D3C82F9F309F8EC
4 changed files with 22 additions and 9 deletions

View file

@ -48,7 +48,13 @@ class ConsumableMutateForm extends InventoryMutateForm {
getMetric());
}
public static ConsumableMutateForm of(Consumable product, UniqueInventoryItem item) {
public static ConsumableMutateForm of(UniqueInventoryItem item) {
Consumable product;
if (item.getProduct() instanceof Consumable consumable) {
product = consumable;
} else {
throw new IllegalArgumentException("Item must be for a product of type Consumable");
}
ConsumableMutateForm form = new ConsumableMutateForm();
form.setName(product.getName());
form.setQuantity(item.getQuantity().getAmount().longValueExact());

View file

@ -81,7 +81,7 @@ class InventoryController {
@GetMapping("/inventory/edit/{pid}")
String edit(Model model, @PathVariable Product pid) {
UniqueInventoryItem item = inventory.findByProduct(pid).get();
final InventoryMutateForm form = InventoryMutateForm.of(pid, item);
final InventoryMutateForm form = InventoryMutateForm.of(item);
return edit(model, form);
}

View file

@ -111,13 +111,14 @@ abstract class InventoryMutateForm {
* @param item an {@link UniqueInventoryItem} holding a {@link Quantity}
* @return an object of an {@link InventoryMutateForm} subclass
*/
public static InventoryMutateForm of(Product product, UniqueInventoryItem item) {
if (product instanceof Consumable consumable) {
return ConsumableMutateForm.of(consumable, item);
} else if (product instanceof Rentable rentable) {
return RentableMutateForm.of(rentable, item);
public static InventoryMutateForm of(UniqueInventoryItem item) {
Product product = item.getProduct();
if (product instanceof Consumable) {
return ConsumableMutateForm.of(item);
} else if (product instanceof Rentable) {
return RentableMutateForm.of(item);
} else {
throw new IllegalArgumentException("InventoryMutateForm::ofProductAndItem not supported for given types");
throw new IllegalArgumentException("InventoryMutateForm::of not supported for given types");
}
}

View file

@ -23,7 +23,13 @@ class RentableMutateForm extends InventoryMutateForm {
this.wholesalePrice = wholesalePrice;
}
public static RentableMutateForm of(Rentable product, UniqueInventoryItem item) {
public static RentableMutateForm of(UniqueInventoryItem item) {
Rentable product;
if (item.getProduct() instanceof Rentable rentable) {
product = rentable;
} else {
throw new IllegalArgumentException("Item must be for a product of type Rentable");
}
RentableMutateForm form = new RentableMutateForm();
form.setName(product.getName());
form.setQuantity(item.getQuantity().getAmount().longValueExact());