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 {HierarchyTreeBuilder} from 'parsers/hierarchy_tree_builder';
18import {HierarchyTreeNode} from 'trace/tree_node/hierarchy_tree_node';
19import {PropertiesProvider} from 'trace/tree_node/properties_provider';
20
21export class HierarchyTreeBuilderInputMethod extends HierarchyTreeBuilder {
22  private childIdentifier = 'child';
23
24  protected override buildIdentifierToChildrenMap(
25    children: PropertiesProvider[],
26  ): Map<string, readonly HierarchyTreeNode[]> {
27    const map = children.reduce((map, child) => {
28      const childProperties = child.getEagerProperties();
29      const childNode = this.makeNode(
30        childProperties.id,
31        childProperties.name,
32        child,
33      );
34      map.set(this.childIdentifier, [childNode]);
35      return map;
36    }, new Map<string, HierarchyTreeNode[]>());
37    return map;
38  }
39
40  protected override assignParentChildRelationships(
41    node: HierarchyTreeNode,
42    identifierToChildren: Map<string | number, HierarchyTreeNode[]>,
43    isRoot?: boolean,
44  ): void {
45    // only ever one child
46    const child: HierarchyTreeNode | undefined = identifierToChildren
47      .get(this.childIdentifier)
48      ?.at(0);
49    if (child) {
50      this.setParentChildRelationship(node, child);
51    }
52  }
53}
54