From 159ce68decbb7237ab2ba33b8458f9ff4d09867d Mon Sep 17 00:00:00 2001 From: Piotr Dec Date: Sat, 16 Mar 2024 12:51:44 +0100 Subject: [PATCH] Initial commit --- .gitignore | 3 + hazelcast/pom.xml | 28 ++++ hazelcast/readme.md | 13 ++ .../time/hazelcast/TimeRangeSerializer.java | 33 +++++ .../hazelcast/TimeRangeSerializerTest.java | 35 +++++ pom.xml | 65 +++++++++ readme.md | 8 ++ time/pom.xml | 22 ++++ .../java/eu/ztsh/lib/time/Calculation.java | 22 ++++ time/src/main/java/eu/ztsh/lib/time/Diff.java | 49 +++++++ .../main/java/eu/ztsh/lib/time/Duration.java | 87 +++++++++++++ .../java/eu/ztsh/lib/time/Formatters.java | 16 +++ .../src/main/java/eu/ztsh/lib/time/State.java | 12 ++ .../main/java/eu/ztsh/lib/time/TimeRange.java | 98 ++++++++++++++ .../src/main/java/eu/ztsh/lib/time/Timer.java | 26 ++++ .../eu/ztsh/lib/time/util/TimeRangeUtil.java | 67 ++++++++++ .../java/eu/ztsh/lib/time/util/TimeUtil.java | 89 +++++++++++++ .../test/java/eu/ztsh/lib/time/DiffTest.java | 35 +++++ .../java/eu/ztsh/lib/time/DurationTest.java | 43 ++++++ .../java/eu/ztsh/lib/time/TimeRangeTest.java | 123 ++++++++++++++++++ .../eu/ztsh/lib/time/util/TimeUtilTest.java | 50 +++++++ 21 files changed, 924 insertions(+) create mode 100644 .gitignore create mode 100644 hazelcast/pom.xml create mode 100644 hazelcast/readme.md create mode 100644 hazelcast/src/main/java/eu/ztsh/lib/time/hazelcast/TimeRangeSerializer.java create mode 100644 hazelcast/src/test/java/eu/ztsh/lib/time/hazelcast/TimeRangeSerializerTest.java create mode 100644 pom.xml create mode 100644 readme.md create mode 100644 time/pom.xml create mode 100644 time/src/main/java/eu/ztsh/lib/time/Calculation.java create mode 100644 time/src/main/java/eu/ztsh/lib/time/Diff.java create mode 100644 time/src/main/java/eu/ztsh/lib/time/Duration.java create mode 100644 time/src/main/java/eu/ztsh/lib/time/Formatters.java create mode 100644 time/src/main/java/eu/ztsh/lib/time/State.java create mode 100644 time/src/main/java/eu/ztsh/lib/time/TimeRange.java create mode 100644 time/src/main/java/eu/ztsh/lib/time/Timer.java create mode 100644 time/src/main/java/eu/ztsh/lib/time/util/TimeRangeUtil.java create mode 100644 time/src/main/java/eu/ztsh/lib/time/util/TimeUtil.java create mode 100644 time/src/test/java/eu/ztsh/lib/time/DiffTest.java create mode 100644 time/src/test/java/eu/ztsh/lib/time/DurationTest.java create mode 100644 time/src/test/java/eu/ztsh/lib/time/TimeRangeTest.java create mode 100644 time/src/test/java/eu/ztsh/lib/time/util/TimeUtilTest.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e02456b --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +target/ +.idea +*.iml diff --git a/hazelcast/pom.xml b/hazelcast/pom.xml new file mode 100644 index 0000000..6373dd8 --- /dev/null +++ b/hazelcast/pom.xml @@ -0,0 +1,28 @@ + + + 4.0.0 + + eu.ztsh.lib + time-parent + 1.0-SNAPSHOT + + + eu.ztsh.lib.time + hazelcast + + + + eu.ztsh.lib.time + time + provided + + + com.hazelcast + hazelcast-all + provided + + + + \ No newline at end of file diff --git a/hazelcast/readme.md b/hazelcast/readme.md new file mode 100644 index 0000000..60d12c8 --- /dev/null +++ b/hazelcast/readme.md @@ -0,0 +1,13 @@ +# Hazelcast serializer + +Provides custom serializer for `TimeRange`. + +## Usage +In `hazelcast.yaml` just add +```yaml +hazelcast: + serialization: + serializers: + - type-class: eu.ztsh.lib.time.TimeRange + class-name: eu.ztsh.lib.time.hazelcast.TimeRangeSerializer +``` diff --git a/hazelcast/src/main/java/eu/ztsh/lib/time/hazelcast/TimeRangeSerializer.java b/hazelcast/src/main/java/eu/ztsh/lib/time/hazelcast/TimeRangeSerializer.java new file mode 100644 index 0000000..bd10408 --- /dev/null +++ b/hazelcast/src/main/java/eu/ztsh/lib/time/hazelcast/TimeRangeSerializer.java @@ -0,0 +1,33 @@ +package eu.ztsh.lib.time.hazelcast; + +import com.hazelcast.nio.serialization.ByteArraySerializer; +import eu.ztsh.lib.time.TimeRange; + +import java.io.IOException; +import java.time.LocalDate; + +import static eu.ztsh.lib.time.Formatters.yearMonthDay; + +public class TimeRangeSerializer implements ByteArraySerializer { + + @Override + public byte[] write(TimeRange timeRange) throws IOException { + return (yearMonthDay.format(timeRange.from()) + ":" + yearMonthDay.format(timeRange.to())).getBytes(); + } + + @Override + public TimeRange read(byte[] bytes) throws IOException { + var stringData = new String(bytes).split(":"); + return new TimeRange(parse(stringData[0]), parse(stringData[1])); + } + + private static LocalDate parse(String input) { + return LocalDate.parse(input, yearMonthDay); + } + + @Override + public int getTypeId() { + return 150; + } + +} diff --git a/hazelcast/src/test/java/eu/ztsh/lib/time/hazelcast/TimeRangeSerializerTest.java b/hazelcast/src/test/java/eu/ztsh/lib/time/hazelcast/TimeRangeSerializerTest.java new file mode 100644 index 0000000..b0b95bf --- /dev/null +++ b/hazelcast/src/test/java/eu/ztsh/lib/time/hazelcast/TimeRangeSerializerTest.java @@ -0,0 +1,35 @@ +package eu.ztsh.lib.time.hazelcast; + +import eu.ztsh.lib.time.TimeRange; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.time.LocalDate; + +import static org.assertj.core.api.Assertions.assertThat; + +class TimeRangeSerializerTest { + + private final LocalDate date1 = LocalDate.of(2023, 10, 10); + private final LocalDate date2 = LocalDate.of(2023, 10, 11); + private final LocalDate date3 = LocalDate.of(2023, 10, 12); + + private final TimeRangeSerializer serializer = new TimeRangeSerializer(); + + @Test + public void identityTest() throws IOException { + var range1 = new TimeRange(date1, date2); + var range2 = serializer.read(serializer.write(range1)); + assertThat(range2).isEqualTo(range1); + } + + @Test + public void nonIdentityTest() throws IOException { + var range1 = new TimeRange(date1, date2); + var range2 = new TimeRange(date1, date3); + var range3 = serializer.read(serializer.write(range1)); + assertThat(range1).isNotEqualTo(range2); + assertThat(range3).isNotEqualTo(range2); + } + +} diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..47a3bdf --- /dev/null +++ b/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + eu.ztsh.lib + time-parent + 1.0-SNAPSHOT + pom + + + time + hazelcast + + + + UTF-8 + 17 + ${java.version} + ${java.version} + 3.0.2 + 4.2.8 + + 3.24.2 + 5.9.3 + + + + + + eu.ztsh.lib.time + time + ${project.version} + + + + jakarta.validation + jakarta.validation-api + ${jakarta-validation.version} + + + com.hazelcast + hazelcast-all + ${hazelcast.version} + + + + + + + org.assertj + assertj-core + ${assertj.version} + test + + + org.junit.jupiter + junit-jupiter + ${junit-jupiter.version} + test + + + + \ No newline at end of file diff --git a/readme.md b/readme.md new file mode 100644 index 0000000..2a489d2 --- /dev/null +++ b/readme.md @@ -0,0 +1,8 @@ +# Time range library + +This library was created as an effect of creating multiple time management applications. + +Main concepts: +- TimeRange: a both end closed range of LocalDates with iterator +- Diff: representation of difference between expected and actual time duration +- Duration: seconds between `LocalDateTime`s or other `Duration`s diff --git a/time/pom.xml b/time/pom.xml new file mode 100644 index 0000000..e969068 --- /dev/null +++ b/time/pom.xml @@ -0,0 +1,22 @@ + + + 4.0.0 + + eu.ztsh.lib + time-parent + 1.0-SNAPSHOT + + + eu.ztsh.lib.time + time + + + + jakarta.validation + jakarta.validation-api + + + + \ No newline at end of file diff --git a/time/src/main/java/eu/ztsh/lib/time/Calculation.java b/time/src/main/java/eu/ztsh/lib/time/Calculation.java new file mode 100644 index 0000000..8c73e45 --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/Calculation.java @@ -0,0 +1,22 @@ +package eu.ztsh.lib.time; + +/** + * Structure for summarized work time. + * + * @param summary Short description + * @param time String representation of spent hours + * @param diff Difference between expected and real work time as {@link Diff} object + */ +public record Calculation(String summary, String time, Diff diff) { + + public static Calculation calculate(String summary, long daysTillToday, long secondsSpent) { + return new Calculation(summary, + String.valueOf(((double) secondsSpent) / (60 * 60)), + Diff.calculate(daysTillToday, secondsSpent)); + } + + public static Calculation off(String summary) { + return new Calculation(summary, "0.0", Diff.OFF); + } + +} diff --git a/time/src/main/java/eu/ztsh/lib/time/Diff.java b/time/src/main/java/eu/ztsh/lib/time/Diff.java new file mode 100644 index 0000000..9135daa --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/Diff.java @@ -0,0 +1,49 @@ +package eu.ztsh.lib.time; + +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.util.concurrent.TimeUnit; + +import static eu.ztsh.lib.time.Formatters.hourMinutesShort; + +/** + * Work time diff counter. + * + *

Can be used for: + *

+ * + * @param value String representation of difference between expected and real time + * @param state Diff placement on timeline + */ +public record Diff(String value, State state) { + + public static final Diff OFF = new Diff("0:00", State.OFF); + + public static Diff calculate(long daysTillToday, long secondsSpent) { + var totalSeconds = TimeUnit.HOURS.toSeconds(daysTillToday * 8); + var diff = secondsSpent - totalSeconds; + if (diff == 0) { + return new Diff("0:00", State.EQUAL); + } + return diff > 0 + ? new Diff("+" + todayWithSeconds(diff).format(hourMinutesShort), State.ADVANCED) + : new Diff("-" + todayWithSeconds(-diff).format(hourMinutesShort), State.OVERDUE); + } + + private static LocalDateTime todayWithSeconds(long seconds) { + return LocalDate.now().atTime(0, 0).plusSeconds(seconds); + } + +} diff --git a/time/src/main/java/eu/ztsh/lib/time/Duration.java b/time/src/main/java/eu/ztsh/lib/time/Duration.java new file mode 100644 index 0000000..0c9a76a --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/Duration.java @@ -0,0 +1,87 @@ +package eu.ztsh.lib.time; + +import jakarta.validation.constraints.NotNull; + +import java.time.LocalDateTime; +import java.time.temporal.ChronoUnit; +import java.util.Objects; + +import static eu.ztsh.lib.time.Formatters.hourMinutes; + +/** + * Object representation od elapsed time + */ +public class Duration implements Comparable { + + private final LocalDateTime started; + private final int timeSpentSeconds; + private final LocalDateTime finished; + + public static Duration between(Duration previous, Duration next) { + if (previous.started.equals(next.started)) { + return new Duration(previous.started, 0, next.started); + } + var seconds = secondsBetween(previous.finished, next.started); + return seconds < 0 + ? new Duration(next.finished, seconds, previous.started) + : new Duration(previous.finished, seconds, next.started); + } + + Duration(LocalDateTime started, LocalDateTime finished) { + this.started = started; + this.finished = finished; + this.timeSpentSeconds = secondsBetween(started, finished); + } + + Duration(LocalDateTime started, int timeSpentSeconds, LocalDateTime finished) { + this.started = started; + this.timeSpentSeconds = timeSpentSeconds; + this.finished = finished; + } + + public LocalDateTime getStarted() { + return started; + } + + public int getTimeSpentSeconds() { + return timeSpentSeconds; + } + + public LocalDateTime getFinished() { + return finished; + } + + /** + * long -> gap -> short + * + * @param duration the object to be compared. + * @return comparison result + */ + @Override + public int compareTo(@NotNull Duration duration) { + var dates = started.compareTo(duration.started); + return dates != 0 ? dates : duration.timeSpentSeconds - timeSpentSeconds; + } + + @Override + public String toString() { + return "Duration(" + hourMinutes.format(started) + "-" + hourMinutes.format(finished) + ")"; + } + + @Override + public int hashCode() { + return Objects.hash(started, finished); + } + + @Override + public boolean equals(Object obj) { + return (obj instanceof Duration duration) + && duration.started.equals(this.started) + && duration.finished.equals(this.finished); + } + + private static int secondsBetween(LocalDateTime from, LocalDateTime to) { + return Math.toIntExact(from.until(to, ChronoUnit.SECONDS)); + } + +} diff --git a/time/src/main/java/eu/ztsh/lib/time/Formatters.java b/time/src/main/java/eu/ztsh/lib/time/Formatters.java new file mode 100644 index 0000000..09b1b19 --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/Formatters.java @@ -0,0 +1,16 @@ +package eu.ztsh.lib.time; + +import java.time.format.DateTimeFormatter; + +/** + * Collection of most commonly used DateTime formatters + */ +public class Formatters { + + public final static DateTimeFormatter hourMinutes = DateTimeFormatter.ofPattern("HH:mm"); + public final static DateTimeFormatter hourMinutesShort = DateTimeFormatter.ofPattern("H:mm"); + public final static DateTimeFormatter dayMonth = DateTimeFormatter.ofPattern("dd.MM"); + public final static DateTimeFormatter yearMonthDay = DateTimeFormatter.ofPattern("yyyy-MM-dd"); + public final static DateTimeFormatter monthYear = DateTimeFormatter.ofPattern("MM.yyyy"); + +} diff --git a/time/src/main/java/eu/ztsh/lib/time/State.java b/time/src/main/java/eu/ztsh/lib/time/State.java new file mode 100644 index 0000000..e7c7b68 --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/State.java @@ -0,0 +1,12 @@ +package eu.ztsh.lib.time; + +/** + * {@link Diff} state with graphical representation + */ +public enum State { + OFF, + OVERDUE, + EQUAL, + ADVANCED + +} diff --git a/time/src/main/java/eu/ztsh/lib/time/TimeRange.java b/time/src/main/java/eu/ztsh/lib/time/TimeRange.java new file mode 100644 index 0000000..55e669e --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/TimeRange.java @@ -0,0 +1,98 @@ +package eu.ztsh.lib.time; + +import java.time.LocalDate; +import java.util.Iterator; +import java.util.List; +import java.util.Objects; +import java.util.stream.Stream; + +import static eu.ztsh.lib.time.Formatters.dayMonth; + +/** + * A range of dates. + * + * @param from Start date (inclusive) + * @param to End date (inclusive) + */ +public record TimeRange(LocalDate from, LocalDate to) { + + public TimeRange { + to = Objects.requireNonNullElse(to, LocalDate.now()); + } + + public TimeRange(LocalDate from) { + this(from, null); + } + + public Iterator iterator() { + return new TimeRangeIterator(from, to); + } + + public List getDates() { + return from.datesUntil(to.plusDays(1)).toList(); + } + + public long getLength() { + return getDates().size(); + } + + public TimeRange expand(LocalDate target) { + if (target.isBefore(from)) { + return new TimeRange(target, to); + } else if (target.isAfter(to)) { + return new TimeRange(from, target); + } + return this; + } + + public TimeRange merge(TimeRange other) { + var merged = Stream.concat(getDates().stream(), other.getDates().stream()).sorted().distinct().toList(); + var first = merged.get(0); + var last = merged.get(merged.size() - 1); + if (first.until(last).getDays() > merged.size()) { + throw new IllegalArgumentException("Adjacent ranges"); + } + return new TimeRange(first, last); + } + + @Override + public boolean equals(Object o) { + return this == o || (o instanceof TimeRange that) + && this.from.equals(that.from) + && this.to.equals(that.to); + } + + @Override + public int hashCode() { + return Objects.hash(from, to); + } + + @Override + public String toString() { + return dayMonth.format(from) + "-" + dayMonth.format(to); + } + + private static class TimeRangeIterator implements Iterator { + + private final LocalDate to; + private LocalDate now; + + private TimeRangeIterator(LocalDate from, LocalDate to) { + this.to = to; + now = from; + } + + @Override + public boolean hasNext() { + return !now.isAfter(to); + } + + @Override + public LocalDate next() { + now = now.plusDays(1); + return now.minusDays(1); + } + + } + +} diff --git a/time/src/main/java/eu/ztsh/lib/time/Timer.java b/time/src/main/java/eu/ztsh/lib/time/Timer.java new file mode 100644 index 0000000..ffaba8f --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/Timer.java @@ -0,0 +1,26 @@ +package eu.ztsh.lib.time; + +import java.time.LocalDateTime; +import java.util.ArrayList; +import java.util.List; + +public class Timer { + + private LocalDateTime currentSession; + private final List sessions = new ArrayList<>(); + + public void start() { + currentSession = LocalDateTime.now(); + } + + public int stop() { + var elapsed = new Duration(currentSession, LocalDateTime.now()); + sessions.add(elapsed); + return elapsed.getTimeSpentSeconds(); + } + + public int getElapsed() { + return sessions.stream().mapToInt(Duration::getTimeSpentSeconds).sum(); + } + +} diff --git a/time/src/main/java/eu/ztsh/lib/time/util/TimeRangeUtil.java b/time/src/main/java/eu/ztsh/lib/time/util/TimeRangeUtil.java new file mode 100644 index 0000000..3d99a0b --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/util/TimeRangeUtil.java @@ -0,0 +1,67 @@ +package eu.ztsh.lib.time.util; + +import eu.ztsh.lib.time.TimeRange; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.List; + +import static eu.ztsh.lib.time.util.TimeUtil.*; + +public class TimeRangeUtil { + + public static List createRanges(Date today) { + return createRanges(convert(today)); + } + + public static List createRanges() { + return createRanges(LocalDate.now()); + } + + public static List createRanges(LocalDate today) { + List ranges = new ArrayList<>(); + FridaySupplier fridaySupplier = isWeekend(today) ? TimeUtil::getLastFriday : TimeUtil::getNextFriday; + // current range + ranges.add(new TimeRange(getLastMonday(today), fridaySupplier.supply(today))); + var limitMonth = today.getMonth().minus(isMonthBeginning(today) ? 2 : 1); + while (true) { + today = today.minusWeeks(1); + var monday = getLastMonday(today); + var sunday = getNextSunday(today); + // check if whole week is out of bounds + if (monday.getMonth().equals(limitMonth) && sunday.getMonth().equals(limitMonth)) { + break; + } + ranges.add(new TimeRange(monday, fridaySupplier.supply(today))); + } + Collections.reverse(ranges); + return ranges; + } + + public static TimeRange getCurrentWeek() { + return getWeekFor(LocalDate.now()); + } + + public static TimeRange getLastWeek() { + return getWeekFor(LocalDate.now().minusWeeks(1)); + } + + private static TimeRange getWeekFor(LocalDate today) { + FridaySupplier fridaySupplier = isWeekend(today) ? TimeUtil::getLastFriday : TimeUtil::getNextFriday; + // current range + return new TimeRange(getLastMonday(today), fridaySupplier.supply(today)); + } + + private interface FridaySupplier { + + LocalDate supply(LocalDate input); + + } + + private TimeRangeUtil() { + throw new IllegalStateException("Utility class"); + } + +} diff --git a/time/src/main/java/eu/ztsh/lib/time/util/TimeUtil.java b/time/src/main/java/eu/ztsh/lib/time/util/TimeUtil.java new file mode 100644 index 0000000..3ef7404 --- /dev/null +++ b/time/src/main/java/eu/ztsh/lib/time/util/TimeUtil.java @@ -0,0 +1,89 @@ +package eu.ztsh.lib.time.util; + +import eu.ztsh.lib.time.TimeRange; + +import java.time.DayOfWeek; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.ZoneId; +import java.time.temporal.TemporalAdjusters; +import java.util.Date; +import java.util.List; + +/** + * Collection of common DateTime oriented functions + */ +public class TimeUtil { + + public static LocalDate today() { + return LocalDate.now(); + } + + public static LocalDate convert(Date dateToConvert) { + return dateToConvert.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDate(); + } + + public static Date convert(LocalDate dateToConvert) { + return Date.from(dateToConvert.atStartOfDay() + .atZone(ZoneId.systemDefault()) + .toInstant()); + } + + public static LocalDateTime convertFull(Date dateToConvert) { + return dateToConvert.toInstant() + .atZone(ZoneId.systemDefault()) + .toLocalDateTime(); + } + + public static boolean isMonday(LocalDate today) { + return today.getDayOfWeek() == DayOfWeek.MONDAY; + } + + public static boolean isWeekend(LocalDate today) { + return today.getDayOfWeek() == DayOfWeek.SATURDAY + || today.getDayOfWeek() == DayOfWeek.SUNDAY; + } + + public static boolean isMonthBeginning(LocalDate today) { + return !today.minusWeeks(1).getMonth().equals(today.getMonth()); + } + + public static LocalDate getLastMonday(LocalDate today) { + return today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY)); + } + + public static LocalDate getNextSunday(LocalDate today) { + return today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY)); + } + + public static LocalDate getNextFriday(LocalDate today) { + return today.with(TemporalAdjusters.nextOrSame(DayOfWeek.FRIDAY)); + } + + public static LocalDate getLastFriday(LocalDate today) { + return today.with(TemporalAdjusters.previous(DayOfWeek.FRIDAY)); + } + + public static LocalDate getFirstDayOfMonth(LocalDate today) { + return today.with(TemporalAdjusters.firstDayOfMonth()); + } + + public static LocalDate getLastDayOfMonth(LocalDate today) { + return today.with(TemporalAdjusters.lastDayOfMonth()); + } + + public static long countUntil(List ranges, LocalDate today) { + return ranges.stream() + .map(TimeRange::getDates) + .flatMap(List::stream) + .filter(date -> date.getMonth().equals(today.getMonth()) && (date.isBefore(today) || date.equals(today))) + .count(); + } + + private TimeUtil() { + throw new IllegalStateException("Utility class"); + } + +} diff --git a/time/src/test/java/eu/ztsh/lib/time/DiffTest.java b/time/src/test/java/eu/ztsh/lib/time/DiffTest.java new file mode 100644 index 0000000..f03ba7f --- /dev/null +++ b/time/src/test/java/eu/ztsh/lib/time/DiffTest.java @@ -0,0 +1,35 @@ +package eu.ztsh.lib.time; + +import org.junit.jupiter.api.Test; + +import java.util.concurrent.TimeUnit; + +import static org.assertj.core.api.Assertions.assertThat; + +class DiffTest { + + private final static long days = 2; + private final static long secondsSpent = TimeUnit.HOURS.toSeconds(days * 8); + + @Test + void equalCountTest() { + commonTest(days, "0:00", State.EQUAL); + } + + @Test + void overdueCountTest() { + commonTest(days + 1, "-8:00", State.OVERDUE); + } + + @Test + void advancedCountTest() { + commonTest(days - 1, "+8:00", State.ADVANCED); + } + + private static void commonTest(long days, String expectedValue, State expectedState) { + var diff = Diff.calculate(days, secondsSpent); + assertThat(diff.value()).isEqualTo(expectedValue); + assertThat(diff.state()).isEqualTo(expectedState); + } + +} diff --git a/time/src/test/java/eu/ztsh/lib/time/DurationTest.java b/time/src/test/java/eu/ztsh/lib/time/DurationTest.java new file mode 100644 index 0000000..10e8090 --- /dev/null +++ b/time/src/test/java/eu/ztsh/lib/time/DurationTest.java @@ -0,0 +1,43 @@ +package eu.ztsh.lib.time; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDateTime; +import java.time.Month; +import java.util.TreeSet; + +import static org.assertj.core.api.Assertions.assertThat; + +class DurationTest { + + private final LocalDateTime base = LocalDateTime.of(2023, Month.JULY, 3, 8, 45); + private final Duration nullDuration = new Duration(base, 0, base); + private final Duration longDuration = new Duration(base, 3 * 60 * 60, base.plusHours(3)); + private final Duration shortDuration = new Duration(base, 30 * 60, base.plusMinutes(30)); + + @Test + public void betweenSimpleComputeTest() { + var next = base.plusSeconds(100); + + var previousDuration = new Duration(base, 0, base); + var nextDuration = new Duration(next, 0, next); + + assertThat(Duration.between(previousDuration, nextDuration).getTimeSpentSeconds()).isEqualTo(100); + } + + @Test + public void betweenInboundComputeTest() { + assertThat(Duration.between(longDuration, shortDuration)).isEqualByComparingTo(nullDuration); + assertThat(Duration.between(shortDuration, longDuration)).isEqualByComparingTo(nullDuration); + } + + @Test + public void sortingTest() { + var set = new TreeSet(); + set.add(nullDuration); + set.add(shortDuration); + set.add(longDuration); + assertThat(set).containsExactly(longDuration, shortDuration, nullDuration); + } + +} diff --git a/time/src/test/java/eu/ztsh/lib/time/TimeRangeTest.java b/time/src/test/java/eu/ztsh/lib/time/TimeRangeTest.java new file mode 100644 index 0000000..79a7773 --- /dev/null +++ b/time/src/test/java/eu/ztsh/lib/time/TimeRangeTest.java @@ -0,0 +1,123 @@ +package eu.ztsh.lib.time; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class TimeRangeTest { + + private final LocalDate date1 = LocalDate.of(2023, 10, 10); + private final LocalDate date2 = LocalDate.of(2023, 10, 12); + private final LocalDate date3 = LocalDate.of(2023, 10, 13); + private final LocalDate date4 = LocalDate.of(2023, 10, 15); + + @Test + public void lengthTest() { + var range = new TimeRange(date1, date2); + assertThat(range.getDates()).hasSize(3); + assertThat(range.getLength()).isEqualTo(3); + } + + @Test + public void equalityTest() { + var range1 = new TimeRange(date1, date2); + var range2 = new TimeRange(date1, date2); + assertThat(range1).isEqualTo(range2); + } + + @Test + public void nonEqualityTest() { + var range1 = new TimeRange(date1, date2); + var range2 = new TimeRange(date1, date3); + assertThat(range1).isNotEqualTo(range2); + } + + @Test + public void expandAfterTest() { + var test = new TimeRange(date1, date2); + var expected = new TimeRange(date1, date3); + var result = test.expand(date3); + assertThat(result).isEqualTo(expected); + } + + @Test + public void expandBeforeTest() { + var test = new TimeRange(date2, date3); + var expected = new TimeRange(date1, date3); + var result = test.expand(date1); + assertThat(result).isEqualTo(expected); + } + + @Test + public void expandInternalTest() { + var test = new TimeRange(date1, date3); + var expected = new TimeRange(date1, date3); + var result = test.expand(date2); + assertThat(result).isEqualTo(expected); + } + + @Test + public void mergeAfterTest() { + var test1 = new TimeRange(date1, date2); + var test2 = new TimeRange(date3, date4); + var expected = new TimeRange(date1, date4); + var result = test1.merge(test2); + assertThat(result).isEqualTo(expected); + } + + @Test + public void mergeBeforeTest() { + var test1 = new TimeRange(date1, date2); + var test2 = new TimeRange(date3, date4); + var expected = new TimeRange(date1, date4); + var result = test2.merge(test1); + assertThat(result).isEqualTo(expected); + } + + @Test + public void mergeIntersectingBeforeTest() { + var test1 = new TimeRange(date1, date3); + var test2 = new TimeRange(date2, date4); + var expected = new TimeRange(date1, date4); + var result = test1.merge(test2); + assertThat(result).isEqualTo(expected); + } + + @Test + public void mergeIntersectionAfterTest() { + var test1 = new TimeRange(date1, date3); + var test2 = new TimeRange(date2, date4); + var expected = new TimeRange(date1, date4); + var result = test2.merge(test1); + assertThat(result).isEqualTo(expected); + } + + @Test + public void mergeMinorContainingTest() { + var test1 = new TimeRange(date1, date4); + var test2 = new TimeRange(date2, date3); + var expected = new TimeRange(date1, date4); + var result = test1.merge(test2); + assertThat(result).isEqualTo(expected); + } + + @Test + public void mergeMajorContainingTest() { + var test1 = new TimeRange(date1, date4); + var test2 = new TimeRange(date2, date3); + var expected = new TimeRange(date1, date4); + var result = test2.merge(test1); + assertThat(result).isEqualTo(expected); + } + + @Test + public void mergeAdjacentTest() { + var test1 = new TimeRange(date1, date2); + var test2 = new TimeRange(date4, date4); + assertThatThrownBy(() -> test1.merge(test2)).isInstanceOf(IllegalArgumentException.class); + } + +} \ No newline at end of file diff --git a/time/src/test/java/eu/ztsh/lib/time/util/TimeUtilTest.java b/time/src/test/java/eu/ztsh/lib/time/util/TimeUtilTest.java new file mode 100644 index 0000000..d2079be --- /dev/null +++ b/time/src/test/java/eu/ztsh/lib/time/util/TimeUtilTest.java @@ -0,0 +1,50 @@ +package eu.ztsh.lib.time.util; + +import org.junit.jupiter.api.Test; + +import java.time.LocalDate; + +import static eu.ztsh.lib.time.util.TimeUtil.countUntil; +import static org.assertj.core.api.Assertions.assertThat; + +class TimeUtilTest { + + @Test + public void secondMondayTest() { + assertThat(TimeUtil.isMonthBeginning(LocalDate.of(2023, 5, 8))).isFalse(); + } + + @Test + public void firstSundayTest() { + assertThat(TimeUtil.isMonthBeginning(LocalDate.of(2023, 5, 7))).isTrue(); + } + + @Test + public void firstTuesdayTest() { + assertThat(TimeUtil.isMonthBeginning(LocalDate.of(2023, 5, 2))).isTrue(); + } + + @Test + public void firstMondayTest() { + assertThat(TimeUtil.isMonthBeginning(LocalDate.of(2022, 11, 7))).isTrue(); + } + + @Test + public void countTest() { + var today = LocalDate.of(2023, 7, 23); // Sunday + var last = TimeUtil.getLastDayOfMonth(today); + var ranges = TimeRangeUtil.createRanges(last); + assertThat(ranges).hasSize(6); + assertThat(countUntil(ranges, today)).isEqualTo(15); + assertThat(countUntil(ranges, today.plusDays(1))) // Monday + .isEqualTo(16); + } + + @Test + public void mondayCalculationTest() { + var monday = LocalDate.of(2023, 5, 1); + assertThat(TimeUtil.getLastMonday(monday)).isEqualTo(monday); + assertThat(TimeUtil.getLastMonday(monday.plusDays(2))).isEqualTo(monday); + } + +}