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 {PropertyTreeNode} from 'trace/tree_node/property_tree_node';
18import {PropertyTreeNodeFactory} from './property_tree_node_factory';
19
20export class PropertyTreeBuilderFromProto {
21  private denylistProperties: string[] = [];
22  private duplicateCount = 0;
23  private proto: any | undefined;
24  private rootId: string | number = 'UnknownRootId';
25  private rootName: string | undefined = 'UnknownRootName';
26  private visitProtoType = true;
27
28  setData(value: any): this {
29    this.proto = value;
30    return this;
31  }
32
33  setRootId(value: string | number): this {
34    this.rootId = value;
35    return this;
36  }
37
38  setRootName(value: string): this {
39    this.rootName = value;
40    return this;
41  }
42
43  setDenyList(value: string[]): this {
44    this.denylistProperties = value;
45    return this;
46  }
47
48  setDuplicateCount(value: number): this {
49    this.duplicateCount = value;
50    return this;
51  }
52
53  setVisitPrototype(value: boolean): this {
54    this.visitProtoType = value;
55    return this;
56  }
57
58  build(): PropertyTreeNode {
59    if (this.proto === undefined) {
60      throw Error('proto not set');
61    }
62    if (this.rootId === undefined) {
63      throw Error('rootId not set');
64    }
65    if (this.rootName === undefined) {
66      throw Error('rootName not set');
67    }
68    const factory = new PropertyTreeNodeFactory(
69      this.denylistProperties,
70      this.visitProtoType,
71    );
72
73    return factory.makeProtoProperty(this.makeNodeId(), '', this.proto);
74  }
75
76  private makeNodeId(): string {
77    let nodeId = `${this.rootId} ${this.rootName}`;
78    if (this.duplicateCount > 0) {
79      nodeId += ` duplicate(${this.duplicateCount})`;
80    }
81    return nodeId;
82  }
83}
84