• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 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 
17 package com.android.ddmlib.log;
18 
19 import com.android.ddmlib.log.EventContainer.EventValueType;
20 
21 
22 /**
23  * Describes an {@link EventContainer} value.
24  * <p/>
25  * This is a stand-alone object, not linked to a particular Event. It describes the value, by
26  * name, type ({@link EventValueType}), and (if needed) value unit ({@link ValueType}).
27  * <p/>
28  * The index of the value is not contained within this class, and is instead dependent on the
29  * index of this particular object in the array of {@link EventValueDescription} returned by
30  * {@link EventLogParser#getEventInfoMap()} when queried for a particular event tag.
31  *
32  */
33 public final class EventValueDescription {
34 
35     /**
36      * Represents the type of a numerical value. This is used to display values of vastly different
37      * type/range in graphs.
38      */
39     public static enum ValueType {
40         NOT_APPLICABLE(0),
41         OBJECTS(1),
42         BYTES(2),
43         MILLISECONDS(3),
44         ALLOCATIONS(4),
45         ID(5),
46         PERCENT(6);
47 
48         private int mValue;
49 
50         /**
51          * Checks that the {@link EventValueType} is compatible with the {@link ValueType}.
52          * @param type the {@link EventValueType} to check.
53          * @throws InvalidValueTypeException if the types are not compatible.
54          */
checkType(EventValueType type)55         public void checkType(EventValueType type) throws InvalidValueTypeException {
56             if ((type != EventValueType.INT && type != EventValueType.LONG)
57                     && this != NOT_APPLICABLE) {
58                 throw new InvalidValueTypeException(
59                         String.format("%1$s doesn't support type %2$s", type, this));
60             }
61         }
62 
63         /**
64          * Returns a {@link ValueType} from an integer value, or <code>null</code> if no match
65          * were found.
66          * @param value the integer value.
67          */
getValueType(int value)68         public static ValueType getValueType(int value) {
69             for (ValueType type : values()) {
70                 if (type.mValue == value) {
71                     return type;
72                 }
73             }
74             return null;
75         }
76 
77         /**
78          * Returns the integer value of the enum.
79          */
getValue()80         public int getValue() {
81             return mValue;
82         }
83 
84         @Override
toString()85         public String toString() {
86             return super.toString().toLowerCase();
87         }
88 
ValueType(int value)89         private ValueType(int value) {
90             mValue = value;
91         }
92     }
93 
94     private String mName;
95     private EventValueType mEventValueType;
96     private ValueType mValueType;
97 
98     /**
99      * Builds a {@link EventValueDescription} with a name and a type.
100      * <p/>
101      * If the type is {@link EventValueType#INT} or {@link EventValueType#LONG}, the
102      * {@link #mValueType} is set to {@link ValueType#BYTES} by default. It set to
103      * {@link ValueType#NOT_APPLICABLE} for all other {@link EventValueType} values.
104      * @param name
105      * @param type
106      */
EventValueDescription(String name, EventValueType type)107     EventValueDescription(String name, EventValueType type) {
108         mName = name;
109         mEventValueType = type;
110         if (mEventValueType == EventValueType.INT || mEventValueType == EventValueType.LONG) {
111             mValueType = ValueType.BYTES;
112         } else {
113             mValueType = ValueType.NOT_APPLICABLE;
114         }
115     }
116 
117     /**
118      * Builds a {@link EventValueDescription} with a name and a type, and a {@link ValueType}.
119      * <p/>
120      * @param name
121      * @param type
122      * @param valueType
123      * @throws InvalidValueTypeException if type and valuetype are not compatible.
124      *
125      */
EventValueDescription(String name, EventValueType type, ValueType valueType)126     EventValueDescription(String name, EventValueType type, ValueType valueType)
127             throws InvalidValueTypeException {
128         mName = name;
129         mEventValueType = type;
130         mValueType = valueType;
131         mValueType.checkType(mEventValueType);
132     }
133 
134     /**
135      * @return the Name.
136      */
getName()137     public String getName() {
138         return mName;
139     }
140 
141     /**
142      * @return the {@link EventValueType}.
143      */
getEventValueType()144     public EventValueType getEventValueType() {
145         return mEventValueType;
146     }
147 
148     /**
149      * @return the {@link ValueType}.
150      */
getValueType()151     public ValueType getValueType() {
152         return mValueType;
153     }
154 
155     @Override
toString()156     public String toString() {
157         if (mValueType != ValueType.NOT_APPLICABLE) {
158             return String.format("%1$s (%2$s, %3$s)", mName, mEventValueType.toString(),
159                     mValueType.toString());
160         }
161 
162         return String.format("%1$s (%2$s)", mName, mEventValueType.toString());
163     }
164 
165     /**
166      * Checks if the value is of the proper type for this receiver.
167      * @param value the value to check.
168      * @return true if the value is of the proper type for this receiver.
169      */
checkForType(Object value)170     public boolean checkForType(Object value) {
171         switch (mEventValueType) {
172             case INT:
173                 return value instanceof Integer;
174             case LONG:
175                 return value instanceof Long;
176             case STRING:
177                 return value instanceof String;
178             case LIST:
179                 return value instanceof Object[];
180         }
181 
182         return false;
183     }
184 
185     /**
186      * Returns an object of a valid type (based on the value returned by
187      * {@link #getEventValueType()}) from a String value.
188      * <p/>
189      * IMPORTANT {@link EventValueType#LIST} and {@link EventValueType#TREE} are not
190      * supported.
191      * @param value the value of the object expressed as a string.
192      * @return an object or null if the conversion could not be done.
193      */
getObjectFromString(String value)194     public Object getObjectFromString(String value) {
195         switch (mEventValueType) {
196             case INT:
197                 try {
198                     return Integer.valueOf(value);
199                 } catch (NumberFormatException e) {
200                     return null;
201                 }
202             case LONG:
203                 try {
204                     return Long.valueOf(value);
205                 } catch (NumberFormatException e) {
206                     return null;
207                 }
208             case STRING:
209                 return value;
210         }
211 
212         return null;
213     }
214 }
215