Skip to content

Instantly share code, notes, and snippets.

@andersonbosa
Last active January 14, 2025 19:53
Show Gist options
  • Save andersonbosa/24f9876e63700d0934fdc32410e2c435 to your computer and use it in GitHub Desktop.
Save andersonbosa/24f9876e63700d0934fdc32410e2c435 to your computer and use it in GitHub Desktop.
Implementation of "update" method in a "in memory" repository.
@Override
public Vehicle update(Long id, UpdateVehicleDto dto) {
Vehicle vehicleToUpdate = findById(id).orElse(null);
if (vehicleToUpdate == null) {
return null;
}
// method 1
if (dto.getBrand() != null) vehicleToUpdate.setBrand(dto.getBrand());
if (dto.getModel() != null) vehicleToUpdate.setModel(dto.getModel());
if (dto.getRegistration() != null) vehicleToUpdate.setRegistration(dto.getRegistration());
if (dto.getColor() != null) vehicleToUpdate.setColor(dto.getColor());
if (dto.getYear() != null) vehicleToUpdate.setYear(dto.getYear());
if (dto.getMax_speed() != null) vehicleToUpdate.setMax_speed(dto.getMax_speed());
if (dto.getPassengers() != null) vehicleToUpdate.setPassengers(dto.getPassengers());
if (dto.getFuel_type() != null) vehicleToUpdate.setFuel_type(dto.getFuel_type());
if (dto.getTransmission() != null) vehicleToUpdate.setTransmission(dto.getTransmission());
if (dto.getLength() != null) vehicleToUpdate.setLength(dto.getLength());
if (dto.getWidth() != null) vehicleToUpdate.setWidth(dto.getWidth());
if (dto.getWeight() != null) vehicleToUpdate.setWeight(dto.getWeight());
// method 2
dto.iterable(false)
.forEach(
(key, newValue) -> {
System.out.printf("==========\n%s: %s%n", key, newValue);
switch (key) {
case "brand":
vehicleToUpdate.setBrand(newValue.toString());
break;
case "model":
vehicleToUpdate.setModel(newValue.toString());
break;
case "registration":
vehicleToUpdate.setRegistration(newValue.toString());
break;
case "color":
vehicleToUpdate.setColor(newValue.toString());
break;
case "year":
vehicleToUpdate.setYear(Integer.parseInt(newValue.toString()));
break;
case "max_speed":
vehicleToUpdate.setMax_speed(newValue.toString());
break;
case "passengers":
vehicleToUpdate.setPassengers(Integer.parseInt(newValue.toString()));
break;
case "fuel_type":
vehicleToUpdate.setFuel_type(newValue.toString());
break;
case "transmission":
vehicleToUpdate.setTransmission(newValue.toString());
break;
case "length":
vehicleToUpdate.setLength(Double.valueOf(newValue.toString()));
break;
case "width":
vehicleToUpdate.setWidth(Double.valueOf(newValue.toString()));
break;
case "weight":
vehicleToUpdate.setWeight(Double.valueOf(newValue.toString()));
break;
}
}
);
return vehicleToUpdate;
}
@andersonbosa
Copy link
Author

DTO Should have that method .iterable

public Map<String, ?> iterable(boolean getNullValues) {
        Map<String, ?> m = new HashMap<>() {{
            put("brand", brand);
            put("model", model);
            put("registration", registration);
            put("color", color);
            put("year", year);
            put("max_speed", max_speed);
            put("passengers", passengers);
            put("fuel_type", fuel_type);
            put("transmission", transmission);
            put("length", length);
            put("width", width);
            put("weight", weight);
        }};

        if (!getNullValues) {
            m.values().removeIf(Objects::isNull);
        }

        return m;
    }

@andersonbosa
Copy link
Author

Yep, this is a overengineering.

Study purposes

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment