Skip to content

Commit

Permalink
chore: clean codes
Browse files Browse the repository at this point in the history
  • Loading branch information
hantsy committed Jan 7, 2025
1 parent 28f2de2 commit f97e402
Show file tree
Hide file tree
Showing 39 changed files with 450 additions and 439 deletions.
17 changes: 0 additions & 17 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -321,23 +321,6 @@
</annotationProcessorPaths>
</configuration>
</plugin>
<plugin>
<groupId>com.spotify.fmt</groupId>
<artifactId>fmt-maven-plugin</artifactId>
<version>2.25</version>
<configuration>
<verbose>true</verbose>
<style>aosp</style>
</configuration>
<executions>
<execution>
<phase>validate</phase>
<goals>
<goal>check</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.eclipse.cargotracker.application.internal;

import jakarta.ejb.Stateless;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

import org.eclipse.cargotracker.application.BookingService;
import org.eclipse.cargotracker.domain.model.cargo.*;
Expand All @@ -16,7 +17,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

@Stateless
@ApplicationScoped
@Transactional
public class DefaultBookingService implements BookingService {

@Inject private CargoRepository cargoRepository;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
package org.eclipse.cargotracker.application.internal;

import jakarta.ejb.Stateless;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.enterprise.event.Event;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

import org.eclipse.cargotracker.application.ApplicationEvents;
import org.eclipse.cargotracker.application.CargoInspectionService;
Expand All @@ -16,7 +17,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

@Stateless
@ApplicationScoped
@Transactional
public class DefaultCargoInspectionService implements CargoInspectionService {

private static final Logger LOGGER =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.eclipse.cargotracker.application.internal;

import jakarta.ejb.Stateless;
import jakarta.enterprise.context.ApplicationScoped;
import jakarta.inject.Inject;
import jakarta.transaction.Transactional;

import org.eclipse.cargotracker.application.ApplicationEvents;
import org.eclipse.cargotracker.application.HandlingEventService;
Expand All @@ -17,7 +18,8 @@
import java.util.logging.Level;
import java.util.logging.Logger;

@Stateless
@ApplicationScoped
@Transactional
public class DefaultHandlingEventService implements HandlingEventService {

private static final Logger LOGGER =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private boolean calculateMisdirectionStatus(Itinerary itinerary) {

private LocalDateTime calculateEta(Itinerary itinerary) {
if (onTrack()) {
return itinerary.getFinalArrivalDate();
return itinerary.finalArrivalDate();
} else {
return ETA_UNKOWN;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package org.eclipse.cargotracker.domain.model.cargo;

import static java.util.Collections.*;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.Size;
Expand All @@ -10,52 +8,59 @@
import org.eclipse.cargotracker.domain.model.handling.HandlingEvent;
import org.eclipse.cargotracker.domain.model.location.Location;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

@Embeddable
public record Itinerary(
// TODO [Clean Code] Look into why cascade delete doesn't work.
// Hibernate issue:
// Changes applied according to WildFly/Hibernate requirements.
// The `orphanRemoval = true` option will causes a `all-delete-orphan` exception under
// WildFly/Hibernate.
// (There is a famous lazy initialization exception you could encounter WildFly/Hibernate.
// The `fetch = FetchType.EAGER` fixes the Hibernate lazy initialization exception
// but maybe cause bad performance. A good practice is accessing the one-to-many relations
// in a session/tx boundary)
//
// @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "cargo_id")
// TODO [Clean Code] Index this is in leg_index
// Hibernate issue:
// Hibernate does not persist the order of the list element when saving into db.
// The `OrderColumn` will persist the position of list elements in db.
@OrderColumn(name = "leg_index")
// The `OrderBy` only ensures the order of list elements in memory. Only
// `@OrderBy("loadTime")`
// is added some tests are still failed under WildFly/Hibernate.
// @OrderBy("loadTime")
@Size(min = 1)
@NotEmpty(message = "Legs must not be empty")
List<Leg> legs) {
public class Itinerary implements Serializable {

// Null object pattern.
public static final Itinerary EMPTY_ITINERARY = new Itinerary(emptyList());

public Itinerary {
public static final Itinerary EMPTY_ITINERARY = new Itinerary();
private static final long serialVersionUID = 1L;

// TODO [Clean Code] Look into why cascade delete doesn't work.
// Hibernate issue:
// Changes applied according to WildFly/Hibernate requirements.
// The `orphanRemoval = true` option will causes a `all-delete-orphan` exception under
// WildFly/Hibernate.
// (There is a famous lazy initialization exception you could encounter WildFly/Hibernate.
// The `fetch = FetchType.EAGER` fixes the Hibernate lazy initialization exception
// but maybe cause bad performance. A good practice is accessing the one-to-many relations
// in a session/tx boundary)
//
// @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "cargo_id")
// TODO [Clean Code] Index this is in leg_index
// Hibernate issue:
// Hibernate does not persist the order of the list element when saving into db.
// The `OrderColumn` will persist the position of list elements in db.
@OrderColumn(name = "leg_index")
// The `OrderBy` only ensures the order of list elements in memory. Only `@OrderBy("loadTime")`
// is added some tests are still failed under WildFly/Hibernate.
// @OrderBy("loadTime")
@Size(min = 1)
@NotEmpty(message = "Legs must not be empty")
private List<Leg> legs = Collections.emptyList();

public Itinerary() {
// Nothing to initialize.
}

public Itinerary(List<Leg> legs) {
Validate.notEmpty(legs);
Validate.noNullElements(legs);

this.legs = legs;
}

//
// public List<Leg> legs() {
// // this.legs.sort(Comparator.comparing(Leg::getLoadTime));
// return Collections.unmodifiableList(this.legs);
// }
public List<Leg> legs() {
// this.legs.sort(Comparator.comparing(Leg::getLoadTime));
return Collections.unmodifiableList(this.legs);
}

/** Test if the given handling event is expected when executing this itinerary. */
public boolean isExpected(HandlingEvent event) {
Expand Down Expand Up @@ -86,35 +91,35 @@ public boolean isExpected(HandlingEvent event) {
leg.getUnloadLocation().equals(event.getLocation())
&& leg.getVoyage().equals(event.getVoyage()));
case CLAIM -> {
Leg leg = getLastLeg();
Leg leg = lastLeg();
yield leg.getUnloadLocation().equals(event.getLocation());
}
case CUSTOMS -> true;
default -> throw new RuntimeException("Event case is not handled");
};
}

Location getInitialDepartureLocation() {
Location initialDepartureLocation() {
if (legs.isEmpty()) {
return Location.UNKNOWN;
} else {
return legs.get(0).getLoadLocation();
}
}

Location getFinalArrivalLocation() {
Location finalArrivalLocation() {
if (legs.isEmpty()) {
return Location.UNKNOWN;
} else {
return getLastLeg().getUnloadLocation();
return lastLeg().getUnloadLocation();
}
}

/**
* @return Date when cargo arrives at final destination.
*/
LocalDateTime getFinalArrivalDate() {
Leg lastLeg = getLastLeg();
LocalDateTime finalArrivalDate() {
Leg lastLeg = lastLeg();

if (lastLeg == null) {
return LocalDateTime.MAX;
Expand All @@ -126,7 +131,7 @@ LocalDateTime getFinalArrivalDate() {
/**
* @return The last leg on the itinerary.
*/
Leg getLastLeg() {
Leg lastLeg() {
if (legs.isEmpty()) {
return null;
} else {
Expand All @@ -143,39 +148,38 @@ private boolean sameValueAs(Itinerary other) {
return other != null && Objects.equals(List.copyOf(this.legs), List.copyOf(other.legs));
}

// @Override
// public boolean equals(Object o) {
// if (this == o) {
// return true;
// }
//
// // if (o == null || getClass() != o.getClass()) {
// // return false;
// // }
// //
// //
// https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java
// // Hibernate issue:
// // `getClass() != o.getClass()` will fail if comparing the objects in different
// // transactions/sessions.
// // The generated dynamic proxies are always different classes.
// if (o == null || !(o instanceof Itinerary)) {
// return false;
// }
//
// Itinerary itinerary = (Itinerary) o;
//
// return sameValueAs(itinerary);
// }
//
// @Override
// public int hashCode() {
// // return legs.hashCode();
// return Objects.hashCode(List.copyOf(legs));
// }
//
// @Override
// public String toString() {
// return "Itinerary{" + "legs=" + legs + '}';
// }Itinerary
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

// if (o == null || getClass() != o.getClass()) {
// return false;
// }
//
// https://stackoverflow.com/questions/27581/what-issues-should-be-considered-when-overriding-equals-and-hashcode-in-java
// Hibernate issue:
// `getClass() != o.getClass()` will fail if comparing the objects in different
// transactions/sessions.
// The generated dynamic proxies are always different classes.
if (o == null || !(o instanceof Itinerary)) {
return false;
}

Itinerary itinerary = (Itinerary) o;

return sameValueAs(itinerary);
}

@Override
public int hashCode() {
// return legs.hashCode();
return Objects.hashCode(List.copyOf(legs));
}

@Override
public String toString() {
return "Itinerary{" + "legs=" + legs + '}';
}
}
Loading

0 comments on commit f97e402

Please sign in to comment.