SlotMapper { public String map(long time, String service) { if (time < 0) { throw new IllegalArgumentException("Timestamp was less than zero"); } long slotnum = time / SLOT_LENGTH; String slot = slotCache.get(slotnum); if (null == slot) { if (slotCache.size() > LARGE_CACHE) { slotCache.clear(); } slot = mapToPath(time, service); slotCache.put(slotnum, slot); } return slot; }  String map(long time, String service); static String mapToPath(long time, String service); static final TimeZone TZ; static final long SLOT_LENGTH; static final int LARGE_CACHE; }
@Test public void testMapping() throws Exception { SlotMapper mapper = new SlotMapper(); String expected = "2011" + File.separator + "01" + File.separator + "24" + File.separator + SERVICENAME + "_2011-01-24_12"; assertEquals(expected, mapper.map(t1, SERVICENAME)); }
@Test (timeout = 1000) public void testPerformance() throws Exception { SlotMapper mapper = new SlotMapper(); long start = System.currentTimeMillis(); int reps = 1000000; for (int i = 0; i < reps; i++) { assertNotNull(mapper.map(t1 + (i * 5000), SERVICENAME)); } long duration = System.currentTimeMillis() - start; log.fine("Mapping reps=" + reps + ", duration= " + duration + " ms (" + ((reps / duration) * 1000) + " maps/sec)" ); }
@Test public void testForZero() throws Exception { if (! "/".equals(File.separator)) { return; } SlotMapper mapper = new SlotMapper(); assertEquals("1970/01/01/"+SERVICENAME+"_1970-01-01_00", mapper.map(0L, SERVICENAME)); }
@Test (expected = IllegalArgumentException.class) public void testNegative() throws Exception { SlotMapper mapper = new SlotMapper(); mapper.map(-1L, SERVICENAME); }
@Test public void testBoundaries() throws Exception { if (! "/".equals(File.separator)) { return; } SlotMapper mapper = new SlotMapper(); GregorianCalendar c1; GregorianCalendar c2; c1 = new GregorianCalendar(2011,0,20,1,59,59); c2 = new GregorianCalendar(2011,0,20,2,0,0); ensureDifferent(mapper, c1, c2); ensureConsistentMapping(mapper, c1); ensureConsistentMapping(mapper, c2); assertEquals("2011/01/20/"+SERVICENAME+"_2011-01-20_01", mapper.map(c1.getTimeInMillis(), SERVICENAME)); assertEquals("2011/01/20/"+SERVICENAME+"_2011-01-20_02", mapper.map(c2.getTimeInMillis(), SERVICENAME)); c1 = new GregorianCalendar(2011,0,20,23,59,59); c2 = new GregorianCalendar(2011,0,21,0,0,0); ensureDifferent(mapper, c1, c2); ensureConsistentMapping(mapper, c1); ensureConsistentMapping(mapper, c2); assertEquals("2011/01/20/"+SERVICENAME+"_2011-01-20_23", mapper.map(c1.getTimeInMillis(), SERVICENAME)); assertEquals("2011/01/21/"+SERVICENAME+"_2011-01-21_00", mapper.map(c2.getTimeInMillis(), SERVICENAME)); c1 = new GregorianCalendar(2011,0,31,23,59,59); c2 = new GregorianCalendar(2011,1,1,0,0,0); ensureDifferent(mapper, c1, c2); ensureConsistentMapping(mapper, c1); ensureConsistentMapping(mapper, c2); assertEquals("2011/01/31/"+SERVICENAME+"_2011-01-31_23", mapper.map(c1.getTimeInMillis(), SERVICENAME)); assertEquals("2011/02/01/"+SERVICENAME+"_2011-02-01_00", mapper.map(c2.getTimeInMillis(), SERVICENAME)); c1 = new GregorianCalendar(2011,1,28,23,59,59); c2 = new GregorianCalendar(2011,2,1,0,0,0); ensureDifferent(mapper, c1, c2); ensureConsistentMapping(mapper, c1); ensureConsistentMapping(mapper, c2); assertEquals("2011/02/28/"+SERVICENAME+"_2011-02-28_23", mapper.map(c1.getTimeInMillis(), SERVICENAME)); assertEquals("2011/03/01/"+SERVICENAME+"_2011-03-01_00", mapper.map(c2.getTimeInMillis(), SERVICENAME)); c1 = new GregorianCalendar(2012,1,29,23,59,59); c2 = new GregorianCalendar(2012,2,1,0,0,0); ensureDifferent(mapper, c1, c2); ensureConsistentMapping(mapper, c1); ensureConsistentMapping(mapper, c2); assertEquals("2012/02/29/"+SERVICENAME+"_2012-02-29_23", mapper.map(c1.getTimeInMillis(), SERVICENAME)); assertEquals("2012/03/01/"+SERVICENAME+"_2012-03-01_00", mapper.map(c2.getTimeInMillis(), SERVICENAME)); c1 = new GregorianCalendar(2011,11,31,23,59,59); c2 = new GregorianCalendar(2012,0,1,0,0,0); ensureDifferent(mapper, c1, c2); ensureConsistentMapping(mapper, c1); ensureConsistentMapping(mapper, c2); assertEquals("2011/12/31/"+SERVICENAME+"_2011-12-31_23", mapper.map(c1.getTimeInMillis(), SERVICENAME)); assertEquals("2012/01/01/"+SERVICENAME+"_2012-01-01_00", mapper.map(c2.getTimeInMillis(), SERVICENAME)); }