-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand time API #55
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Rollczi
wants to merge
1
commit into
master
Choose a base branch
from
expand-time-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Expand time API #55
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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) { | ||
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(); | ||
} | ||
|
||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
||
return builder.toString(); | ||
} | ||
|
||
protected abstract Duration toDuration(LocalDateTimeProvider baseForTimeEstimation, T temporalAmount); | ||
|
11 changes: 11 additions & 0 deletions
11
eternalcode-commons-shared/src/main/java/com/eternalcode/commons/time/TimePart.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
9 changes: 9 additions & 0 deletions
9
eternalcode-commons-shared/src/main/java/com/eternalcode/commons/time/TimeResult.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) { | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The new public method
prepare
should have its own unit tests. While the existing tests forformat
will indirectly cover some ofprepare
's logic, they don't validate the structure of the returnedTimeResult
object (e.g., theisNegative
flag, or the details of eachTimePart
). Adding dedicated tests forprepare
would ensure its contract is met and prevent future regressions.