Last active
July 5, 2018 08:03
-
-
Save AniketSK/fe97e889efbde7fbdc01b912c10753a7 to your computer and use it in GitHub Desktop.
A demonstration of setting and accessing time in a unit-safe way.
This file contains 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
import java.util.concurrent.TimeUnit; | |
public class ExerciseJava { | |
private long duration; | |
public void setDuration(long duration, TimeUnit unit) { | |
this.duration = TimeUnit.MILLISECONDS.convert(duration, unit); | |
} | |
public long getDuration(TimeUnit unit) { | |
return unit.convert(duration, TimeUnit.MILLISECONDS); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Alternatively, if it makes sense. You could declare the unit and it's bindings more explicitly, but this I feel is a tradeoff for clarity.