TimeFrameImpl implements TimeFrame { @Override public boolean isDerivableFrom(TimeFrame tf) { TimeFrameImpl other = (TimeFrameImpl)tf; boolean retVal = false; PeriodType otherType = other.getPeriod().getPeriodType(); PeriodType thisType = period.getPeriodType(); int otherDuration = other.getPeriod().size(); int thisDuration = getPeriod().size(); if(equals(other)) { retVal = true; }else{ if(((otherType == thisType && otherType != PeriodType.WEEK && (otherDuration == thisDuration || (thisDuration % otherDuration == 0 && other.startDate.isEqual(startDate) && areEqualOrNull(other.endDate, endDate)))) || (otherType.isLowerThan(thisType) && otherType != PeriodType.WEEK && otherDuration == 1))) { retVal = true; } PeriodType resolutionDeterminer = otherType.isLowerThan(thisType) ? thisType : otherType; if(resolutionDeterminer.resolutionInstant(other.startDate).isAfter(resolutionDeterminer.resolutionInstant(startDate))) { retVal = false; } if((other.endDate != null && endDate == null) || ((other.endDate != null && endDate != null) && resolutionDeterminer.resolutionInstant(other.endDate).isAfter(resolutionDeterminer.resolutionInstant(endDate)))) { retVal = false; } } return retVal; } TimeFrameImpl(Period period, DateTime startDate, DateTime endDate); @Override Period getPeriod(); @Override DateTime getStartDate(); @Override DateTime getEndDate(); @Override boolean isDerivableFrom(TimeFrame tf); @Override int hashCode(); @Override boolean equals(Object obj); String toString();  }
@Test public void testIsDerivableFrom() { DateTime dt1 = new DateTime(2013, 12, 10, 12, 0, 0); TimeFrameImpl tf1 = new TimeFrameImpl(new Period(PeriodType.TICK, 1), dt1, null); DateTime dt2 = new DateTime(2013, 12, 10, 12, 0, 0); TimeFrameImpl tf2 = new TimeFrameImpl(new Period(PeriodType.SECOND, 1), dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); assertFalse(tf1.isDerivableFrom(tf2)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.SECOND, 1), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(Period.ONE_MINUTE, dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); assertFalse(tf1.isDerivableFrom(tf2)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 1), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(Period.ONE_HOUR, dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); assertFalse(tf1.isDerivableFrom(tf2)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(Period.ONE_HOUR, dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(Period.DAY, dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); assertFalse(tf1.isDerivableFrom(tf2)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(Period.DAY, dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(Period.WEEK, dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); assertFalse(tf1.isDerivableFrom(tf2)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(Period.WEEK, dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(Period.MONTH, dt2, null); assertFalse(tf2.isDerivableFrom(tf1)); assertFalse(tf1.isDerivableFrom(tf2)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(Period.MONTH, dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(Period.QUARTER, dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); assertFalse(tf1.isDerivableFrom(tf2)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(Period.QUARTER, dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(Period.YEAR, dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); assertFalse(tf1.isDerivableFrom(tf2)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(Period.ONE_MINUTE, dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 6), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, null); assertFalse(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 11, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, null); assertFalse(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt1, new DateTime(2013, 12, 20, 12, 0, 0)); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, null); assertFalse(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, new DateTime(2013, 12, 20, 12, 0, 0)); assertTrue(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 5), dt1, new DateTime(2013, 12, 20, 12, 0, 0)); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, new DateTime(2013, 12, 20, 12, 0, 0)); assertTrue(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 1), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, null); assertTrue(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 13, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 1), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, null); assertFalse(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 1), dt1, null); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, new DateTime(2013, 12, 30, 12, 0, 0)); assertTrue(tf2.isDerivableFrom(tf1)); dt1 = new DateTime(2013, 12, 10, 12, 0, 0); tf1 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 1), dt1, new DateTime(2013, 12, 30, 12, 0, 0)); dt2 = new DateTime(2013, 12, 10, 12, 0, 0); tf2 = new TimeFrameImpl(new Period(PeriodType.MINUTE, 10), dt2, null); assertFalse(tf2.isDerivableFrom(tf1)); }