TimeIntervalCollection.js 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. import binarySearch from './binarySearch.js';
  2. import defaultValue from './defaultValue.js';
  3. import defined from './defined.js';
  4. import defineProperties from './defineProperties.js';
  5. import DeveloperError from './DeveloperError.js';
  6. import Event from './Event.js';
  7. import GregorianDate from './GregorianDate.js';
  8. import isLeapYear from './isLeapYear.js';
  9. import Iso8601 from './Iso8601.js';
  10. import JulianDate from './JulianDate.js';
  11. import TimeInterval from './TimeInterval.js';
  12. function compareIntervalStartTimes(left, right) {
  13. return JulianDate.compare(left.start, right.start);
  14. }
  15. /**
  16. * A non-overlapping collection of {@link TimeInterval} instances sorted by start time.
  17. * @alias TimeIntervalCollection
  18. * @constructor
  19. *
  20. * @param {TimeInterval[]} [intervals] An array of intervals to add to the collection.
  21. */
  22. function TimeIntervalCollection(intervals) {
  23. this._intervals = [];
  24. this._changedEvent = new Event();
  25. if (defined(intervals)) {
  26. var length = intervals.length;
  27. for (var i = 0; i < length; i++) {
  28. this.addInterval(intervals[i]);
  29. }
  30. }
  31. }
  32. defineProperties(TimeIntervalCollection.prototype, {
  33. /**
  34. * Gets an event that is raised whenever the collection of intervals change.
  35. * @memberof TimeIntervalCollection.prototype
  36. * @type {Event}
  37. * @readonly
  38. */
  39. changedEvent : {
  40. get : function() {
  41. return this._changedEvent;
  42. }
  43. },
  44. /**
  45. * Gets the start time of the collection.
  46. * @memberof TimeIntervalCollection.prototype
  47. * @type {JulianDate}
  48. * @readonly
  49. */
  50. start : {
  51. get : function() {
  52. var intervals = this._intervals;
  53. return intervals.length === 0 ? undefined : intervals[0].start;
  54. }
  55. },
  56. /**
  57. * Gets whether or not the start time is included in the collection.
  58. * @memberof TimeIntervalCollection.prototype
  59. * @type {Boolean}
  60. * @readonly
  61. */
  62. isStartIncluded : {
  63. get : function() {
  64. var intervals = this._intervals;
  65. return intervals.length === 0 ? false : intervals[0].isStartIncluded;
  66. }
  67. },
  68. /**
  69. * Gets the stop time of the collection.
  70. * @memberof TimeIntervalCollection.prototype
  71. * @type {JulianDate}
  72. * @readonly
  73. */
  74. stop : {
  75. get : function() {
  76. var intervals = this._intervals;
  77. var length = intervals.length;
  78. return length === 0 ? undefined : intervals[length - 1].stop;
  79. }
  80. },
  81. /**
  82. * Gets whether or not the stop time is included in the collection.
  83. * @memberof TimeIntervalCollection.prototype
  84. * @type {Boolean}
  85. * @readonly
  86. */
  87. isStopIncluded : {
  88. get : function() {
  89. var intervals = this._intervals;
  90. var length = intervals.length;
  91. return length === 0 ? false : intervals[length - 1].isStopIncluded;
  92. }
  93. },
  94. /**
  95. * Gets the number of intervals in the collection.
  96. * @memberof TimeIntervalCollection.prototype
  97. * @type {Number}
  98. * @readonly
  99. */
  100. length : {
  101. get : function() {
  102. return this._intervals.length;
  103. }
  104. },
  105. /**
  106. * Gets whether or not the collection is empty.
  107. * @memberof TimeIntervalCollection.prototype
  108. * @type {Boolean}
  109. * @readonly
  110. */
  111. isEmpty : {
  112. get : function() {
  113. return this._intervals.length === 0;
  114. }
  115. }
  116. });
  117. /**
  118. * Compares this instance against the provided instance componentwise and returns
  119. * <code>true</code> if they are equal, <code>false</code> otherwise.
  120. *
  121. * @param {TimeIntervalCollection} [right] The right hand side collection.
  122. * @param {TimeInterval~DataComparer} [dataComparer] A function which compares the data of the two intervals. If omitted, reference equality is used.
  123. * @returns {Boolean} <code>true</code> if they are equal, <code>false</code> otherwise.
  124. */
  125. TimeIntervalCollection.prototype.equals = function(right, dataComparer) {
  126. if (this === right) {
  127. return true;
  128. }
  129. if (!(right instanceof TimeIntervalCollection)) {
  130. return false;
  131. }
  132. var intervals = this._intervals;
  133. var rightIntervals = right._intervals;
  134. var length = intervals.length;
  135. if (length !== rightIntervals.length) {
  136. return false;
  137. }
  138. for (var i = 0; i < length; i++) {
  139. if (!TimeInterval.equals(intervals[i], rightIntervals[i], dataComparer)) {
  140. return false;
  141. }
  142. }
  143. return true;
  144. };
  145. /**
  146. * Gets the interval at the specified index.
  147. *
  148. * @param {Number} index The index of the interval to retrieve.
  149. * @returns {TimeInterval} The interval at the specified index, or <code>undefined</code> if no interval exists as that index.
  150. */
  151. TimeIntervalCollection.prototype.get = function(index) {
  152. //>>includeStart('debug', pragmas.debug);
  153. if (!defined(index)) {
  154. throw new DeveloperError('index is required.');
  155. }
  156. //>>includeEnd('debug');
  157. return this._intervals[index];
  158. };
  159. /**
  160. * Removes all intervals from the collection.
  161. */
  162. TimeIntervalCollection.prototype.removeAll = function() {
  163. if (this._intervals.length > 0) {
  164. this._intervals.length = 0;
  165. this._changedEvent.raiseEvent(this);
  166. }
  167. };
  168. /**
  169. * Finds and returns the interval that contains the specified date.
  170. *
  171. * @param {JulianDate} date The date to search for.
  172. * @returns {TimeInterval|undefined} The interval containing the specified date, <code>undefined</code> if no such interval exists.
  173. */
  174. TimeIntervalCollection.prototype.findIntervalContainingDate = function(date) {
  175. var index = this.indexOf(date);
  176. return index >= 0 ? this._intervals[index] : undefined;
  177. };
  178. /**
  179. * Finds and returns the data for the interval that contains the specified date.
  180. *
  181. * @param {JulianDate} date The date to search for.
  182. * @returns {Object} The data for the interval containing the specified date, or <code>undefined</code> if no such interval exists.
  183. */
  184. TimeIntervalCollection.prototype.findDataForIntervalContainingDate = function(date) {
  185. var index = this.indexOf(date);
  186. return index >= 0 ? this._intervals[index].data : undefined;
  187. };
  188. /**
  189. * Checks if the specified date is inside this collection.
  190. *
  191. * @param {JulianDate} julianDate The date to check.
  192. * @returns {Boolean} <code>true</code> if the collection contains the specified date, <code>false</code> otherwise.
  193. */
  194. TimeIntervalCollection.prototype.contains = function(julianDate) {
  195. return this.indexOf(julianDate) >= 0;
  196. };
  197. var indexOfScratch = new TimeInterval();
  198. /**
  199. * Finds and returns the index of the interval in the collection that contains the specified date.
  200. *
  201. * @param {JulianDate} date The date to search for.
  202. * @returns {Number} The index of the interval that contains the specified date, if no such interval exists,
  203. * it returns a negative number which is the bitwise complement of the index of the next interval that
  204. * starts after the date, or if no interval starts after the specified date, the bitwise complement of
  205. * the length of the collection.
  206. */
  207. TimeIntervalCollection.prototype.indexOf = function(date) {
  208. //>>includeStart('debug', pragmas.debug);
  209. if (!defined(date)) {
  210. throw new DeveloperError('date is required');
  211. }
  212. //>>includeEnd('debug');
  213. var intervals = this._intervals;
  214. indexOfScratch.start = date;
  215. indexOfScratch.stop = date;
  216. var index = binarySearch(intervals, indexOfScratch, compareIntervalStartTimes);
  217. if (index >= 0) {
  218. if (intervals[index].isStartIncluded) {
  219. return index;
  220. }
  221. if (index > 0 && intervals[index - 1].stop.equals(date) && intervals[index - 1].isStopIncluded) {
  222. return index - 1;
  223. }
  224. return ~index;
  225. }
  226. index = ~index;
  227. if (index > 0 && (index - 1) < intervals.length && TimeInterval.contains(intervals[index - 1], date)) {
  228. return index - 1;
  229. }
  230. return ~index;
  231. };
  232. /**
  233. * Returns the first interval in the collection that matches the specified parameters.
  234. * All parameters are optional and <code>undefined</code> parameters are treated as a don't care condition.
  235. *
  236. * @param {Object} [options] Object with the following properties:
  237. * @param {JulianDate} [options.start] The start time of the interval.
  238. * @param {JulianDate} [options.stop] The stop time of the interval.
  239. * @param {Boolean} [options.isStartIncluded] <code>true</code> if <code>options.start</code> is included in the interval, <code>false</code> otherwise.
  240. * @param {Boolean} [options.isStopIncluded] <code>true</code> if <code>options.stop</code> is included in the interval, <code>false</code> otherwise.
  241. * @returns {TimeInterval} The first interval in the collection that matches the specified parameters.
  242. */
  243. TimeIntervalCollection.prototype.findInterval = function(options) {
  244. options = defaultValue(options, defaultValue.EMPTY_OBJECT);
  245. var start = options.start;
  246. var stop = options.stop;
  247. var isStartIncluded = options.isStartIncluded;
  248. var isStopIncluded = options.isStopIncluded;
  249. var intervals = this._intervals;
  250. for (var i = 0, len = intervals.length; i < len; i++) {
  251. var interval = intervals[i];
  252. if ((!defined(start) || interval.start.equals(start)) &&
  253. (!defined(stop) || interval.stop.equals(stop)) &&
  254. (!defined(isStartIncluded) || interval.isStartIncluded === isStartIncluded) &&
  255. (!defined(isStopIncluded) || interval.isStopIncluded === isStopIncluded)) {
  256. return intervals[i];
  257. }
  258. }
  259. return undefined;
  260. };
  261. /**
  262. * Adds an interval to the collection, merging intervals that contain the same data and
  263. * splitting intervals of different data as needed in order to maintain a non-overlapping collection.
  264. * The data in the new interval takes precedence over any existing intervals in the collection.
  265. *
  266. * @param {TimeInterval} interval The interval to add.
  267. * @param {TimeInterval~DataComparer} [dataComparer] A function which compares the data of the two intervals. If omitted, reference equality is used.
  268. */
  269. TimeIntervalCollection.prototype.addInterval = function(interval, dataComparer) {
  270. //>>includeStart('debug', pragmas.debug);
  271. if (!defined(interval)) {
  272. throw new DeveloperError('interval is required');
  273. }
  274. //>>includeEnd('debug');
  275. if (interval.isEmpty) {
  276. return;
  277. }
  278. var intervals = this._intervals;
  279. // Handle the common case quickly: we're adding a new interval which is after all existing intervals.
  280. if (intervals.length === 0 || JulianDate.greaterThan(interval.start, intervals[intervals.length - 1].stop)) {
  281. intervals.push(interval);
  282. this._changedEvent.raiseEvent(this);
  283. return;
  284. }
  285. // Keep the list sorted by the start date
  286. var index = binarySearch(intervals, interval, compareIntervalStartTimes);
  287. if (index < 0) {
  288. index = ~index;
  289. } else {
  290. // interval's start date exactly equals the start date of at least one interval in the collection.
  291. // It could actually equal the start date of two intervals if one of them does not actually
  292. // include the date. In that case, the binary search could have found either. We need to
  293. // look at the surrounding intervals and their IsStartIncluded properties in order to make sure
  294. // we're working with the correct interval.
  295. // eslint-disable-next-line no-lonely-if
  296. if (index > 0 &&
  297. interval.isStartIncluded &&
  298. intervals[index - 1].isStartIncluded &&
  299. intervals[index - 1].start.equals(interval.start)) {
  300. --index;
  301. } else if (index < intervals.length &&
  302. !interval.isStartIncluded &&
  303. intervals[index].isStartIncluded &&
  304. intervals[index].start.equals(interval.start)) {
  305. ++index;
  306. }
  307. }
  308. var comparison;
  309. if (index > 0) {
  310. // Not the first thing in the list, so see if the interval before this one
  311. // overlaps this one.
  312. comparison = JulianDate.compare(intervals[index - 1].stop, interval.start);
  313. if (comparison > 0 ||
  314. (comparison === 0 &&
  315. (intervals[index - 1].isStopIncluded || interval.isStartIncluded))) {
  316. // There is an overlap
  317. if (defined(dataComparer) ? dataComparer(intervals[index - 1].data, interval.data) : (intervals[index - 1].data === interval.data)) {
  318. // Overlapping intervals have the same data, so combine them
  319. if (JulianDate.greaterThan(interval.stop, intervals[index - 1].stop)) {
  320. interval = new TimeInterval({
  321. start: intervals[index - 1].start,
  322. stop: interval.stop,
  323. isStartIncluded: intervals[index - 1].isStartIncluded,
  324. isStopIncluded: interval.isStopIncluded,
  325. data: interval.data
  326. });
  327. } else {
  328. interval = new TimeInterval({
  329. start: intervals[index - 1].start,
  330. stop: intervals[index - 1].stop,
  331. isStartIncluded: intervals[index - 1].isStartIncluded,
  332. isStopIncluded: intervals[index - 1].isStopIncluded || (interval.stop.equals(intervals[index - 1].stop) && interval.isStopIncluded),
  333. data: interval.data
  334. });
  335. }
  336. intervals.splice(index - 1, 1);
  337. --index;
  338. } else {
  339. // Overlapping intervals have different data. The new interval
  340. // being added 'wins' so truncate the previous interval.
  341. // If the existing interval extends past the end of the new one,
  342. // split the existing interval into two intervals.
  343. comparison = JulianDate.compare(intervals[index - 1].stop, interval.stop);
  344. if (comparison > 0 ||
  345. (comparison === 0 && intervals[index - 1].isStopIncluded && !interval.isStopIncluded)) {
  346. intervals.splice(index, 0, new TimeInterval({
  347. start: interval.stop,
  348. stop: intervals[index - 1].stop,
  349. isStartIncluded: !interval.isStopIncluded,
  350. isStopIncluded: intervals[index - 1].isStopIncluded,
  351. data: intervals[index - 1].data
  352. }));
  353. }
  354. intervals[index - 1] = new TimeInterval({
  355. start: intervals[index - 1].start,
  356. stop: interval.start,
  357. isStartIncluded: intervals[index - 1].isStartIncluded,
  358. isStopIncluded: !interval.isStartIncluded,
  359. data: intervals[index - 1].data
  360. });
  361. }
  362. }
  363. }
  364. while (index < intervals.length) {
  365. // Not the last thing in the list, so see if the intervals after this one overlap this one.
  366. comparison = JulianDate.compare(interval.stop, intervals[index].start);
  367. if (comparison > 0 ||
  368. (comparison === 0 && (interval.isStopIncluded || intervals[index].isStartIncluded))) {
  369. // There is an overlap
  370. if (defined(dataComparer) ? dataComparer(intervals[index].data, interval.data) : intervals[index].data === interval.data) {
  371. // Overlapping intervals have the same data, so combine them
  372. interval = new TimeInterval({
  373. start: interval.start,
  374. stop: JulianDate.greaterThan(intervals[index].stop, interval.stop) ? intervals[index].stop : interval.stop,
  375. isStartIncluded: interval.isStartIncluded,
  376. isStopIncluded: JulianDate.greaterThan(intervals[index].stop, interval.stop) ? intervals[index].isStopIncluded : interval.isStopIncluded,
  377. data: interval.data
  378. });
  379. intervals.splice(index, 1);
  380. } else {
  381. // Overlapping intervals have different data. The new interval
  382. // being added 'wins' so truncate the next interval.
  383. intervals[index] = new TimeInterval({
  384. start: interval.stop,
  385. stop: intervals[index].stop,
  386. isStartIncluded: !interval.isStopIncluded,
  387. isStopIncluded: intervals[index].isStopIncluded,
  388. data: intervals[index].data
  389. });
  390. if (intervals[index].isEmpty) {
  391. intervals.splice(index, 1);
  392. } else {
  393. // Found a partial span, so it is not possible for the next
  394. // interval to be spanned at all. Stop looking.
  395. break;
  396. }
  397. }
  398. } else {
  399. // Found the last one we're spanning, so stop looking.
  400. break;
  401. }
  402. }
  403. // Add the new interval
  404. intervals.splice(index, 0, interval);
  405. this._changedEvent.raiseEvent(this);
  406. };
  407. /**
  408. * Removes the specified interval from this interval collection, creating a hole over the specified interval.
  409. * The data property of the input interval is ignored.
  410. *
  411. * @param {TimeInterval} interval The interval to remove.
  412. * @returns {Boolean} <code>true</code> if the interval was removed, <code>false</code> if no part of the interval was in the collection.
  413. */
  414. TimeIntervalCollection.prototype.removeInterval = function(interval) {
  415. //>>includeStart('debug', pragmas.debug);
  416. if (!defined(interval)) {
  417. throw new DeveloperError('interval is required');
  418. }
  419. //>>includeEnd('debug');
  420. if (interval.isEmpty) {
  421. return false;
  422. }
  423. var intervals = this._intervals;
  424. var index = binarySearch(intervals, interval, compareIntervalStartTimes);
  425. if (index < 0) {
  426. index = ~index;
  427. }
  428. var result = false;
  429. // Check for truncation of the end of the previous interval.
  430. if (index > 0 &&
  431. (JulianDate.greaterThan(intervals[index - 1].stop, interval.start) ||
  432. (intervals[index - 1].stop.equals(interval.start) && intervals[index - 1].isStopIncluded && interval.isStartIncluded))) {
  433. result = true;
  434. if (JulianDate.greaterThan(intervals[index - 1].stop, interval.stop) ||
  435. (intervals[index - 1].isStopIncluded && !interval.isStopIncluded && intervals[index - 1].stop.equals(interval.stop))) {
  436. // Break the existing interval into two pieces
  437. intervals.splice(index, 0, new TimeInterval({
  438. start: interval.stop,
  439. stop: intervals[index - 1].stop,
  440. isStartIncluded: !interval.isStopIncluded,
  441. isStopIncluded: intervals[index - 1].isStopIncluded,
  442. data: intervals[index - 1].data
  443. }));
  444. }
  445. intervals[index - 1] = new TimeInterval({
  446. start: intervals[index - 1].start,
  447. stop: interval.start,
  448. isStartIncluded: intervals[index - 1].isStartIncluded,
  449. isStopIncluded: !interval.isStartIncluded,
  450. data: intervals[index - 1].data
  451. });
  452. }
  453. // Check if the Start of the current interval should remain because interval.start is the same but
  454. // it is not included.
  455. if (index < intervals.length &&
  456. !interval.isStartIncluded &&
  457. intervals[index].isStartIncluded &&
  458. interval.start.equals(intervals[index].start)) {
  459. result = true;
  460. intervals.splice(index, 0, new TimeInterval({
  461. start: intervals[index].start,
  462. stop: intervals[index].start,
  463. isStartIncluded: true,
  464. isStopIncluded: true,
  465. data: intervals[index].data
  466. }));
  467. ++index;
  468. }
  469. // Remove any intervals that are completely overlapped by the input interval.
  470. while (index < intervals.length && JulianDate.greaterThan(interval.stop, intervals[index].stop)) {
  471. result = true;
  472. intervals.splice(index, 1);
  473. }
  474. // Check for the case where the input interval ends on the same date
  475. // as an existing interval.
  476. if (index < intervals.length && interval.stop.equals(intervals[index].stop)) {
  477. result = true;
  478. if (!interval.isStopIncluded && intervals[index].isStopIncluded) {
  479. // Last point of interval should remain because the stop date is included in
  480. // the existing interval but is not included in the input interval.
  481. if (index + 1 < intervals.length &&
  482. intervals[index + 1].start.equals(interval.stop) &&
  483. intervals[index].data === intervals[index + 1].data) {
  484. // Combine single point with the next interval
  485. intervals.splice(index, 1);
  486. intervals[index] = new TimeInterval({
  487. start: intervals[index].start,
  488. stop: intervals[index].stop,
  489. isStartIncluded: true,
  490. isStopIncluded: intervals[index].isStopIncluded,
  491. data: intervals[index].data
  492. });
  493. } else {
  494. intervals[index] = new TimeInterval({
  495. start: interval.stop,
  496. stop: interval.stop,
  497. isStartIncluded: true,
  498. isStopIncluded: true,
  499. data: intervals[index].data
  500. });
  501. }
  502. } else {
  503. // Interval is completely overlapped
  504. intervals.splice(index, 1);
  505. }
  506. }
  507. // Truncate any partially-overlapped intervals.
  508. if (index < intervals.length &&
  509. (JulianDate.greaterThan(interval.stop, intervals[index].start) ||
  510. (interval.stop.equals(intervals[index].start) && interval.isStopIncluded && intervals[index].isStartIncluded))) {
  511. result = true;
  512. intervals[index] = new TimeInterval({
  513. start: interval.stop,
  514. stop: intervals[index].stop,
  515. isStartIncluded: !interval.isStopIncluded,
  516. isStopIncluded: intervals[index].isStopIncluded,
  517. data: intervals[index].data
  518. });
  519. }
  520. if (result) {
  521. this._changedEvent.raiseEvent(this);
  522. }
  523. return result;
  524. };
  525. /**
  526. * Creates a new instance that is the intersection of this collection and the provided collection.
  527. *
  528. * @param {TimeIntervalCollection} other The collection to intersect with.
  529. * @param {TimeInterval~DataComparer} [dataComparer] A function which compares the data of the two intervals. If omitted, reference equality is used.
  530. * @param {TimeInterval~MergeCallback} [mergeCallback] A function which merges the data of the two intervals. If omitted, the data from the left interval will be used.
  531. * @returns {TimeIntervalCollection} A new TimeIntervalCollection which is the intersection of this collection and the provided collection.
  532. */
  533. TimeIntervalCollection.prototype.intersect = function(other, dataComparer, mergeCallback) {
  534. //>>includeStart('debug', pragmas.debug);
  535. if (!defined(other)) {
  536. throw new DeveloperError('other is required.');
  537. }
  538. //>>includeEnd('debug');
  539. var result = new TimeIntervalCollection();
  540. var left = 0;
  541. var right = 0;
  542. var intervals = this._intervals;
  543. var otherIntervals = other._intervals;
  544. while (left < intervals.length && right < otherIntervals.length) {
  545. var leftInterval = intervals[left];
  546. var rightInterval = otherIntervals[right];
  547. if (JulianDate.lessThan(leftInterval.stop, rightInterval.start)) {
  548. ++left;
  549. } else if (JulianDate.lessThan(rightInterval.stop, leftInterval.start)) {
  550. ++right;
  551. } else {
  552. // The following will return an intersection whose data is 'merged' if the callback is defined
  553. if (defined(mergeCallback) ||
  554. ((defined(dataComparer) && dataComparer(leftInterval.data, rightInterval.data)) ||
  555. (!defined(dataComparer) && rightInterval.data === leftInterval.data))) {
  556. var intersection = TimeInterval.intersect(leftInterval, rightInterval, new TimeInterval(), mergeCallback);
  557. if (!intersection.isEmpty) {
  558. // Since we start with an empty collection for 'result', and there are no overlapping intervals in 'this' (as a rule),
  559. // the 'intersection' will never overlap with a previous interval in 'result'. So, no need to do any additional 'merging'.
  560. result.addInterval(intersection, dataComparer);
  561. }
  562. }
  563. if (JulianDate.lessThan(leftInterval.stop, rightInterval.stop) ||
  564. (leftInterval.stop.equals(rightInterval.stop) && !leftInterval.isStopIncluded && rightInterval.isStopIncluded)) {
  565. ++left;
  566. } else {
  567. ++right;
  568. }
  569. }
  570. }
  571. return result;
  572. };
  573. /**
  574. * Creates a new instance from a JulianDate array.
  575. *
  576. * @param {Object} options Object with the following properties:
  577. * @param {JulianDate[]} options.julianDates An array of ISO 8601 dates.
  578. * @param {Boolean} [options.isStartIncluded=true] <code>true</code> if start time is included in the interval, <code>false</code> otherwise.
  579. * @param {Boolean} [options.isStopIncluded=true] <code>true</code> if stop time is included in the interval, <code>false</code> otherwise.
  580. * @param {Boolean} [options.leadingInterval=false] <code>true</code> if you want to add a interval from Iso8601.MINIMUM_VALUE to start time, <code>false</code> otherwise.
  581. * @param {Boolean} [options.trailingInterval=false] <code>true</code> if you want to add a interval from stop time to Iso8601.MAXIMUM_VALUE, <code>false</code> otherwise.
  582. * @param {Function} [options.dataCallback] A function that will be return the data that is called with each interval before it is added to the collection. If unspecified, the data will be the index in the collection.
  583. * @param {TimeIntervalCollection} [result] An existing instance to use for the result.
  584. * @returns {TimeIntervalCollection} The modified result parameter or a new instance if none was provided.
  585. */
  586. TimeIntervalCollection.fromJulianDateArray = function(options, result) {
  587. //>>includeStart('debug', pragmas.debug);
  588. if (!defined(options)) {
  589. throw new DeveloperError('options is required.');
  590. }
  591. if (!defined(options.julianDates)) {
  592. throw new DeveloperError('options.iso8601Array is required.');
  593. }
  594. //>>includeEnd('debug');
  595. if (!defined(result)) {
  596. result = new TimeIntervalCollection();
  597. }
  598. var julianDates = options.julianDates;
  599. var length = julianDates.length;
  600. var dataCallback = options.dataCallback;
  601. var isStartIncluded = defaultValue(options.isStartIncluded, true);
  602. var isStopIncluded = defaultValue(options.isStopIncluded, true);
  603. var leadingInterval = defaultValue(options.leadingInterval, false);
  604. var trailingInterval = defaultValue(options.trailingInterval, false);
  605. var interval;
  606. // Add a default interval, which will only end up being used up to first interval
  607. var startIndex = 0;
  608. if (leadingInterval) {
  609. ++startIndex;
  610. interval = new TimeInterval({
  611. start : Iso8601.MINIMUM_VALUE,
  612. stop : julianDates[0],
  613. isStartIncluded : true,
  614. isStopIncluded : !isStartIncluded
  615. });
  616. interval.data = defined(dataCallback) ? dataCallback(interval, result.length) : result.length;
  617. result.addInterval(interval);
  618. }
  619. for (var i = 0; i < length - 1; ++i) {
  620. var startDate = julianDates[i];
  621. var endDate = julianDates[i + 1];
  622. interval = new TimeInterval({
  623. start : startDate,
  624. stop : endDate,
  625. isStartIncluded : (result.length === startIndex) ? isStartIncluded : true,
  626. isStopIncluded : (i === (length - 2)) ? isStopIncluded : false
  627. });
  628. interval.data = defined(dataCallback) ? dataCallback(interval, result.length) : result.length;
  629. result.addInterval(interval);
  630. startDate = endDate;
  631. }
  632. if (trailingInterval) {
  633. interval = new TimeInterval({
  634. start : julianDates[length - 1],
  635. stop : Iso8601.MAXIMUM_VALUE,
  636. isStartIncluded : !isStopIncluded,
  637. isStopIncluded : true
  638. });
  639. interval.data = defined(dataCallback) ? dataCallback(interval, result.length) : result.length;
  640. result.addInterval(interval);
  641. }
  642. return result;
  643. };
  644. var scratchGregorianDate = new GregorianDate();
  645. var monthLengths = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  646. /**
  647. * Adds duration represented as a GregorianDate to a JulianDate
  648. *
  649. * @param {JulianDate} julianDate The date.
  650. * @param {GregorianDate} duration An duration represented as a GregorianDate.
  651. * @param {JulianDate} result An existing instance to use for the result.
  652. * @returns {JulianDate} The modified result parameter.
  653. *
  654. * @private
  655. */
  656. function addToDate(julianDate, duration, result) {
  657. if (!defined(result)) {
  658. result = new JulianDate();
  659. }
  660. JulianDate.toGregorianDate(julianDate, scratchGregorianDate);
  661. var millisecond = scratchGregorianDate.millisecond + duration.millisecond;
  662. var second = scratchGregorianDate.second + duration.second;
  663. var minute = scratchGregorianDate.minute + duration.minute;
  664. var hour = scratchGregorianDate.hour + duration.hour;
  665. var day = scratchGregorianDate.day + duration.day;
  666. var month = scratchGregorianDate.month + duration.month;
  667. var year = scratchGregorianDate.year + duration.year;
  668. if (millisecond >= 1000) {
  669. second += Math.floor(millisecond / 1000);
  670. millisecond = millisecond % 1000;
  671. }
  672. if (second >= 60) {
  673. minute += Math.floor(second / 60);
  674. second = second % 60;
  675. }
  676. if (minute >= 60) {
  677. hour += Math.floor(minute / 60);
  678. minute = minute % 60;
  679. }
  680. if (hour >= 24) {
  681. day += Math.floor(hour / 24);
  682. hour = hour % 24;
  683. }
  684. // If days is greater than the month's length we need to remove those number of days,
  685. // readjust month and year and repeat until days is less than the month's length.
  686. monthLengths[2] = isLeapYear(year) ? 29 : 28;
  687. while ((day > monthLengths[month]) || (month >= 13)) {
  688. if (day > monthLengths[month]) {
  689. day -= monthLengths[month];
  690. ++month;
  691. }
  692. if (month >= 13) {
  693. --month;
  694. year += Math.floor(month / 12);
  695. month = month % 12;
  696. ++month;
  697. }
  698. monthLengths[2] = isLeapYear(year) ? 29 : 28;
  699. }
  700. scratchGregorianDate.millisecond = millisecond;
  701. scratchGregorianDate.second = second;
  702. scratchGregorianDate.minute = minute;
  703. scratchGregorianDate.hour = hour;
  704. scratchGregorianDate.day = day;
  705. scratchGregorianDate.month = month;
  706. scratchGregorianDate.year = year;
  707. return JulianDate.fromGregorianDate(scratchGregorianDate, result);
  708. }
  709. var scratchJulianDate = new JulianDate();
  710. var durationRegex = /P(?:([\d.,]+)Y)?(?:([\d.,]+)M)?(?:([\d.,]+)W)?(?:([\d.,]+)D)?(?:T(?:([\d.,]+)H)?(?:([\d.,]+)M)?(?:([\d.,]+)S)?)?/;
  711. /**
  712. * Parses ISO8601 duration string
  713. *
  714. * @param {String} iso8601 An ISO 8601 duration.
  715. * @param {GregorianDate} result An existing instance to use for the result.
  716. * @returns {Boolean} True is parsing succeeded, false otherwise
  717. *
  718. * @private
  719. */
  720. function parseDuration(iso8601, result) {
  721. if (!defined(iso8601) || iso8601.length === 0) {
  722. return false;
  723. }
  724. // Reset object
  725. result.year = 0;
  726. result.month = 0;
  727. result.day = 0;
  728. result.hour = 0;
  729. result.minute = 0;
  730. result.second = 0;
  731. result.millisecond = 0;
  732. if (iso8601[0] === 'P') {
  733. var matches = iso8601.match(durationRegex);
  734. if (!defined(matches)) {
  735. return false;
  736. }
  737. if (defined(matches[1])) { // Years
  738. result.year = Number(matches[1].replace(',', '.'));
  739. }
  740. if (defined(matches[2])) { // Months
  741. result.month = Number(matches[2].replace(',', '.'));
  742. }
  743. if (defined(matches[3])) { // Weeks
  744. result.day = Number(matches[3].replace(',', '.')) * 7;
  745. }
  746. if (defined(matches[4])) { // Days
  747. result.day += Number(matches[4].replace(',', '.'));
  748. }
  749. if (defined(matches[5])) { // Hours
  750. result.hour = Number(matches[5].replace(',', '.'));
  751. }
  752. if (defined(matches[6])) { // Weeks
  753. result.minute = Number(matches[6].replace(',', '.'));
  754. }
  755. if (defined(matches[7])) { // Seconds
  756. var seconds = Number(matches[7].replace(',', '.'));
  757. result.second = Math.floor(seconds);
  758. result.millisecond = (seconds % 1) * 1000;
  759. }
  760. } else {
  761. // They can technically specify the duration as a normal date with some caveats. Try our best to load it.
  762. if (iso8601[iso8601.length - 1] !== 'Z') { // It's not a date, its a duration, so it always has to be UTC
  763. iso8601 += 'Z';
  764. }
  765. JulianDate.toGregorianDate(JulianDate.fromIso8601(iso8601, scratchJulianDate), result);
  766. }
  767. // A duration of 0 will cause an infinite loop, so just make sure something is non-zero
  768. return (result.year || result.month || result.day || result.hour ||
  769. result.minute || result.second || result.millisecond);
  770. }
  771. var scratchDuration = new GregorianDate();
  772. /**
  773. * Creates a new instance from an {@link http://en.wikipedia.org/wiki/ISO_8601|ISO 8601} time interval (start/end/duration).
  774. *
  775. * @param {Object} options Object with the following properties:
  776. * @param {String} options.iso8601 An ISO 8601 interval.
  777. * @param {Boolean} [options.isStartIncluded=true] <code>true</code> if start time is included in the interval, <code>false</code> otherwise.
  778. * @param {Boolean} [options.isStopIncluded=true] <code>true</code> if stop time is included in the interval, <code>false</code> otherwise.
  779. * @param {Boolean} [options.leadingInterval=false] <code>true</code> if you want to add a interval from Iso8601.MINIMUM_VALUE to start time, <code>false</code> otherwise.
  780. * @param {Boolean} [options.trailingInterval=false] <code>true</code> if you want to add a interval from stop time to Iso8601.MAXIMUM_VALUE, <code>false</code> otherwise.
  781. * @param {Function} [options.dataCallback] A function that will be return the data that is called with each interval before it is added to the collection. If unspecified, the data will be the index in the collection.
  782. * @param {TimeIntervalCollection} [result] An existing instance to use for the result.
  783. * @returns {TimeIntervalCollection} The modified result parameter or a new instance if none was provided.
  784. */
  785. TimeIntervalCollection.fromIso8601 = function(options, result) {
  786. //>>includeStart('debug', pragmas.debug);
  787. if (!defined(options)) {
  788. throw new DeveloperError('options is required.');
  789. }
  790. if (!defined(options.iso8601)) {
  791. throw new DeveloperError('options.iso8601 is required.');
  792. }
  793. //>>includeEnd('debug');
  794. var dates = options.iso8601.split('/');
  795. var start = JulianDate.fromIso8601(dates[0]);
  796. var stop = JulianDate.fromIso8601(dates[1]);
  797. var julianDates = [];
  798. if (!parseDuration(dates[2], scratchDuration)) {
  799. julianDates.push(start, stop);
  800. } else {
  801. var date = JulianDate.clone(start);
  802. julianDates.push(date);
  803. while (JulianDate.compare(date, stop) < 0) {
  804. date = addToDate(date, scratchDuration);
  805. var afterStop = (JulianDate.compare(stop, date) <= 0);
  806. if (afterStop) {
  807. JulianDate.clone(stop, date);
  808. }
  809. julianDates.push(date);
  810. }
  811. }
  812. return TimeIntervalCollection.fromJulianDateArray({
  813. julianDates : julianDates,
  814. isStartIncluded : options.isStartIncluded,
  815. isStopIncluded : options.isStopIncluded,
  816. leadingInterval : options.leadingInterval,
  817. trailingInterval : options.trailingInterval,
  818. dataCallback : options.dataCallback
  819. }, result);
  820. };
  821. /**
  822. * Creates a new instance from a {@link http://en.wikipedia.org/wiki/ISO_8601|ISO 8601} date array.
  823. *
  824. * @param {Object} options Object with the following properties:
  825. * @param {String[]} options.iso8601Dates An array of ISO 8601 dates.
  826. * @param {Boolean} [options.isStartIncluded=true] <code>true</code> if start time is included in the interval, <code>false</code> otherwise.
  827. * @param {Boolean} [options.isStopIncluded=true] <code>true</code> if stop time is included in the interval, <code>false</code> otherwise.
  828. * @param {Boolean} [options.leadingInterval=false] <code>true</code> if you want to add a interval from Iso8601.MINIMUM_VALUE to start time, <code>false</code> otherwise.
  829. * @param {Boolean} [options.trailingInterval=false] <code>true</code> if you want to add a interval from stop time to Iso8601.MAXIMUM_VALUE, <code>false</code> otherwise.
  830. * @param {Function} [options.dataCallback] A function that will be return the data that is called with each interval before it is added to the collection. If unspecified, the data will be the index in the collection.
  831. * @param {TimeIntervalCollection} [result] An existing instance to use for the result.
  832. * @returns {TimeIntervalCollection} The modified result parameter or a new instance if none was provided.
  833. */
  834. TimeIntervalCollection.fromIso8601DateArray = function(options, result) {
  835. //>>includeStart('debug', pragmas.debug);
  836. if (!defined(options)) {
  837. throw new DeveloperError('options is required.');
  838. }
  839. if (!defined(options.iso8601Dates)) {
  840. throw new DeveloperError('options.iso8601Dates is required.');
  841. }
  842. //>>includeEnd('debug');
  843. return TimeIntervalCollection.fromJulianDateArray({
  844. julianDates : options.iso8601Dates.map(function(date) {
  845. return JulianDate.fromIso8601(date);
  846. }),
  847. isStartIncluded : options.isStartIncluded,
  848. isStopIncluded : options.isStopIncluded,
  849. leadingInterval : options.leadingInterval,
  850. trailingInterval : options.trailingInterval,
  851. dataCallback : options.dataCallback
  852. }, result);
  853. };
  854. /**
  855. * Creates a new instance from a {@link http://en.wikipedia.org/wiki/ISO_8601|ISO 8601} duration array.
  856. *
  857. * @param {Object} options Object with the following properties:
  858. * @param {JulianDate} options.epoch An date that the durations are relative to.
  859. * @param {String} options.iso8601Durations An array of ISO 8601 durations.
  860. * @param {Boolean} [options.relativeToPrevious=false] <code>true</code> if durations are relative to previous date, <code>false</code> if always relative to the epoch.
  861. * @param {Boolean} [options.isStartIncluded=true] <code>true</code> if start time is included in the interval, <code>false</code> otherwise.
  862. * @param {Boolean} [options.isStopIncluded=true] <code>true</code> if stop time is included in the interval, <code>false</code> otherwise.
  863. * @param {Boolean} [options.leadingInterval=false] <code>true</code> if you want to add a interval from Iso8601.MINIMUM_VALUE to start time, <code>false</code> otherwise.
  864. * @param {Boolean} [options.trailingInterval=false] <code>true</code> if you want to add a interval from stop time to Iso8601.MAXIMUM_VALUE, <code>false</code> otherwise.
  865. * @param {Function} [options.dataCallback] A function that will be return the data that is called with each interval before it is added to the collection. If unspecified, the data will be the index in the collection.
  866. * @param {TimeIntervalCollection} [result] An existing instance to use for the result.
  867. * @returns {TimeIntervalCollection} The modified result parameter or a new instance if none was provided.
  868. */
  869. TimeIntervalCollection.fromIso8601DurationArray = function(options, result) {
  870. //>>includeStart('debug', pragmas.debug);
  871. if (!defined(options)) {
  872. throw new DeveloperError('options is required.');
  873. }
  874. if (!defined(options.epoch)) {
  875. throw new DeveloperError('options.epoch is required.');
  876. }
  877. if (!defined(options.iso8601Durations)) {
  878. throw new DeveloperError('options.iso8601Durations is required.');
  879. }
  880. //>>includeEnd('debug');
  881. var epoch = options.epoch;
  882. var iso8601Durations = options.iso8601Durations;
  883. var relativeToPrevious = defaultValue(options.relativeToPrevious, false);
  884. var julianDates = [];
  885. var date, previousDate;
  886. var length = iso8601Durations.length;
  887. for (var i = 0; i < length; ++i) {
  888. // Allow a duration of 0 on the first iteration, because then it is just the epoch
  889. if (parseDuration(iso8601Durations[i], scratchDuration) || i === 0) {
  890. if (relativeToPrevious && defined(previousDate)) {
  891. date = addToDate(previousDate, scratchDuration);
  892. } else {
  893. date = addToDate(epoch, scratchDuration);
  894. }
  895. julianDates.push(date);
  896. previousDate = date;
  897. }
  898. }
  899. return TimeIntervalCollection.fromJulianDateArray({
  900. julianDates : julianDates,
  901. isStartIncluded : options.isStartIncluded,
  902. isStopIncluded : options.isStopIncluded,
  903. leadingInterval : options.leadingInterval,
  904. trailingInterval : options.trailingInterval,
  905. dataCallback : options.dataCallback
  906. }, result);
  907. };
  908. export default TimeIntervalCollection;