KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > util > CacheSizes


1 /**
2  * $RCSfile: CacheSizes.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2004/12/26 13:27:16 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.util;
13
14 import java.util.Map JavaDoc;
15 import java.util.Collection JavaDoc;
16
17 /**
18  * Utility class for determining the sizes in bytes of commonly used objects.
19  * Classes implementing the Cacheable interface should use this class to
20  * determine their size.
21  *
22  * @author Matt Tucker
23  */

24 public class CacheSizes {
25
26     /**
27      * Returns the size in bytes of a basic Object. This method should only
28      * be used for actual Object objects and not classes that extend Object.
29      *
30      * @return the size of an Object.
31      */

32     public static int sizeOfObject() {
33         return 4;
34     }
35
36     /**
37      * Returns the size in bytes of a String.
38      *
39      * @param string the String to determine the size of.
40      * @return the size of a String.
41      */

42     public static int sizeOfString(String JavaDoc string) {
43         if (string == null) {
44             return 0;
45         }
46         return 4 + string.length() * 2;
47     }
48
49     /**
50      * Returns the size in bytes of a primitive int.
51      *
52      * @return the size of a primitive int.
53      */

54     public static int sizeOfInt() {
55         return 4;
56     }
57
58     /**
59      * Returns the size in bytes of a primitive char.
60      *
61      * @return the size of a primitive char.
62      */

63     public static int sizeOfChar() {
64         return 2;
65     }
66
67     /**
68      * Returns the size in bytes of a primitive boolean.
69      *
70      * @return the size of a primitive boolean.
71      */

72     public static int sizeOfBoolean() {
73         return 1;
74     }
75
76     /**
77      * Returns the size in bytes of a primitive long.
78      *
79      * @return the size of a primitive long.
80      */

81     public static int sizeOfLong() {
82         return 8;
83     }
84
85     /**
86      * Returns the size in bytes of a primitive double.
87      *
88      * @return the size of a primitive double.
89      */

90     public static int sizeOfDouble() {
91         return 8;
92     }
93
94     /**
95      * Returns the size in bytes of a Date.
96      *
97      * @return the size of a Date.
98      */

99     public static int sizeOfDate() {
100         return 12;
101     }
102
103     /**
104      * Returns the size in bytes of a Map object. All keys and
105      * values <b>must be Strings</b>.
106      *
107      * @param map the Map object to determine the size of.
108      * @return the size of the Map object.
109      */

110     public static int sizeOfMap(Map JavaDoc map) {
111         if (map == null) {
112             return 0;
113         }
114         // Base map object -- should be something around this size.
115
int size = 36;
116         // Add in size of each value
117
Object JavaDoc[] values = map.values().toArray();
118         for (int i = 0; i < values.length; i++) {
119             size += sizeOfString((String JavaDoc)values[i]);
120         }
121         Object JavaDoc[] keys = map.keySet().toArray();
122         // Add in each key
123
for (int i = 0; i < keys.length; i++) {
124             size += sizeOfString((String JavaDoc)keys[i]);
125         }
126         return size;
127     }
128
129     /**
130      * Returns the size in bytes of a Collection object. Elements are assumed to be
131      * <tt>String</tt>s, <tt>Long</tt>s or <tt>Cacheable</tt> objects.
132      *
133      * @param list the Collection object to determine the size of.
134      * @return the size of the Collection object.
135      */

136     public static int sizeOfCollection(Collection JavaDoc list) {
137         if (list == null) {
138             return 0;
139         }
140         // Base list object (approximate)
141
int size = 36;
142         // Add in size of each value
143
Object JavaDoc[] values = list.toArray();
144         for (int i = 0; i < values.length; i++) {
145             Object JavaDoc obj = values[i];
146             if (obj instanceof String JavaDoc) {
147                 size += sizeOfString((String JavaDoc)obj);
148             }
149             else if (obj instanceof Long JavaDoc) {
150                 size += sizeOfLong() + sizeOfObject();
151             }
152             else {
153                 size += ((Cacheable)obj).getCachedSize();
154             }
155         }
156         return size;
157     }
158 }
Popular Tags