ProfilePeriod implements Serializable { public static ProfilePeriod fromTimestamp(long epochMillis, long duration, TimeUnit units) { if(duration <= 0) { throw new IllegalArgumentException(format("period duration must be > 0; got '%d %s'", duration, units)); } long durationMillis = units.toMillis(duration); long periodId = epochMillis / durationMillis; return new ProfilePeriod(periodId, duration, units); } ProfilePeriod(); private  ProfilePeriod(long periodId, long duration, TimeUnit units); static ProfilePeriod fromTimestamp(long epochMillis, long duration, TimeUnit units); static ProfilePeriod fromPeriodId(long periodId, long duration, TimeUnit units); long getStartTimeMillis(); long getEndTimeMillis(); ProfilePeriod next(); long getPeriod(); long getDurationMillis(); @Override boolean equals(Object o); @Override int hashCode(); @Override String toString(); static List<T> visitPeriods(long startEpochMillis
                                        , long endEpochMillis
                                        , long duration
                                        , TimeUnit units
                                        , Optional<Predicate<ProfilePeriod>> inclusionPredicate
                                        , Function<ProfilePeriod,T> transformation
                                        );  }
@Test public void testPeriodDurationOfZero() { long duration = 0; TimeUnit units = TimeUnit.HOURS; assertThrows(IllegalArgumentException.class, () -> ProfilePeriod.fromTimestamp(0, duration, units)); }
@Test public void testKryoSerialization() { ProfilePeriod expected = ProfilePeriod.fromTimestamp(AUG2016, 1, TimeUnit.HOURS); Kryo kryo = new Kryo(); ByteArrayOutputStream byteStream = new ByteArrayOutputStream(); Output output = new Output(byteStream); kryo.writeObject(output, expected); byte[] bits = output.toBytes(); assertNotNull(bits); Input input = new Input(new ByteArrayInputStream(bits)); ProfilePeriod actual = kryo.readObject(input, ProfilePeriod.class); assertNotNull(actual); assertEquals(expected, actual); }
@Test public void testJavaSerialization() throws Exception { ProfilePeriod expected = ProfilePeriod.fromTimestamp(AUG2016, 1, TimeUnit.HOURS); ByteArrayOutputStream bytes = new ByteArrayOutputStream(); ObjectOutputStream out = new ObjectOutputStream(bytes); out.writeObject(expected); byte[] raw = bytes.toByteArray(); assertTrue(raw.length > 0); ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(raw)); Object actual = in.readObject(); assertEquals(expected, actual); }