1/* 2 * Copyright (C) 2022 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 {FrameMap} from 'trace/frame_map'; 18import {Timestamp} from 'trace/timestamp'; 19import {Traces} from 'trace/traces'; 20import {TraceType} from 'trace/trace_type'; 21import {TraceBuilder} from './trace_builder'; 22 23export class TracesBuilder { 24 private readonly traceBuilders = new Map<TraceType, TraceBuilder<{}>>(); 25 26 setEntries(type: TraceType, entries: Array<{}>): TracesBuilder { 27 const builder = this.getOrCreateTraceBuilder(type); 28 builder.setEntries(entries); 29 return this; 30 } 31 32 setTimestamps(type: TraceType, timestamps: Timestamp[]): TracesBuilder { 33 const builder = this.getOrCreateTraceBuilder(type); 34 builder.setTimestamps(timestamps); 35 return this; 36 } 37 38 setFrameMap(type: TraceType, frameMap: FrameMap | undefined): TracesBuilder { 39 const builder = this.getOrCreateTraceBuilder(type); 40 builder.setFrameMap(frameMap); 41 return this; 42 } 43 44 build(): Traces { 45 const traces = new Traces(); 46 this.traceBuilders.forEach((builder, type) => { 47 traces.setTrace(type, builder.build()); 48 }); 49 return traces; 50 } 51 52 private getOrCreateTraceBuilder(type: TraceType): TraceBuilder<{}> { 53 let builder = this.traceBuilders.get(type); 54 if (!builder) { 55 builder = new TraceBuilder<{}>(); 56 builder.setType(type); 57 this.traceBuilders.set(type, builder); 58 } 59 return builder; 60 } 61} 62