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