1/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {Timestamp} from 'common/time';
18import {AbsoluteFrameIndex} from './index_types';
19import {TraceEntry} from './trace';
20
21export class TracePosition {
22  static fromTimestamp(timestamp: Timestamp): TracePosition {
23    return new TracePosition(timestamp);
24  }
25
26  static fromTraceEntry(
27    entry: TraceEntry<{}>,
28    explicitTimestamp?: Timestamp,
29  ): TracePosition {
30    let frame: AbsoluteFrameIndex | undefined;
31    if (entry.getFullTrace().hasFrameInfo()) {
32      const frames = entry.getFramesRange();
33      frame = frames && frames.start < frames.end ? frames.start : undefined;
34    }
35    const timestamp = explicitTimestamp
36      ? explicitTimestamp
37      : entry.getTimestamp();
38    return new TracePosition(timestamp, frame, entry);
39  }
40
41  isEqual(other: TracePosition): boolean {
42    return (
43      this.timestamp.getValueNs() === other.timestamp.getValueNs() &&
44      this.frame === other.frame &&
45      this.entry?.getFullTrace().type === other.entry?.getFullTrace().type &&
46      this.entry?.getIndex() === other.entry?.getIndex()
47    );
48  }
49
50  private constructor(
51    readonly timestamp: Timestamp,
52    readonly frame?: AbsoluteFrameIndex,
53    readonly entry?: TraceEntry<{}>,
54  ) {}
55}
56