1/*
2 * Copyright (C) 2024 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 {TraceRect} from 'trace/trace_rect';
18import {PropertiesProvider} from 'trace/tree_node/properties_provider';
19import {PropertyTreeNode} from './property_tree_node';
20import {TreeNode} from './tree_node';
21
22export class HierarchyTreeNode extends TreeNode {
23  private rects: TraceRect[] | undefined;
24  private zParent: HierarchyTreeNode | undefined;
25  private parent: this | undefined;
26
27  constructor(
28    id: string,
29    name: string,
30    protected readonly propertiesProvider: PropertiesProvider,
31  ) {
32    super(id, name);
33  }
34
35  async getAllProperties(): Promise<PropertyTreeNode> {
36    return await this.propertiesProvider.getAll();
37  }
38
39  getEagerPropertyByName(name: string): PropertyTreeNode | undefined {
40    return this.propertiesProvider.getEagerProperties().getChildByName(name);
41  }
42
43  addEagerProperty(property: PropertyTreeNode): void {
44    this.propertiesProvider.addEagerProperty(property);
45  }
46
47  setRects(value: TraceRect[]) {
48    this.rects = value;
49  }
50
51  getRects(): TraceRect[] | undefined {
52    return this.rects;
53  }
54
55  setZParent(parent: HierarchyTreeNode): void {
56    this.zParent = parent;
57  }
58
59  getZParent(): HierarchyTreeNode | undefined {
60    return this.zParent ?? this.parent;
61  }
62
63  setParent(parent: this): void {
64    this.parent = parent;
65  }
66
67  getParent(): this | undefined {
68    return this.parent;
69  }
70
71  override isRoot(): boolean {
72    return !this.parent;
73  }
74
75  findAncestor(targetNodeFilter: (node: this) => boolean): this | undefined {
76    let ancestor = this.getParent();
77
78    while (ancestor && !targetNodeFilter(ancestor)) {
79      ancestor = ancestor.getParent();
80    }
81
82    return ancestor;
83  }
84}
85