How can I make an API for a class that the user passes a TimeUnit e.g. minutes, seconds, hours and a number and keep the millisec value internally in the class.
The following seems the only way?
void someMethodDays(int numOfDays) {
this.longValue = TimeUnit.DAYS.toMillis(numOfDays);
}
void someMethodHour(int numOfHours) {
this.longValue = TimeUnit.HOURS.toMillis(numOfDays);
} etc
Is this the only way? A method per value with a descriptive name for argument?
You could model your class after a well-known and tested class:
java.time.LocalDate, which provides theplus(long, TemporalUnit)method.Similarly, you could create a
someMethod(long, TimeUnit)method that allows callers to pass in arbitrary amounts of anyTimeUnit.Note that
LocalDatealso provides specialized methods for adding certain common units of time, likeplusDays(). That gives the caller the ability to decide which is clearer for the code they're writing: