GeneralHprofDumpHandler.java revision 554d7ee0f5d177b6c0bce805f5a5917b6b211978
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.preload.classdataretrieval.hprof;
18
19import com.android.ddmlib.Client;
20import com.android.ddmlib.ClientData.IHprofDumpHandler;
21
22import java.util.ArrayList;
23import java.util.List;
24
25public class GeneralHprofDumpHandler implements IHprofDumpHandler {
26
27    private List<IHprofDumpHandler> handlers = new ArrayList<>();
28
29    public void addHandler(IHprofDumpHandler h) {
30      synchronized (handlers) {
31        handlers.add(h);
32      }
33    }
34
35    public void removeHandler(IHprofDumpHandler h) {
36      synchronized (handlers) {
37        handlers.remove(h);
38      }
39    }
40
41    private List<IHprofDumpHandler> getIterationList() {
42      synchronized (handlers) {
43        return new ArrayList<>(handlers);
44      }
45    }
46
47    @Override
48    public void onEndFailure(Client arg0, String arg1) {
49      List<IHprofDumpHandler> iterList = getIterationList();
50      for (IHprofDumpHandler h : iterList) {
51        h.onEndFailure(arg0, arg1);
52      }
53    }
54
55    @Override
56    public void onSuccess(String arg0, Client arg1) {
57      List<IHprofDumpHandler> iterList = getIterationList();
58      for (IHprofDumpHandler h : iterList) {
59        h.onSuccess(arg0, arg1);
60      }
61    }
62
63    @Override
64    public void onSuccess(byte[] arg0, Client arg1) {
65      List<IHprofDumpHandler> iterList = getIterationList();
66      for (IHprofDumpHandler h : iterList) {
67        h.onSuccess(arg0, arg1);
68      }
69    }
70  }