blob: f5738a14b3d4a20f3da308937d0d2ce1c6551f57 [file] [log] [blame]
Zhiyi Zhangcea58d52015-08-26 10:19:56 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2015, Regents of the University of California
4 *
5 * This file is part of ndn-group-encrypt (Group-based Encryption Protocol for NDN).
6 * See AUTHORS.md for complete list of ndn-group-encrypt authors and contributors.
7 *
8 * ndn-group-encrypt is free software: you can redistribute it and/or modify it under the terms
9 * of the GNU General Public License as published by the Free Software Foundation,
10 * either version 3 of the License, or (at your option) any later version.
11 *
12 * ndn-group-encrypt is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
13 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14 * PURPOSE. See the GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along with
17 * ndn-group-encrypt, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * @author Zhiyi Zhang <dreamerbarrychang@gmail.com>
20 */
21
22#include "interval.hpp"
23
24namespace ndn {
25namespace gep {
26
27static const TimeStamp DEFAULT_TIME = boost::posix_time::from_iso_string("14000101T000000");
28
29Interval::Interval(bool isValid)
30 : m_startTime(DEFAULT_TIME)
31 , m_endTime(DEFAULT_TIME)
32 , m_isValid(isValid)
33{
34}
35
36Interval::Interval(const TimeStamp& startTime,
37 const TimeStamp& endTime)
38 : m_startTime(startTime)
39 , m_endTime(endTime)
40 , m_isValid(true)
41{
42 BOOST_ASSERT(startTime < endTime);
43}
44
45bool
46Interval::covers(const TimeStamp& tp) const
47{
48 BOOST_ASSERT(isValid());
49
50 if (isEmpty())
51 return false;
52 return (m_startTime <= tp && tp < m_endTime);
53}
54
55Interval&
56Interval::operator &&(const Interval& interval)
57{
58 BOOST_ASSERT(isValid() && interval.isValid());
59
60 // if one is empty, result is empty
61 if (isEmpty() || interval.isEmpty()) {
62 m_startTime = m_endTime;
63 return *this;
64 }
65 // two intervals do not have intersection
66 if (m_startTime >= interval.getEndTime() || m_endTime <= interval.getStartTime()) {
67 m_startTime = m_endTime;
68 return *this;
69 }
70
71 // get the start time
72 if (m_startTime <= interval.getStartTime())
73 m_startTime = interval.getStartTime();
74
75 // get the end time
76 if (m_endTime > interval.getEndTime())
77 m_endTime = interval.getEndTime();
78
79 return *this;
80}
81
82Interval&
83Interval::operator ||(const Interval& interval)
84{
85 BOOST_ASSERT(this->isValid() && interval.isValid());
86
87 if (isEmpty()) {
88 // left interval is empty, return left one
89 m_startTime = interval.getStartTime();
90 m_endTime = interval.getEndTime();
91 return *this;
92 }
93 if (interval.isEmpty()) {
94 // right interval is empty, return right one
95 return *this;
96 }
97 if (m_startTime >= interval.getEndTime() || m_endTime <= interval.getStartTime()) {
98 // two intervals do not have intersection
99 BOOST_THROW_EXCEPTION(Error("cannot generate a union interval when there's no intersection"));
100 }
101
102 // get the start time
103 if (m_startTime > interval.getStartTime())
104 m_startTime = interval.getStartTime();
105
106 // get the end time
107 if (m_endTime <= interval.getEndTime())
108 m_endTime = interval.getEndTime();
109
110 return *this;
111}
112
113} // namespace gep
114} // namespace ndn