Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -295,33 +295,15 @@ public ChronoUnit getUnit() {
}
}

/**
* Formats the given estimated temporal amount to a string.
* <p>
* Examples:
* </p>
* <ul>
* <li>Duration of 30 seconds: {@code 30s}</li>
* <li>Duration of 25 hours: {@code 1d1h}</li>
* <li>Duration of 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes and 7 seconds: {@code 1y2mo3w4d5h6m7s}</li>
* <li>Duration of 1 hours and 61 minutes: {@code 2h1m}</li>
* <li>Past duration of 1 hours and 61 minutes: {@code -2h1m}</li>
* <li>Period of 1 year, 2 months, 4 days: {@code 1y2mo4d}</li>
* <li>Past period of 1 year, 2 months, 4 days: {@code -1y2mo4d}</li>
* </ul>
*
* @param temporalAmount the temporal amount to format. Must not be null.
* @return the formatted string
*/
public String format(T temporalAmount) {
StringBuilder builder = new StringBuilder();
public TimeResult prepare(T temporalAmount) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The new public method prepare should have its own unit tests. While the existing tests for format will indirectly cover some of prepare's logic, they don't validate the structure of the returned TimeResult object (e.g., the isNegative flag, or the details of each TimePart). Adding dedicated tests for prepare would ensure its contract is met and prevent future regressions.

List<TimePart> parts = new ArrayList<>();
Duration duration = this.toDuration(this.baseForTimeEstimation, temporalAmount);
for (TimeModifier modifier : this.modifiers) {
duration = modifier.modify(duration);
}

boolean isNegative = duration.isNegative();
if (duration.isNegative()) {
builder.append('-');
duration = duration.negated();
}

Expand All @@ -345,18 +327,52 @@ public String format(T temporalAmount) {

BigInteger nanosCountCleared = count.multiply(nanosInOneUnit);

builder.append(count).append(key);
parts.add(new TimePart(count, key, unit));
duration = duration.minusNanos(nanosCountCleared.longValue());
}

String result = builder.toString();
return new TimeResult(parts, isNegative);
}

if (result.isEmpty()) {
/**
* Formats the given estimated temporal amount to a string.
* <p>
* Examples:
* </p>
* <ul>
* <li>Duration of 30 seconds: {@code 30s}</li>
* <li>Duration of 25 hours: {@code 1d1h}</li>
* <li>Duration of 1 year, 2 months, 3 weeks, 4 days, 5 hours, 6 minutes and 7 seconds: {@code 1y2mo3w4d5h6m7s}</li>
* <li>Duration of 1 hours and 61 minutes: {@code 2h1m}</li>
* <li>Past duration of 1 hours and 61 minutes: {@code -2h1m}</li>
* <li>Period of 1 year, 2 months, 4 days: {@code 1y2mo4d}</li>
* <li>Past period of 1 year, 2 months, 4 days: {@code -1y2mo4d}</li>
* </ul>
*
* @param temporalAmount the temporal amount to format. Must not be null.
* @return the formatted string
*/
public String format(T temporalAmount) {
TimeResult result = this.prepare(temporalAmount);
if (result.parts().isEmpty()) {
String defaultSymbol = this.defaultZeroSymbol.get();
return "0" + defaultSymbol;
}

return result;
StringBuilder builder = new StringBuilder();
if (result.isNegative()) {
builder.append('-');
}

for (TimePart part : result.parts()) {
if (part.count().equals(BigInteger.ZERO)) {
continue;
}

builder.append(part.count()).append(part.name());
}
Comment on lines +367 to +373

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This loop contains a redundant check. The prepare() method already filters out time parts with a zero count, so the if (part.count().equals(BigInteger.ZERO)) check is unnecessary. You can simplify the loop.

        for (TimePart part : result.parts()) {
            builder.append(part.count()).append(part.name());
        }


return builder.toString();
}

protected abstract Duration toDuration(LocalDateTimeProvider baseForTimeEstimation, T temporalAmount);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.eternalcode.commons.time;

import java.math.BigInteger;
import java.time.temporal.ChronoUnit;

public record TimePart(
BigInteger count,
String name,
ChronoUnit unit
) {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.eternalcode.commons.time;

import java.util.List;

public record TimeResult(
List<TimePart> parts,
boolean isNegative
) {
}