DateConverter { public static Long[][] encode(final Collection<Date> dates) { if (dates == null || dates.isEmpty()) return null; final int remainder = dates.size() % MAX_ARRAY_SIZE; final int nbArrays = dates.size() / MAX_ARRAY_SIZE + (remainder == 0 ? 0 : 1); final Long[][] arrays = new Long[nbArrays][]; for (int i = 0; i < arrays.length - 1; i++) { arrays[i] = new Long[MAX_ARRAY_SIZE]; } arrays[arrays.length - 1] = new Long[remainder == 0 ? MAX_ARRAY_SIZE : remainder]; int i = 0; for (final Date date : dates) { arrays[i / MAX_ARRAY_SIZE][i % MAX_ARRAY_SIZE] = encode(date); i++; } return arrays; } private  DateConverter(); static Long[][] encode(final Collection<Date> dates); static Long encode(final Date date); static Date decode(final long timestamp); static long toTimestamp(final Date date); static Date fromTimestamp(final long timestamp);  }
@Test public void testEncodeDates() { final List<Long> timestamps = new ArrayList<>(); for (int i = 0; i < 256; i++) { timestamps.add(i * 1000L); } final Long[][] encoded = DateConverter.encode(timestamps.stream().map(Date::new).collect(Collectors.toList())); assertEquals(encoded.length, 2); assertEquals(encoded[0].length, 255); assertEquals(encoded[1].length, 1); int i = 0; for (final Long[] part : encoded) { for (final Long v : part) { assertEquals("Index: " + i, v, timestamps.get(i++)); } } assertNull(DateConverter.encode((List<Date>) null)); assertNull(DateConverter.encode(List.of())); }
@Test public void testEncodeDate() { final int expectedTimestamp = 123; final long timestamp = DateConverter.encode(new Date(expectedTimestamp)); assertEquals(expectedTimestamp, timestamp); assertNull(DateConverter.encode((Date) null)); }