1 /* 2 * Copyright (C) 2017 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 import java.util.Objects; 20 21 /** 22 * Used to represent how much space an instance takes up. 23 * An abstraction is introduced rather than using a long directly in order to 24 * more easily keep track of the different components of the size. For 25 * example, some instances may have associated native, code, or graphics 26 * sizes. 27 * <p> 28 * Size objects are immutable. 29 */ 30 public class Size { 31 private final long mJavaSize; 32 private final long mRegisteredNativeSize; 33 34 /** 35 * An instance of Size with 0 for all categories. 36 */ 37 public static Size ZERO = new Size(0, 0); 38 39 /** 40 * Constructs a new instance of Size. 41 * 42 * @param javaSize number of bytes in the java category 43 * @param registeredNativeSize number of bytes in the registeredNativeSize 44 * category 45 */ Size(long javaSize, long registeredNativeSize)46 public Size(long javaSize, long registeredNativeSize) { 47 mJavaSize = javaSize; 48 mRegisteredNativeSize = registeredNativeSize; 49 } 50 51 /** 52 * Returns the sum of the size of all categories. 53 * 54 * @return the total size 55 */ getSize()56 public long getSize() { 57 return mJavaSize + mRegisteredNativeSize; 58 } 59 60 /** 61 * Returns the size of the java category. 62 * 63 * @return the java category size 64 */ getJavaSize()65 public long getJavaSize() { 66 return mJavaSize; 67 } 68 69 /** 70 * Returns the size of the registered native category. 71 * 72 * @return the registered native category size 73 */ getRegisteredNativeSize()74 public long getRegisteredNativeSize() { 75 return mRegisteredNativeSize; 76 } 77 78 /** 79 * Returns true if all categories of this size are zero. 80 * 81 * @return true if the size is zero 82 */ isZero()83 public boolean isZero() { 84 return mJavaSize == 0 && mRegisteredNativeSize == 0; 85 } 86 87 /** 88 * Returns a new Size object that is the sum of this size and the other. 89 * 90 * @param other the size to sum with this size 91 * @return the new size object 92 */ plus(Size other)93 public Size plus(Size other) { 94 if (isZero()) { 95 return other; 96 } else if (other.isZero()) { 97 return this; 98 } else { 99 return new Size(mJavaSize + other.mJavaSize, 100 mRegisteredNativeSize + other.mRegisteredNativeSize); 101 } 102 } 103 104 /** 105 * Returns a new Size object that has <code>size</code> more registered 106 * native size than this Size object. 107 * 108 * @param size the size to add to the registered native category 109 * @return the new size object 110 */ plusRegisteredNativeSize(long size)111 public Size plusRegisteredNativeSize(long size) { 112 return new Size(mJavaSize, mRegisteredNativeSize + size); 113 } 114 115 @Override hashCode()116 public int hashCode() { 117 return Objects.hash(mJavaSize, mRegisteredNativeSize); 118 } 119 equals(Object other)120 @Override public boolean equals(Object other) { 121 if (other instanceof Size) { 122 Size s = (Size)other; 123 return mJavaSize == s.mJavaSize && mRegisteredNativeSize == s.mRegisteredNativeSize; 124 } 125 return false; 126 } 127 } 128 129