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 {Operation} from 'trace/tree_node/operations/operation';
18import {OperationChain} from 'trace/tree_node/operations/operation_chain';
19import {TreeNode} from 'trace/tree_node/tree_node';
20
21export class UiTreeFormatter<T extends TreeNode> {
22  private uiTree: T | undefined;
23  private operations = OperationChain.emptyChain<T>();
24
25  setUiTree(value: T): this {
26    this.uiTree = value;
27    return this;
28  }
29
30  addOperation(operation: Operation<T>): this {
31    this.operations.push(operation);
32    return this;
33  }
34
35  format(): T {
36    if (this.uiTree === undefined) {
37      throw Error('tree not set');
38    }
39
40    return this.operations.apply(this.uiTree);
41  }
42}
43