KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > util > cache > CacheSizes


1 /*
2  * NEMESIS-FORUM.
3  * Copyright (C) 2002 David Laurent(lithium2@free.fr). All rights reserved.
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights reserved.
6  *
7  * Copyright (C) 2001 Yasna.com. All rights reserved.
8  *
9  * Copyright (C) 2000 CoolServlets.com. All rights reserved.
10  *
11  * NEMESIS-FORUM. is free software; you can redistribute it and/or
12  * modify it under the terms of the Apache Software License, Version 1.1,
13  * or (at your option) any later version.
14  *
15  * NEMESIS-FORUM core framework, NEMESIS-FORUM backoffice, NEMESIS-FORUM frontoffice
16  * application are parts of NEMESIS-FORUM and are distributed under
17  * same terms of licence.
18  *
19  *
20  * NEMESIS-FORUM includes software developed by the Apache Software Foundation (http://www.apache.org/)
21  * and software developed by CoolServlets.com (http://www.coolservlets.com).
22  * and software developed by Yasna.com (http://www.yasna.com).
23  *
24  */

25
26
27 package org.nemesis.forum.util.cache;
28
29 import java.util.Enumeration JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Properties JavaDoc;
33
34 /**
35  * Utility class for determining the sizes in bytes of commonly used objects.
36  * Classes implementing the Cacheable interface should use this class to
37  * determine their size.
38  */

39 public class CacheSizes {
40
41     /**
42      * Returns the size in bytes of a basic Object. This method should only
43      * be used for actual Object objects and not classes that extend Object.
44      *
45      * @return the size of an Object.
46      */

47     public static int sizeOfObject() {
48         return 4;
49     }
50
51     /**
52      * Returns the size in bytes of a String.
53      *
54      * @param string the String to determine the size of.
55      * @return the size of a String.
56      */

57     public static int sizeOfString(String JavaDoc string) {
58         if (string == null) {
59             return 0;
60         }
61         return 4 + string.length()*2;
62     }
63
64     /**
65      * Returns the size in bytes of a primitive int.
66      *
67      * @return the size of a primitive int.
68      */

69     public static int sizeOfInt() {
70         return 4;
71     }
72
73     /**
74      * Returns the size in bytes of a primitive char.
75      *
76      * @return the size of a primitive char.
77      */

78     public static int sizeOfChar() {
79         return 2;
80     }
81
82     /**
83      * Returns the size in bytes of a primitive boolean.
84      *
85      * @return the size of a primitive boolean.
86      */

87     public static int sizeOfBoolean() {
88         return 1;
89     }
90
91     /**
92      * Returns the size in bytes of a primitive long.
93      *
94      * @return the size of a primitive long.
95      */

96     public static int sizeOfLong() {
97         return 8;
98     }
99
100     /**
101      * Returns the size in bytes of a primitive double.
102      *
103      * @return the size of a primitive double.
104      */

105     public static int sizeOfDouble() {
106         return 8;
107     }
108
109     /**
110      * Returns the size in bytes of a Date.
111      *
112      * @return the size of a Date.
113      */

114     public static int sizeOfDate() {
115         return 12;
116     }
117
118     /**
119      * Returns the size in bytes of a Properties object. All properties and
120      * property names must be Strings.
121      *
122      * @param properties the Properties object to determine the size of.
123      * @return the size of a Properties object.
124      */

125     public static int sizeOfProperties(Properties JavaDoc properties) {
126         if (properties == null) {
127             return 0;
128         }
129         //Base properties object
130
int size = 36;
131         //Add in size of each property
132
Enumeration JavaDoc e = properties.elements();
133         while(e.hasMoreElements()) {
134             String JavaDoc prop = (String JavaDoc)e.nextElement();
135             size += sizeOfString(prop);
136         }
137         //Add in property names
138
e = properties.propertyNames();
139         while(e.hasMoreElements()) {
140             String JavaDoc prop = (String JavaDoc)e.nextElement();
141             size += sizeOfString(prop);
142         }
143         return size;
144     }
145
146     /**
147      * Returns the size in bytes of a Map object. All keys and
148      * values <b>must be Strings</b>.
149      *
150      * @param map the Map object to determine the size of.
151      * @return the size of the Map object.
152      */

153     public static int sizeOfMap(Map JavaDoc map) {
154         if (map == null) {
155             return 0;
156         }
157         //Base map object -- should be something around this size.
158
int size = 36;
159         //Add in size of each value
160
Iterator JavaDoc iter = map.values().iterator();
161         while(iter.hasNext()) {
162             String JavaDoc value = (String JavaDoc)iter.next();
163             size += sizeOfString(value);
164         }
165         //Add in each key
166
iter = map.keySet().iterator();
167         while(iter.hasNext()) {
168             String JavaDoc key = (String JavaDoc)iter.next();
169             size += sizeOfString(key);
170         }
171         return size;
172     }
173 }
174
Popular Tags