public void testCoverTimePoint() { Interval interval = new Interval(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T120000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T160000")); double timePoint1 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T120000"); double timePoint2 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T130000"); double timePoint3 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T170000"); double timePoint4 = net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T110000"); Assert.AssertEquals(true, interval.covers(timePoint1)); Assert.AssertEquals(true, interval.covers(timePoint2)); Assert.AssertEquals(false, interval.covers(timePoint3)); Assert.AssertEquals(false, interval.covers(timePoint4)); }
public void testConstruction() { // Construct with the right parameters. Interval interval1 = new Interval(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T120000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T160000")); Assert.AssertEquals("20150825T120000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(interval1.getStartTime())); Assert.AssertEquals("20150825T160000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(interval1.getEndTime())); Assert.AssertEquals(true, interval1.isValid()); // Construct with the invalid interval. Interval interval2 = new Interval(); Assert.AssertEquals(false, interval2.isValid()); // Construct with the empty interval. Interval interval3 = new Interval(true); Assert.AssertEquals(true, interval3.isValid()); Assert.AssertEquals(true, interval3.isEmpty()); }
public Result(bool isPositive, Interval interval) { this.isPositive = isPositive; this.interval = interval; }
/// <summary> /// A helper function to calculate black interval results or white interval /// results. /// </summary> /// /// <param name="list"></param> /// <param name="timeStamp">The time stamp as milliseconds since Jan 1, 1970 UTC.</param> /// <param name="positiveResult">The positive result which is updated.</param> /// <param name="negativeResult">The negative result which is updated.</param> private static void calculateIntervalResult( HashedSet<RepetitiveInterval> list, double timeStamp, Interval positiveResult, Interval negativeResult) { Object[] array = ILOG.J2CsMapping.Collections.Collections.ToArray(list); System.Array.Sort(array); /* foreach */ foreach (Object elementObj in array) { RepetitiveInterval element = (RepetitiveInterval) elementObj; RepetitiveInterval.Result result = element.getInterval(timeStamp); Interval tempInterval = result.interval; if (result.isPositive == true) { try { positiveResult.unionWith(tempInterval); } catch (Interval.Error ex) { // We don't expect to get this error. throw new Exception("Error in Interval.unionWith: " + ex.Message); } } else { if (!negativeResult.isValid()) negativeResult.set(tempInterval); else negativeResult.intersectWith(tempInterval); } } }
/// <summary> /// Get the interval that covers the time stamp. This iterates over the two /// repetitive interval sets and find the shortest interval that allows a group /// member to access the data. If there is no interval covering the time stamp, /// this returns false for isPositive and returns a negative interval. /// </summary> /// /// <param name="timeStamp">The time stamp as milliseconds since Jan 1, 1970 UTC.</param> /// <returns>An object with fields (isPositive, interval) where isPositive is /// true if the returned interval is positive or false if negative, and /// interval is the Interval covering the time stamp, or a negative interval if /// not found.</returns> public Schedule.Result getCoveringInterval(double timeStamp) { Interval blackPositiveResult = new Interval(true); Interval whitePositiveResult = new Interval(true); Interval blackNegativeResult = new Interval(); Interval whiteNegativeResult = new Interval(); // Get the black result. calculateIntervalResult(blackIntervalList_, timeStamp, blackPositiveResult, blackNegativeResult); // If the black positive result is not empty, then isPositive must be false. if (!blackPositiveResult.isEmpty()) return new Schedule.Result (false, blackPositiveResult); // Get the whiteResult. calculateIntervalResult(whiteIntervalList_, timeStamp, whitePositiveResult, whiteNegativeResult); if (whitePositiveResult.isEmpty() && !whiteNegativeResult.isValid()) { // There is no white interval covering the time stamp. // Return false and a 24-hour interval. double timeStampDateOnly = net.named_data.jndn.encrypt.RepetitiveInterval .toDateOnlyMilliseconds(timeStamp); return new Schedule.Result (false, new Interval(timeStampDateOnly, timeStampDateOnly + MILLISECONDS_IN_DAY)); } if (!whitePositiveResult.isEmpty()) { // There is white interval covering the time stamp. // Return true and calculate the intersection. if (blackNegativeResult.isValid()) return new Schedule.Result (true, whitePositiveResult.intersectWith(blackNegativeResult)); else return new Schedule.Result (true, whitePositiveResult); } else // There is no white interval covering the time stamp. // Return false. return new Schedule.Result (false, whiteNegativeResult); }
public void testIntersectionAndUnion() { Interval interval1 = new Interval(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T030000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T050000")); // No intersection. Interval interval2 = new Interval(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T050000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T070000")); // No intersection. Interval interval3 = new Interval(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T060000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T070000")); // There's an intersection. Interval interval4 = new Interval(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T010000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T040000")); // Right in interval1, there's an intersection. Interval interval5 = new Interval(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T030000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T040000")); // Wrap interval1, there's an intersection. Interval interval6 = new Interval(net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T010000"), net.named_data.jndn.tests.unit_tests.UnitTestsCommon.fromIsoString("20150825T050000")); // Empty interval. Interval interval7 = new Interval(true); Interval tempInterval = new Interval(interval1); tempInterval.intersectWith(interval2); Assert.AssertEquals(true, tempInterval.isEmpty()); tempInterval = new Interval(interval1); bool gotError = true; try { tempInterval.unionWith(interval2); gotError = false; } catch (Exception ex) { } if (!gotError) Assert.Fail("Expected error in unionWith(interval2)"); tempInterval = new Interval(interval1); tempInterval.intersectWith(interval3); Assert.AssertEquals(true, tempInterval.isEmpty()); tempInterval = new Interval(interval1); gotError = true; try { tempInterval.unionWith(interval3); gotError = false; } catch (Interval.Error ex_0) { } if (!gotError) Assert.Fail("Expected error in unionWith(interval3)"); tempInterval = new Interval(interval1); tempInterval.intersectWith(interval4); Assert.AssertEquals(false, tempInterval.isEmpty()); Assert.AssertEquals("20150825T030000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getStartTime())); Assert.AssertEquals("20150825T040000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getEndTime())); tempInterval = new Interval(interval1); tempInterval.unionWith(interval4); Assert.AssertEquals(false, tempInterval.isEmpty()); Assert.AssertEquals("20150825T010000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getStartTime())); Assert.AssertEquals("20150825T050000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getEndTime())); tempInterval = new Interval(interval1); tempInterval.intersectWith(interval5); Assert.AssertEquals(false, tempInterval.isEmpty()); Assert.AssertEquals("20150825T030000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getStartTime())); Assert.AssertEquals("20150825T040000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getEndTime())); tempInterval = new Interval(interval1); tempInterval.unionWith(interval5); Assert.AssertEquals(false, tempInterval.isEmpty()); Assert.AssertEquals("20150825T030000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getStartTime())); Assert.AssertEquals("20150825T050000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getEndTime())); tempInterval = new Interval(interval1); tempInterval.intersectWith(interval6); Assert.AssertEquals(false, tempInterval.isEmpty()); Assert.AssertEquals("20150825T030000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getStartTime())); Assert.AssertEquals("20150825T050000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getEndTime())); tempInterval = new Interval(interval1); tempInterval.unionWith(interval6); Assert.AssertEquals(false, tempInterval.isEmpty()); Assert.AssertEquals("20150825T010000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getStartTime())); Assert.AssertEquals("20150825T050000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getEndTime())); tempInterval = new Interval(interval1); tempInterval.intersectWith(interval7); Assert.AssertEquals(true, tempInterval.isEmpty()); tempInterval = new Interval(interval1); tempInterval.unionWith(interval7); Assert.AssertEquals(false, tempInterval.isEmpty()); Assert.AssertEquals("20150825T030000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getStartTime())); Assert.AssertEquals("20150825T050000", net.named_data.jndn.tests.unit_tests.UnitTestsCommon.toIsoString(tempInterval.getEndTime())); }
/// <summary> /// Calculate an Interval that covers the timeSlot. /// </summary> /// /// <param name="timeSlot">The time slot to cover as milliseconds since Jan 1, 1970 UTC.</param> /// <param name="memberKeys">of the public key and the value is the Blob of the public key DER. (Use Map without generics so it works with older Java compilers.)</param> /// <returns>The Interval covering the time slot.</returns> /// <exception cref="GroupManagerDb.Error">for a database error.</exception> private Interval calculateInterval(double timeSlot, IDictionary memberKeys) { // Prepare. Interval positiveResult = new Interval(); Interval negativeResult = new Interval(); memberKeys.clear(); // Get the all intervals from the schedules. IList scheduleNames = database_.listAllScheduleNames(); for (int i = 0; i < scheduleNames.Count; ++i) { String scheduleName = (String) scheduleNames[i]; Schedule schedule = database_.getSchedule(scheduleName); Schedule.Result result = schedule.getCoveringInterval(timeSlot); Interval tempInterval = result.interval; if (result.isPositive) { if (!positiveResult.isValid()) positiveResult = tempInterval; positiveResult.intersectWith(tempInterval); IDictionary map = database_.getScheduleMembers(scheduleName); ILOG.J2CsMapping.Collections.Collections.PutAll(memberKeys,map); } else { if (!negativeResult.isValid()) negativeResult = tempInterval; negativeResult.intersectWith(tempInterval); } } if (!positiveResult.isValid()) // Return an invalid interval when there is no member which has an // interval covering the time slot. return new Interval(false); // Get the final interval result. Interval finalInterval; if (negativeResult.isValid()) finalInterval = positiveResult.intersectWith(negativeResult); else finalInterval = positiveResult; return finalInterval; }
/// <summary> /// Create an Interval, copying values from the other interval. /// </summary> /// /// <param name="interval">The other Interval with values to copy</param> public Interval(Interval interval) { startTime_ = interval.startTime_; endTime_ = interval.endTime_; isValid_ = interval.isValid_; }
/// <summary> /// Set this Interval to the union of this and the other interval. /// This and the other interval should be valid but either can be empty. /// This and the other interval should have an intersection. (Contiguous /// intervals are not allowed.) /// </summary> /// /// <param name="interval">The other Interval to union with.</param> /// <returns>This Interval.</returns> /// <exception cref="Interval.Error">if the two intervals do not have an intersection.</exception> public Interval unionWith(Interval interval) { if (!isValid_) throw new Exception( "Interval.intersectWith: This Interval is invalid"); if (!interval.isValid_) throw new Exception( "Interval.intersectWith: The other Interval is invalid"); if (isEmpty()) { // This interval is empty, so use the other. startTime_ = interval.startTime_; endTime_ = interval.endTime_; return this; } if (interval.isEmpty()) // The other interval is empty, so keep using this one. return this; if (startTime_ >= interval.endTime_ || endTime_ <= interval.startTime_) throw new Interval.Error( "Interval.unionWith: The two intervals do not have an intersection"); // Get the start time. if (startTime_ > interval.startTime_) startTime_ = interval.startTime_; // Get the end time. if (endTime_ < interval.endTime_) endTime_ = interval.endTime_; return this; }
/// <summary> /// Set this Interval to the intersection of this and the other interval. /// This and the other interval should be valid but either can be empty. /// </summary> /// /// <param name="interval">The other Interval to intersect with.</param> /// <returns>This Interval.</returns> public Interval intersectWith(Interval interval) { if (!isValid_) throw new Exception( "Interval.intersectWith: This Interval is invalid"); if (!interval.isValid_) throw new Exception( "Interval.intersectWith: The other Interval is invalid"); if (isEmpty() || interval.isEmpty()) { // If either is empty, the result is empty. startTime_ = endTime_; return this; } if (startTime_ >= interval.endTime_ || endTime_ <= interval.startTime_) { // The two intervals don't have an intersection, so the result is empty. startTime_ = endTime_; return this; } // Get the start time. if (startTime_ <= interval.startTime_) startTime_ = interval.startTime_; // Get the end time. if (endTime_ > interval.endTime_) endTime_ = interval.endTime_; return this; }