1/*
2 * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "TimeRanges.h"
28
29namespace WebCore {
30
31Ref<TimeRanges> TimeRanges::create()
32{
33 return adoptRef(*new TimeRanges);
34}
35
36Ref<TimeRanges> TimeRanges::create(double start, double end)
37{
38 return adoptRef(*new TimeRanges(start, end));
39}
40
41Ref<TimeRanges> TimeRanges::create(const PlatformTimeRanges& other)
42{
43 return adoptRef(*new TimeRanges(other));
44}
45
46TimeRanges::TimeRanges()
47{
48}
49
50TimeRanges::TimeRanges(double start, double end)
51 : m_ranges(PlatformTimeRanges(MediaTime::createWithDouble(start), MediaTime::createWithDouble(end)))
52{
53}
54
55TimeRanges::TimeRanges(const PlatformTimeRanges& other)
56 : m_ranges(other)
57{
58}
59
60ExceptionOr<double> TimeRanges::start(unsigned index) const
61{
62 bool valid;
63 MediaTime result = m_ranges.start(index, valid);
64 if (!valid)
65 return Exception { IndexSizeError };
66 return result.toDouble();
67}
68
69ExceptionOr<double> TimeRanges::end(unsigned index) const
70{
71 bool valid;
72 MediaTime result = m_ranges.end(index, valid);
73 if (!valid)
74 return Exception { IndexSizeError };
75 return result.toDouble();
76}
77
78void TimeRanges::invert()
79{
80 m_ranges.invert();
81}
82
83Ref<TimeRanges> TimeRanges::copy() const
84{
85 return TimeRanges::create(m_ranges);
86}
87
88void TimeRanges::intersectWith(const TimeRanges& other)
89{
90 m_ranges.intersectWith(other.ranges());
91}
92
93void TimeRanges::unionWith(const TimeRanges& other)
94{
95 m_ranges.unionWith(other.ranges());
96}
97
98unsigned TimeRanges::length() const
99{
100 return m_ranges.length();
101}
102
103void TimeRanges::add(double start, double end)
104{
105 m_ranges.add(MediaTime::createWithDouble(start), MediaTime::createWithDouble(end));
106}
107
108bool TimeRanges::contain(double time) const
109{
110 return m_ranges.contain(MediaTime::createWithDouble(time));
111}
112
113size_t TimeRanges::find(double time) const
114{
115 return m_ranges.find(MediaTime::createWithDouble(time));
116}
117
118double TimeRanges::nearest(double time) const
119{
120 return m_ranges.nearest(MediaTime::createWithDouble(time)).toDouble();
121}
122
123double TimeRanges::totalDuration() const
124{
125 return m_ranges.totalDuration().toDouble();
126}
127
128}
129