KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > Yasna > util > CacheSizes


1 /**
2  * $RCSfile: CacheSizes.java,v $
3  * $Revision: 1.3 $
4  * $Date: 2005/09/26 18:25:02 $
5  *
6  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
7  *
8  * ===================================================================
9  * The Apache Software License, Version 1.1
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright
16  * notice, this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in
20  * the documentation and/or other materials provided with the
21  * distribution.
22  *
23  * 3. The end-user documentation included with the redistribution,
24  * if any, must include the following acknowledgment:
25  * "This product includes software developed by
26  * CoolServlets.com (http://www.Yasna.com)."
27  * Alternately, this acknowledgment may appear in the software itself,
28  * if and wherever such third-party acknowledgments normally appear.
29  *
30  * 4. The names "Jive" and "CoolServlets.com" must not be used to
31  * endorse or promote products derived from this software without
32  * prior written permission. For written permission, please
33  * contact webmaster@Yasna.com.
34  *
35  * 5. Products derived from this software may not be called "Jive",
36  * nor may "Jive" appear in their name, without prior written
37  * permission of CoolServlets.com.
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This software consists of voluntary contributions made by many
54  * individuals on behalf of CoolServlets.com. For more information
55  * on CoolServlets.com, please see <http://www.Yasna.com>.
56  */

57
58 package com.Yasna.util;
59
60 import java.util.*;
61
62 /**
63  * Utility class for determining the sizes in bytes of commonly used objects.
64  * Classes implementing the Cacheable interface should use this class to
65  * determine their size.
66  */

67 public class CacheSizes {
68
69     /**
70      * Returns the size in bytes of a basic Object. This method should only
71      * be used for actual Object objects and not classes that extend Object.
72      *
73      * @return the size of an Object.
74      */

75     public static int sizeOfObject() {
76         return 4;
77     }
78
79     /**
80      * Returns the size in bytes of a String.
81      *
82      * @param string the String to determine the size of.
83      * @return the size of a String.
84      */

85     public static int sizeOfString(String JavaDoc string) {
86         if (string == null) {
87             return 0;
88         }
89         return 4 + string.length()*2;
90     }
91
92     /**
93      * Returns the size in bytes of a primitive int.
94      *
95      * @return the size of a primitive int.
96      */

97     public static int sizeOfInt() {
98         return 4;
99     }
100
101     /**
102      * Returns the size in bytes of a primitive char.
103      *
104      * @return the size of a primitive char.
105      */

106     public static int sizeOfChar() {
107         return 2;
108     }
109
110     /**
111      * Returns the size in bytes of a primitive boolean.
112      *
113      * @return the size of a primitive boolean.
114      */

115     public static int sizeOfBoolean() {
116         return 1;
117     }
118
119     /**
120      * Returns the size in bytes of a primitive long.
121      *
122      * @return the size of a primitive long.
123      */

124     public static int sizeOfLong() {
125         return 8;
126     }
127
128     /**
129      * Returns the size in bytes of a primitive double.
130      *
131      * @return the size of a primitive double.
132      */

133     public static int sizeOfDouble() {
134         return 8;
135     }
136
137     /**
138      * Returns the size in bytes of a Date.
139      *
140      * @return the size of a Date.
141      */

142     public static int sizeOfDate() {
143         return 12;
144     }
145
146     /**
147      * Returns the size in bytes of a Properties object. All properties and
148      * property names must be Strings.
149      *
150      * @param properties the Properties object to determine the size of.
151      * @return the size of a Properties object.
152      */

153     public static int sizeOfProperties(Properties properties) {
154         if (properties == null) {
155             return 0;
156         }
157         //Base properties object
158
int size = 36;
159         //Add in size of each property
160
Enumeration enume = properties.elements();
161         while(enume.hasMoreElements()) {
162             String JavaDoc prop = (String JavaDoc)enume.nextElement();
163             size += sizeOfString(prop);
164         }
165         //Add in property names
166
enume = properties.propertyNames();
167         while(enume.hasMoreElements()) {
168             String JavaDoc prop = (String JavaDoc)enume.nextElement();
169             size += sizeOfString(prop);
170         }
171         return size;
172     }
173
174     /**
175      * Returns the size in bytes of a Map object. All keys and
176      * values <b>must be Strings</b>.
177      *
178      * @param map the Map object to determine the size of.
179      * @return the size of the Map object.
180      */

181     public static int sizeOfMap(Map map) {
182         if (map == null) {
183             return 0;
184         }
185         //Base map object -- should be something around this size.
186
int size = 36;
187         //Add in size of each value
188
Iterator iter = map.values().iterator();
189         while(iter.hasNext()) {
190             String JavaDoc value = (String JavaDoc)iter.next();
191             size += sizeOfString(value);
192         }
193         //Add in each key
194
iter = map.keySet().iterator();
195         while(iter.hasNext()) {
196             String JavaDoc key = (String JavaDoc)iter.next();
197             size += sizeOfString(key);
198         }
199         return size;
200     }
201 }
202
Popular Tags