/// <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)); } }
/// <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); }
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()); }
/// <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); }
/// <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> /// 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; }