001: /**
002: * $RCSfile: CacheSizes.java,v $
003: * $Revision: 1.3 $
004: * $Date: 2005/09/26 18:25:02 $
005: *
006: * Copyright (C) 2000 CoolServlets.com. All rights reserved.
007: *
008: * ===================================================================
009: * The Apache Software License, Version 1.1
010: *
011: * Redistribution and use in source and binary forms, with or without
012: * modification, are permitted provided that the following conditions
013: * are met:
014: *
015: * 1. Redistributions of source code must retain the above copyright
016: * notice, this list of conditions and the following disclaimer.
017: *
018: * 2. Redistributions in binary form must reproduce the above copyright
019: * notice, this list of conditions and the following disclaimer in
020: * the documentation and/or other materials provided with the
021: * distribution.
022: *
023: * 3. The end-user documentation included with the redistribution,
024: * if any, must include the following acknowledgment:
025: * "This product includes software developed by
026: * CoolServlets.com (http://www.Yasna.com)."
027: * Alternately, this acknowledgment may appear in the software itself,
028: * if and wherever such third-party acknowledgments normally appear.
029: *
030: * 4. The names "Jive" and "CoolServlets.com" must not be used to
031: * endorse or promote products derived from this software without
032: * prior written permission. For written permission, please
033: * contact webmaster@Yasna.com.
034: *
035: * 5. Products derived from this software may not be called "Jive",
036: * nor may "Jive" appear in their name, without prior written
037: * permission of CoolServlets.com.
038: *
039: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
040: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
041: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
042: * DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
043: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
044: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
045: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
046: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
047: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
048: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
049: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
050: * SUCH DAMAGE.
051: * ====================================================================
052: *
053: * This software consists of voluntary contributions made by many
054: * individuals on behalf of CoolServlets.com. For more information
055: * on CoolServlets.com, please see <http://www.Yasna.com>.
056: */package com.Yasna.util;
057:
058: import java.util.*;
059:
060: /**
061: * Utility class for determining the sizes in bytes of commonly used objects.
062: * Classes implementing the Cacheable interface should use this class to
063: * determine their size.
064: */
065: public class CacheSizes {
066:
067: /**
068: * Returns the size in bytes of a basic Object. This method should only
069: * be used for actual Object objects and not classes that extend Object.
070: *
071: * @return the size of an Object.
072: */
073: public static int sizeOfObject() {
074: return 4;
075: }
076:
077: /**
078: * Returns the size in bytes of a String.
079: *
080: * @param string the String to determine the size of.
081: * @return the size of a String.
082: */
083: public static int sizeOfString(String string) {
084: if (string == null) {
085: return 0;
086: }
087: return 4 + string.length() * 2;
088: }
089:
090: /**
091: * Returns the size in bytes of a primitive int.
092: *
093: * @return the size of a primitive int.
094: */
095: public static int sizeOfInt() {
096: return 4;
097: }
098:
099: /**
100: * Returns the size in bytes of a primitive char.
101: *
102: * @return the size of a primitive char.
103: */
104: public static int sizeOfChar() {
105: return 2;
106: }
107:
108: /**
109: * Returns the size in bytes of a primitive boolean.
110: *
111: * @return the size of a primitive boolean.
112: */
113: public static int sizeOfBoolean() {
114: return 1;
115: }
116:
117: /**
118: * Returns the size in bytes of a primitive long.
119: *
120: * @return the size of a primitive long.
121: */
122: public static int sizeOfLong() {
123: return 8;
124: }
125:
126: /**
127: * Returns the size in bytes of a primitive double.
128: *
129: * @return the size of a primitive double.
130: */
131: public static int sizeOfDouble() {
132: return 8;
133: }
134:
135: /**
136: * Returns the size in bytes of a Date.
137: *
138: * @return the size of a Date.
139: */
140: public static int sizeOfDate() {
141: return 12;
142: }
143:
144: /**
145: * Returns the size in bytes of a Properties object. All properties and
146: * property names must be Strings.
147: *
148: * @param properties the Properties object to determine the size of.
149: * @return the size of a Properties object.
150: */
151: public static int sizeOfProperties(Properties properties) {
152: if (properties == null) {
153: return 0;
154: }
155: //Base properties object
156: int size = 36;
157: //Add in size of each property
158: Enumeration enume = properties.elements();
159: while (enume.hasMoreElements()) {
160: String prop = (String) enume.nextElement();
161: size += sizeOfString(prop);
162: }
163: //Add in property names
164: enume = properties.propertyNames();
165: while (enume.hasMoreElements()) {
166: String prop = (String) enume.nextElement();
167: size += sizeOfString(prop);
168: }
169: return size;
170: }
171:
172: /**
173: * Returns the size in bytes of a Map object. All keys and
174: * values <b>must be Strings</b>.
175: *
176: * @param map the Map object to determine the size of.
177: * @return the size of the Map object.
178: */
179: public static int sizeOfMap(Map map) {
180: if (map == null) {
181: return 0;
182: }
183: //Base map object -- should be something around this size.
184: int size = 36;
185: //Add in size of each value
186: Iterator iter = map.values().iterator();
187: while (iter.hasNext()) {
188: String value = (String) iter.next();
189: size += sizeOfString(value);
190: }
191: //Add in each key
192: iter = map.keySet().iterator();
193: while (iter.hasNext()) {
194: String key = (String) iter.next();
195: size += sizeOfString(key);
196: }
197: return size;
198: }
199: }
|