1/*
2 * Copyright (C) 2015 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
17package com.android.ahat;
18
19import com.android.tools.perflib.heap.Heap;
20import com.android.tools.perflib.heap.Instance;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.Collections;
24import java.util.List;
25
26/**
27 * Class for rendering a list of instances dominated by a single instance in a
28 * pretty way.
29 */
30class DominatedList {
31  /**
32   * Render a table to the given HtmlWriter showing a pretty list of
33   * instances.
34   *
35   * @param snapshot  the snapshot where the instances reside
36   * @param doc       the document to render the dominated list to
37   * @param query     the current page query
38   * @param id        a unique identifier to use for the dominated list in the current page
39   * @param instances the collection of instances to generate a list for
40   */
41  public static void render(final AhatSnapshot snapshot,
42      Doc doc, Query query, String id, Collection<Instance> instances) {
43    List<Instance> insts = new ArrayList<Instance>(instances);
44    Collections.sort(insts, Sort.defaultInstanceCompare(snapshot));
45    HeapTable.render(doc, query, id, new TableConfig(snapshot), snapshot, insts);
46  }
47
48  private static class TableConfig implements HeapTable.TableConfig<Instance> {
49    AhatSnapshot mSnapshot;
50
51    public TableConfig(AhatSnapshot snapshot) {
52      mSnapshot = snapshot;
53    }
54
55    @Override
56    public String getHeapsDescription() {
57      return "Bytes Retained by Heap";
58    }
59
60    @Override
61    public long getSize(Instance element, Heap heap) {
62      int index = mSnapshot.getHeapIndex(heap);
63      return element.getRetainedSize(index);
64    }
65
66    @Override
67    public List<HeapTable.ValueConfig<Instance>> getValueConfigs() {
68      HeapTable.ValueConfig<Instance> value = new HeapTable.ValueConfig<Instance>() {
69        public String getDescription() {
70          return "Object";
71        }
72
73        public DocString render(Instance element) {
74          return Value.render(mSnapshot, element);
75        }
76      };
77      return Collections.singletonList(value);
78    }
79  }
80}
81