question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Rename dropwizard Duration

See original GitHub issue

It would be nice if the DropWizard Duration class had a different name like ConfigurationDuration or YamlReadableDuration or something like that. I find that a lot of the APIs I use need a regular java.time.Duration and I ended up having to fully qualify one or the other. Most of us only use the DropWizard Duration as a simpler string to write in a config file.

Another solution would be to add a method toJavaDuration() so I can just take one of those and turn it into the java.time.Duration I need.

Minor for sure, just thought I’d document it while I was running into it.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
sleberknightcommented, Aug 10, 2022

Ok, sounds good. Just so that I’m clear on this, do you only need the one PR for 2.1.x branch, and then you’ll just cherry-pick it to the other branches?

1reaction
sleberknightcommented, Aug 10, 2022

We’ve actually had a very small utility for years to do this exact thing, i.e. convert from io.dropwizard.util.Duration to a java.time.Duration. In Dropwizard’s Duration, with Java 9 and later it can be as simple as:

// JDK 9+
public java.time.Duration toJavaDuration() {
    return java.time.Duration.of(count, unit.toChronoUnit());
}

But since Dropwizard 2.1.x is still using Java 8, the toChronoUnit() method doesn’t exist in TimeUnit. It was added in JDK 9.

So, in the 2.1.x branch one solution would be to copy the implementation of JDK 9’s TimeUnit#toChronoUnit to the Duration class as a private static method and use it like this:

// JDK 8
public java.time.Duration toJavaDuration() {
    return java.time.Duration.of(count, timeUnitToChronoUnit(unit));
}

// In JDK 9, TimeUnit contains the toChronoUnit method, but not in 8.
// This copies JDK 9's TimeUnit#toChronoUnit, and can be replaced once
// the baseline is higher than Java 8.
private static ChronoUnit timeUnitToChronoUnit(TimeUnit unit) {
    switch (unit) {
        case NANOSECONDS: return ChronoUnit.NANOS;
        case MICROSECONDS: return ChronoUnit.MICROS;
        case MILLISECONDS: return ChronoUnit.MILLIS;
        case SECONDS: return ChronoUnit.SECONDS;
        case MINUTES: return ChronoUnit.MINUTES;
        case HOURS: return ChronoUnit.HOURS;
        case DAYS: return ChronoUnit.DAYS;
        default: throw new AssertionError();
    }
}

While I am not usually a fan of copying a method like this, the alternatives would probably need to do some conversions using the java.time.Duration.ofXxx methods like ofMillis and ofNanos, which would be more complicated in order to make sure there is no loss of precision. One big advantage is that using the Duration#of method takes care of the complexity of dealing with the various units. Have a look at the implementation of java.time.Duration#of and specifically all the details inside the plus(long amountToAdd, TemporalUnit unit) method that it calls.

Assuming the above makes sense, I’d suggest doing separate pull requests for the 2.1.x branch and the 3.x and/or 4.x branches. The 3.x and 4.x branches can make use of the TimeUnit#toChronoUnit method and have the simple one line implementation, while the 2.1.x branch would use the JDK 8 code above.

If you think the above makes sense, I can create the PRs for the 2.1.x branch and then for the 3.x branch and optionally the 4.x branch. Or, if the JDK 8 solution doesn’t make sense or you have a better idea that I didn’t think of, I’d be happy to implement that as well.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Dropwizard Core
A Task is a run-time action your application provides access to on the administrative port via HTTP. All Dropwizard applications start with: the...
Read more >
How does FlexyPool support the Dropwizard Metrics package ...
Being integrated into Dropwizard, the package name was bound to be renamed. So instead of com.codahale.metrics the 4.0.0 release will use ...
Read more >
Make metric names configurable · Issue #76 · vert-x3/vertx ...
Hi, I've got an intention to override default metrics names Vertx provides. For example, I'm not happy with this metric for HTTP server ......
Read more >
Rename fields from events | Metricbeat Reference [7.17] | Elastic
The rename processor cannot be used to overwrite fields. ... is a subfield of c ), assigning scalar values results in an Elasticsearch...
Read more >
Difference between Application and Service in Dropwizard
Added HibernateBundle#configure(Configuration) for customization of Hibernate configuration. Added support for Joda Time DateTime arguments and results when ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found