TimeUtils { public static String toTimePrecision(final TimeUnit t) { switch (t) { case HOURS: return "h"; case MINUTES: return "m"; case SECONDS: return "s"; case MILLISECONDS: return "ms"; case MICROSECONDS: return "u"; case NANOSECONDS: return "n"; default: EnumSet<TimeUnit> allowedTimeunits = EnumSet.of( TimeUnit.HOURS, TimeUnit.MINUTES, TimeUnit.SECONDS, TimeUnit.MILLISECONDS, TimeUnit.MICROSECONDS, TimeUnit.NANOSECONDS); throw new IllegalArgumentException("time precision must be one of:" + allowedTimeunits); } }  static String toTimePrecision(final TimeUnit t);  }
@Test public void test() { assertThat("m".equals(TimeUtils.toTimePrecision(TimeUnit.MINUTES))); assertThat("h".equals(TimeUtils.toTimePrecision(TimeUnit.HOURS))); assertThat("s".equals(TimeUtils.toTimePrecision(TimeUnit.SECONDS))); assertThat("ms".equals(TimeUtils.toTimePrecision(TimeUnit.MILLISECONDS))); assertThat("u".equals(TimeUtils.toTimePrecision(TimeUnit.MICROSECONDS))); assertThat("n".equals(TimeUtils.toTimePrecision(TimeUnit.NANOSECONDS))); }
@Test(expected = IllegalArgumentException.class) public void shouldThrowException() { TimeUtils.toTimePrecision(TimeUnit.DAYS); }