1 /* 2 * Copyright (C) 2016 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.ahat.heapdump; 18 19 /** 20 * Value represents a field value in a heap dump. The field value is either a 21 * subclass of AhatInstance or a primitive Java type. 22 */ 23 public class Value { 24 private Object mObject; 25 26 /** 27 * Constructs a value from a generic Java Object. 28 * The Object must either be a boxed Java primitive type or a subclass of 29 * AhatInstance. The object must not be null. 30 */ Value(Object object)31 Value(Object object) { 32 // TODO: Check that the Object is either an AhatSnapshot or boxed Java 33 // primitive type? 34 assert object != null; 35 mObject = object; 36 } 37 38 /** 39 * Returns true if the Value is an AhatInstance, as opposed to a Java 40 * primitive value. 41 */ isAhatInstance()42 public boolean isAhatInstance() { 43 return mObject instanceof AhatInstance; 44 } 45 46 /** 47 * Return the Value as an AhatInstance if it is one. 48 * Returns null if the Value represents a Java primitive value. 49 */ asAhatInstance()50 public AhatInstance asAhatInstance() { 51 if (isAhatInstance()) { 52 return (AhatInstance)mObject; 53 } 54 return null; 55 } 56 57 /** 58 * Returns true if the Value is an Integer. 59 */ isInteger()60 public boolean isInteger() { 61 return mObject instanceof Integer; 62 } 63 64 /** 65 * Return the Value as an Integer if it is one. 66 * Returns null if the Value does not represent an Integer. 67 */ asInteger()68 public Integer asInteger() { 69 if (isInteger()) { 70 return (Integer)mObject; 71 } 72 return null; 73 } 74 75 /** 76 * Returns true if the Value is an Long. 77 */ isLong()78 public boolean isLong() { 79 return mObject instanceof Long; 80 } 81 82 /** 83 * Return the Value as an Long if it is one. 84 * Returns null if the Value does not represent an Long. 85 */ asLong()86 public Long asLong() { 87 if (isLong()) { 88 return (Long)mObject; 89 } 90 return null; 91 } 92 93 /** 94 * Return the Value as a Byte if it is one. 95 * Returns null if the Value does not represent a Byte. 96 */ asByte()97 public Byte asByte() { 98 if (mObject instanceof Byte) { 99 return (Byte)mObject; 100 } 101 return null; 102 } 103 104 /** 105 * Return the Value as a Char if it is one. 106 * Returns null if the Value does not represent a Char. 107 */ asChar()108 public Character asChar() { 109 if (mObject instanceof Character) { 110 return (Character)mObject; 111 } 112 return null; 113 } 114 toString()115 public String toString() { 116 return mObject.toString(); 117 } 118 getBaseline(Value value)119 public static Value getBaseline(Value value) { 120 if (value == null || !value.isAhatInstance()) { 121 return value; 122 } 123 return new Value(value.asAhatInstance().getBaseline()); 124 } 125 equals(Object other)126 @Override public boolean equals(Object other) { 127 if (other instanceof Value) { 128 Value value = (Value)other; 129 return mObject.equals(value.mObject); 130 } 131 return false; 132 } 133 } 134