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