Skip to content

Commit

Permalink
#370 - Interval can now be used as embeddable without explicit proper…
Browse files Browse the repository at this point in the history
…ty annotation.

Interval is now annotated with @embeddable, so that client code doesn't have to explicitly declare a property of that type with @embeddable itself.
  • Loading branch information
vrckr authored and odrotbohm committed Feb 17, 2023
1 parent 4a027ae commit dd0375d
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
32 changes: 17 additions & 15 deletions src/main/java/org/salespointframework/time/Interval.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.salespointframework.time;

import jakarta.persistence.Embeddable;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import lombok.NonNull;
Expand All @@ -33,7 +34,9 @@
*
* @author Oliver Drotbohm
* @author Martin Morgenstern
* @author Rebecca Uecker
*/
@Embeddable
@Value
@NoArgsConstructor(force = true, access = AccessLevel.PRIVATE)
public final class Interval {
Expand All @@ -50,7 +53,7 @@ public final class Interval {

/**
* Creates a new {@link Interval} between the given start and end.
*
*
* @param start must not be {@literal null}.
* @param end must not be {@literal null}.
*/
Expand All @@ -66,7 +69,7 @@ private Interval(LocalDateTime start, LocalDateTime end) {

/**
* Starts building a new {@link Interval} with the given start time.
*
*
* @param start must not be {@literal null}.
* @return will never be {@literal null}.
*/
Expand All @@ -76,17 +79,17 @@ public static IntervalBuilder from(LocalDateTime start) {

/**
* Returns the duration of the interval, with the end excluded.
*
*
* @return will never be {@literal null}.
*/
public Duration getDuration() {
return Duration.between(start, end);
}

/**
* Returns whether the given {@link LocalDateTime} is contained in the current {@link Interval}.
* The comparison includes start and end, i.e., the method treats this interval as closed.
*
* Returns whether the given {@link LocalDateTime} is contained in the current {@link Interval}. The comparison
* includes start and end, i.e., the method treats this interval as closed.
*
* @param reference must not be {@literal null}.
* @return
*/
Expand All @@ -101,9 +104,9 @@ public boolean contains(LocalDateTime reference) {
}

/**
* Returns whether the current {@link Interval} overlaps with the given one. The
* comparison excludes start and end, i.e., the method treats both intervals as open.
*
* Returns whether the current {@link Interval} overlaps with the given one. The comparison excludes start and end,
* i.e., the method treats both intervals as open.
*
* @param reference must not be {@literal null}.
* @return
*/
Expand All @@ -118,16 +121,15 @@ public boolean overlaps(Interval reference) {
}

/**
* Returns the {@link Duration} represented by the given {@link Interval}, with the
* end excluded.
*
* Returns the {@link Duration} represented by the given {@link Interval}, with the end excluded.
*
* @return
*/
public Duration toDuration() {
return Duration.between(start, end);
}

/*
/*
* (non-Javadoc)
* @see java.lang.Object#toString()
*/
Expand All @@ -143,7 +145,7 @@ public static class IntervalBuilder {

/**
* Creates an {@link Interval} from the current start time until the given end time.
*
*
* @param end must not be {@literal null} and after the current start time or equal to it.
* @return will never be {@literal null}.
*/
Expand All @@ -153,7 +155,7 @@ public Interval to(LocalDateTime end) {

/**
* Creates a new {@link Interval} from the current start time adding the given {@link TemporalAmount} to it.
*
*
* @param amount must not be {@literal null}.
* @return will never be {@literal null}.
* @see Duration
Expand Down
3 changes: 3 additions & 0 deletions src/test/java/example/DummyEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package example;

import org.salespointframework.time.Interval;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.Id;
Expand All @@ -26,4 +28,5 @@
class DummyEntity {

@Id @GeneratedValue Long id;
Interval interval;
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

import static org.assertj.core.api.Assertions.*;

import jakarta.persistence.Embedded;
import jakarta.persistence.Entity;
import jakarta.persistence.EntityManager;
import jakarta.persistence.GeneratedValue;
Expand All @@ -37,6 +36,7 @@
* Integration tests for {@link Interval}
*
* @author Oliver Gierke
* @author Rebecca Uecker
*/
@Transactional
@ApplicationModuleTest
Expand Down Expand Up @@ -69,6 +69,6 @@ void intervalCanBePersistedAsEmbeddable() {
static class SomeEntity {

@Id @GeneratedValue Long id;
@Embedded Interval interval;
Interval interval;
}
}

0 comments on commit dd0375d

Please sign in to comment.