• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 package com.android.loganalysis.item;
17 
18 
19 import java.util.Arrays;
20 import java.util.HashSet;
21 import java.util.Map;
22 import java.util.Set;
23 
24 /**
25  * An {@link IItem} used to store DVM Lock allocation Info
26  */
27 public class DvmLockSampleItem extends GenericItem {
28 
29     public static final String PROCESS_NAME = "PROCESS_NAME";
30     public static final String SENSITIVITY_FLAG = "SENSITIVITY_FLAG";
31     public static final String WAITING_THREAD_NAME = "WAITING_THREAD_NAME";
32     public static final String WAIT_TIME = "WAIT_TIME";
33     public static final String WAITING_SOURCE_FILE = "WAITING_SOURCE_FILE";
34     public static final String WAITING_SOURCE_LINE = "WAITING_SOURCE_LINE";
35     public static final String OWNER_FILE_NAME = "OWNER_FILE_NAME";
36     public static final String OWNER_ACQUIRE_SOURCE_LINE = "OWNER_ACQUIRE_SOURCE_LINE";
37     public static final String SAMPLE_PERCENTAGE = "SAMPLE_PERCENTAGE";
38 
39     private static final Set<String> ATTRIBUTES = new HashSet<String>(Arrays.asList(
40             PROCESS_NAME, SENSITIVITY_FLAG, WAITING_THREAD_NAME, WAIT_TIME,
41             WAITING_SOURCE_FILE, WAITING_SOURCE_LINE, OWNER_FILE_NAME,
42             OWNER_ACQUIRE_SOURCE_LINE, SAMPLE_PERCENTAGE));
43 
44     @SuppressWarnings("serial")
45     private static final Map<String, Class<?>> TYPES = Map.of(
46         PROCESS_NAME, String.class,
47         SENSITIVITY_FLAG, Boolean.class,
48         WAITING_THREAD_NAME, String.class,
49         WAIT_TIME, Integer.class,
50         WAITING_SOURCE_FILE, String.class,
51         WAITING_SOURCE_LINE, Integer.class,
52         OWNER_FILE_NAME, String.class,
53         OWNER_ACQUIRE_SOURCE_LINE, Integer.class,
54         SAMPLE_PERCENTAGE, Integer.class);
55 
DvmLockSampleItem()56     public DvmLockSampleItem() {
57         super(ATTRIBUTES);
58     }
59 
60     /** {@inheritDoc} */
61     @Override
setAttribute(String attribute, Object value)62     public void setAttribute(String attribute, Object value) throws IllegalArgumentException {
63         if(ATTRIBUTES.contains(attribute)) {
64             if (TYPES.get(attribute).isAssignableFrom(value.getClass())) {
65                 super.setAttribute(attribute, value);
66             } else {
67                 throw new IllegalArgumentException(
68                         "Invalid attribute type for " + attribute +
69                                 ": found " + value.getClass().getCanonicalName() +
70                                 " expected " + TYPES.get(attribute).getCanonicalName());
71             }
72         } else {
73             throw new IllegalArgumentException("Invalid attribute key: " + attribute);
74         }
75     }
76 
77     /** {@inheritDoc} */
78     @Override
getAttribute(String attribute)79     public Object getAttribute(String attribute) throws IllegalArgumentException {
80         return super.getAttribute(attribute);
81     }
82 }
83