Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / trace-viewer / third_party / tvcm / src / tvcm / range.js
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 'use strict';
6
7 /**
8  * @fileoverview Quick range computations.
9  */
10 tvcm.exportTo('tvcm', function() {
11
12   function Range() {
13     this.isEmpty_ = true;
14     this.min_ = undefined;
15     this.max_ = undefined;
16   };
17
18   Range.prototype = {
19     __proto__: Object.prototype,
20
21     reset: function() {
22       this.isEmpty_ = true;
23       this.min_ = undefined;
24       this.max_ = undefined;
25     },
26
27     get isEmpty() {
28       return this.isEmpty_;
29     },
30
31     addRange: function(range) {
32       if (range.isEmpty)
33         return;
34       this.addValue(range.min);
35       this.addValue(range.max);
36     },
37
38     addValue: function(value) {
39       if (this.isEmpty_) {
40         this.max_ = value;
41         this.min_ = value;
42         this.isEmpty_ = false;
43         return;
44       }
45       this.max_ = Math.max(this.max_, value);
46       this.min_ = Math.min(this.min_, value);
47     },
48
49     set min(min) {
50       this.isEmpty_ = false;
51       this.min_ = min;
52     },
53
54     get min() {
55       if (this.isEmpty_)
56         return undefined;
57       return this.min_;
58     },
59
60     get max() {
61       if (this.isEmpty_)
62         return undefined;
63       return this.max_;
64     },
65
66     set max(max) {
67       this.isEmpty_ = false;
68       this.max_ = max;
69     },
70
71     get range() {
72       if (this.isEmpty_)
73         return undefined;
74       return this.max_ - this.min_;
75     },
76
77     get center() {
78       return (this.min_ + this.max_) * 0.5;
79     },
80
81     equals: function(that) {
82       if (this.isEmpty && that.isEmpty)
83         return true;
84       if (this.isEmpty != that.isEmpty)
85         return false;
86       return this.min === that.min &&
87           this.max === that.max;
88     },
89
90     containsRange: function(range) {
91       if (this.isEmpty || range.isEmpty)
92         return false;
93       return this.min <= range.min && this.max >= range.max;
94     },
95
96     containsExplicitRange: function(min, max) {
97       if (this.isEmpty)
98         return false;
99       return this.min <= min && this.max >= max;
100     },
101
102     intersectsRange: function(range) {
103       if (this.isEmpty || range.isEmpty)
104         return false;
105       return !(range.max < this.min ||
106                range.min > this.max);
107     },
108
109     intersectsExplicitRange: function(min, max) {
110       if (this.isEmpty)
111         return false;
112       return !(max < this.min ||
113                min > this.max);
114     }
115   };
116
117   Range.compareByMinTimes = function(a, b) {
118     if (!a.isEmpty && !b.isEmpty)
119       return a.min_ - b.min_;
120
121     if (a.isEmpty && !b.isEmpty)
122       return -1;
123
124     if (!a.isEmpty && b.isEmpty)
125       return 1;
126
127     return 0;
128   };
129
130   return {
131     Range: Range
132   };
133
134 });