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