package com.parkingexchange.common; public class AssertionConcern { protected…
- User
package com.parkingexchange.common;
public class AssertionConcern {
protected AssertionConcern() { super(); } protected void assertArgumentEquals(Object anObject1, Object anObject2, String aMessage) { if (!anObject1.equals(anObject2)) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentFalse(boolean aBoolean, String aMessage) { if (aBoolean) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentLength(String aString, int aMaximum, String aMessage) { int length = aString.trim().length(); if (length > aMaximum) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentLength(String aString, int aMinimum, int aMaximum, String aMessage) { int length = aString.trim().length(); if (length < aMinimum || length > aMaximum) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentNotEmpty(String aString, String aMessage) { if (aString == null || aString.trim().isEmpty()) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentNotEquals(Object anObject1, Object anObject2, String aMessage) { if (anObject1.equals(anObject2)) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentNotNull(Object anObject, String aMessage) { if (anObject == null) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentRange(double aValue, double aMinimum, double aMaximum, String aMessage) { if (aValue < aMinimum || aValue > aMaximum) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentRange(float aValue, float aMinimum, float aMaximum, String aMessage) { if (aValue < aMinimum || aValue > aMaximum) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentRange(int aValue, int aMinimum, int aMaximum, String aMessage) { if (aValue < aMinimum || aValue > aMaximum) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentRange(long aValue, long aMinimum, long aMaximum, String aMessage) { if (aValue < aMinimum || aValue > aMaximum) { throw new IllegalArgumentException(aMessage); } } protected void assertArgumentTrue(boolean aBoolean, String aMessage) { if (!aBoolean) { throw new IllegalArgumentException(aMessage); } } protected void assertStateFalse(boolean aBoolean, String aMessage) { if (aBoolean) { throw new IllegalStateException(aMessage); } } protected void assertStateTrue(boolean aBoolean, String aMessage) { if (!aBoolean) { throw new IllegalStateException(aMessage); } }} package com.parkingexchange.parking.domain.model;
import com.parkingexchange.common.domain.model.BaseEntity; import jakarta.persistence.*; import jakarta.validation.Valid; import jakarta.validation.constraints.Future; import jakarta.validation.constraints.NotNull;
import java.time.LocalDateTime; import java.util.Objects; import java.util.UUID;
/**
-
Aggregate Root Entity representing the availability of a parking spot. */ @Entity public class Availability extends BaseEntity {
@EmbeddedId private AvailabilityId availabilityId;
@NotNull @Column(name = "leaving_user_id", nullable = false) private UUID leavingUserId;
@NotNull @Valid @Embedded private Location location;
@NotNull @Future(message = "Estimated leaving time must be in the future") @Column(name = "estimated_leaving_time", nullable = false) private LocalDateTime estimatedLeavingTime;
@NotNull @Enumerated(EnumType.STRING) @Column(name = "status", nullable = false) private AvailabilityStatus status;
@Column(name = "reserved_by_user_id") private UUID reservedByUserId;
protected Availability() { // Required by JPA }
private Availability(AvailabilityId availabilityId, UUID leavingUserId, Location location, LocalDateTime estimatedLeavingTime) { this.availabilityId = Objects.requireNonNull(availabilityId, "AvailabilityId cannot be null"); setLeavingUserId(leavingUserId); setLocation(location); setEstimatedLeavingTime(estimatedLeavingTime); this.status = AvailabilityStatus.AVAILABLE; }
// Static factory method for creation public static Availability create(UUID leavingUserId, Location location, LocalDateTime estimatedLeavingTime) { AvailabilityId availabilityId = AvailabilityId.generate(); return new Availability(availabilityId, leavingUserId, location, estimatedLeavingTime); }
// Business methods
/**
- Reserves this availability for a given arriving user.
- @param arrivingUserId The ID of the arriving user. */ public void reserve(UUID arrivingUserId) { this.assertArgumentTrue(this.getStatus().equals(AvailabilityStatus.AVAILABLE), "Only available spots can be reserved"); this.reservedByUserId = Objects.requireNonNull(arrivingUserId, "ArrivingUserId cannot be null"); this.status = AvailabilityStatus.RESERVED; }
/**
- Confirms that the spot has been taken by the arriving user.
- @param arrivingUserId The ID of the arriving user. */ public void confirmTaken(UUID arrivingUserId) { this.assertArgumentTrue(this.getStatus().equals(AvailabilityStatus.RESERVED), "Only reserved spots can be confirmed as taken"); this.assertArgumentTrue(this.getReservedByUserId().equals(arrivingUserId), "This spot is not reserved by the provided user"); this.status = AvailabilityStatus.TAKEN; }
/**
- Cancels this availability. */ public void cancel(UUID userId) { if (!this.leavingUserId.equals(userId) && !Objects.equals(this.reservedByUserId, userId)) { throw new IllegalStateException("You do not have permission to cancel this availability"); } if (this.status == AvailabilityStatus.CANCELED) { throw new IllegalStateException("Availability is already canceled"); } this.status = AvailabilityStatus.CANCELED; }
// Getters and Setters with validation where applicable
public AvailabilityId getAvailabilityId() { return availabilityId; }
public UUID getLeavingUserId() { return leavingUserId; }
public UUID getReservedByUserId(){ return reservedByUserId; }
private void setLeavingUserId(UUID leavingUserId) { this.leavingUserId = Objects.requireNonNull(leavingUserId, "LeavingUserId cannot be null"); }
public Location getLocation() { return location; }
private void setLocation(Location location) { this.location = Objects.requireNonNull(location, "Location cannot be null"); }
public LocalDateTime getEstimatedLeavingTime() { return estimatedLeavingTime; }
private void setEstimatedLeavingTime(LocalDateTime estimatedLeavingTime) { if (estimatedLeavingTime == null) { throw new IllegalArgumentException("Estimated leaving time cannot be null"); } if (estimatedLeavingTime.isBefore(LocalDateTime.now())) { throw new IllegalArgumentException("Estimated leaving time must be in the future"); } this.estimatedLeavingTime = estimatedLeavingTime; }
public AvailabilityStatus getStatus() { return status; }
// equals and hashCode based on 'availabilityId'
@Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false;
Availability that = (Availability) o; return availabilityId.equals(that.availabilityId);}
@Override public int hashCode() { return availabilityId.hashCode(); }
@Override public String toString() { return "Availability{" + "availabilityId=" + availabilityId + ", leavingUserId=" + leavingUserId + ", location=" + location + ", estimatedLeavingTime=" + estimatedLeavingTime + ", status=" + status + ", reservedByUserId=" + reservedByUserId + ", version=" + super.toString() + '}'; } }Can you cotninue modifying availability in order to use the assertion from the abse class
-
- Assistant
Sure! Let's modify the
Availabilityclass to utilize the assertion methods from the base class (AssertionConcern). We'll replace the direct usage ofObjects.requireNonNull,ifstatements that throw exceptions, and any direct exception throwing with the assertion methods provided.First, ensure that
Availabilityhas access to these methods. Since we're extendingBaseEntity, and assuming thatBaseEntityextendsAssertionConcern, we can proceed.Here's the modified
Availabilityclass:package com.parkingexchange.parking.domain.model; import com.parkingexchange.common.domain.model.BaseEntity; import jakarta.persistence.*; import jakarta.validation.Valid; import jakarta.validation.constraints.Future; import jakarta.validation.constraints.NotNull; import java.time.LocalDateTime; import java.util.Objects; import java.util.UUID; /** * Aggregate Root Entity representing the availability of a parking spot. */ @Entity public class Availability extends BaseEntity { @EmbeddedId private AvailabilityId availabilityId; @NotNull @Column(name = "leaving_user_id", nullable = false) private UUID leavingUserId; @NotNull @Valid @Embedded private Location location; @NotNull @Future(message = "Estimated leaving time must be in the future") @Column(name = "estimated_leaving_time", nullable = false) private LocalDateTime estimatedLeavingTime; @NotNull @Enumerated(EnumType.STRING) @Column(name = "status", nullable = false) private AvailabilityStatus status; @Column(name = "reserved_by_user_id") private UUID reservedByUserId; protected Availability() { // Required by JPA } private Availability(AvailabilityId availabilityId, UUID leavingUserId, Location location, LocalDateTime estimatedLeavingTime) { this.assertArgumentNotNull(availabilityId, "AvailabilityId cannot be null"); this.availabilityId = availabilityId; this.setLeavingUserId(leavingUserId); this.setLocation(location); this.setEstimatedLeavingTime(estimatedLeavingTime); this.status = AvailabilityStatus.AVAILABLE; } // Static factory method for creation public static Availability create(UUID leavingUserId, Location location, LocalDateTime estimatedLeavingTime) { AvailabilityId availabilityId = AvailabilityId.generate(); return new Availability(availabilityId, leavingUserId, location, estimatedLeavingTime); } // Business methods /** * Reserves this availability for a given arriving user. * * @param arrivingUserId The ID of the arriving user. */ public void reserve(UUID arrivingUserId) { this.assertArgumentTrue(this.getStatus().equals(AvailabilityStatus.AVAILABLE), "Only available spots can be reserved"); this.assertArgumentNotNull(arrivingUserId, "ArrivingUserId cannot be null"); this.reservedByUserId = arrivingUserId; this.status = AvailabilityStatus.RESERVED; } /** * Confirms that the spot has been taken by the arriving user. * * @param arrivingUserId The ID of the arriving user. */ public void confirmTaken(UUID arrivingUserId) { this.assertArgumentTrue(this.getStatus().equals(AvailabilityStatus.RESERVED), "Only reserved spots can be confirmed as taken"); this.assertArgumentTrue(this.getReservedByUserId().equals(arrivingUserId), "This spot is not reserved by the provided user"); this.status = AvailabilityStatus.TAKEN; } /** * Cancels this availability. */ public void cancel(UUID userId) { boolean hasPermission = this.leavingUserId.equals(userId) || Objects.equals(this.reservedByUserId, userId); this.assertStateTrue(hasPermission, "You do not have permission to cancel this availability"); this.assertStateFalse(this.getStatus() == AvailabilityStatus.CANCELED, "Availability is already canceled"); this.status = AvailabilityStatus.CANCELED; } // Getters and Setters with validation where applicable public AvailabilityId getAvailabilityId() { return availabilityId; } public UUID getLeavingUserId() { return leavingUserId; } public UUID getReservedByUserId(){ return reservedByUserId; } private void setLeavingUserId(UUID leavingUserId) { this.assertArgumentNotNull(leavingUserId, "LeavingUserId cannot be null"); this.leavingUserId = leavingUserId; } public Location getLocation() { return location; } private void setLocation(Location location) { this.assertArgumentNotNull(location, "Location cannot be null"); this.location = location; } public LocalDateTime getEstimatedLeavingTime() { return estimatedLeavingTime; } private void setEstimatedLeavingTime(LocalDateTime estimatedLeavingTime) { this.assertArgumentNotNull(estimatedLeavingTime, "Estimated leaving time cannot be null"); this.assertArgumentTrue(estimatedLeavingTime.isAfter(LocalDateTime.now()), "Estimated leaving time must be in the future"); this.estimatedLeavingTime = estimatedLeavingTime; } public AvailabilityStatus getStatus() { return status; } // equals and hashCode based on 'availabilityId' @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Availability that = (Availability) o; return availabilityId.equals(that.availabilityId); } @Override public int hashCode() { return availabilityId.hashCode(); } @Override public String toString() { return "Availability{" + "availabilityId=" + availabilityId + ", leavingUserId=" + leavingUserId + ", location=" + location + ", estimatedLeavingTime=" + estimatedLeavingTime + ", status=" + status + ", reservedByUserId=" + reservedByUserId + ", version=" + super.toString() + '}'; } }Explanation of Modifications:
-
Constructor Modifications:
- Replaced
Objects.requireNonNullwithassertArgumentNotNullforavailabilityIdin the constructor. - Ensured that
setLeavingUserId,setLocation, andsetEstimatedLeavingTimeare called, which now contain assertions.
- Replaced
-
Setter Methods:
setLeavingUserId(UUID leavingUserId):- Replaced
Objects.requireNonNullwithassertArgumentNotNullto validate thatleavingUserIdis not null.
- Replaced
setLocation(Location location):- Replaced
Objects.requireNonNullwithassertArgumentNotNullto validate thatlocationis not null.
- Replaced
setEstimatedLeavingTime(LocalDateTime estimatedLeavingTime):- Replaced
ifstatements checking for null and future time withassertArgumentNotNullandassertArgumentTruerespectively.
- Replaced
-
Business Methods:
reserve(UUID arrivingUserId):- Replaced
Objects.requireNonNullwithassertArgumentNotNullforarrivingUserId. - Used
assertArgumentTrueto ensure the availability is in the correct status.
- Replaced
confirmTaken(UUID arrivingUserId):- Used
assertArgumentTrueto check status and matchingreservedByUserId.
- Used
cancel(UUID userId):- Used
assertStateTrueandassertStateFalseto validate the user's permission and current status.
- Used
-
Removed Direct Exception Throwing:
- All direct
throw new IllegalArgumentExceptionandthrow new IllegalStateExceptionare replaced by appropriate assertion methods fromAssertionConcern.
- All direct
Assumptions:
BaseEntityextendsAssertionConcern. This allowsAvailabilityto access theassert*methods since they areprotected.- The
AssertionConcernmethods are suitable for the validation scenarios inAvailability. - The
assertArgumentTrueandassertArgumentFalsemethods throwIllegalArgumentException. - The
assertStateTrueandassertStateFalsemethods throwIllegalStateException.
Notes:
- By using the assertion methods, we centralize validation logic and maintain consistency across entities.
- The assertion methods improve code readability and maintainability by reducing boilerplate code.
- Ensure that all dependencies and imports are correctly resolved in your project.
-