001: /**
002: * $RCSfile$
003: * $Revision: 8139 $
004: * $Date: 2007-05-01 11:50:19 -0700 (Tue, 01 May 2007) $
005: *
006: * Copyright (C) 2004 Jive Software. All rights reserved.
007: *
008: * This software is published under the terms of the GNU Public License (GPL),
009: * a copy of which is included in this distribution.
010: */package org.jivesoftware.util.cache;
011:
012: import org.jivesoftware.util.cache.Cacheable;
013:
014: import java.util.Map;
015: import java.util.Collection;
016:
017: /**
018: * Utility class for determining the sizes in bytes of commonly used objects.
019: * Classes implementing the Cacheable interface should use this class to
020: * determine their size.
021: *
022: * @author Matt Tucker
023: */
024: public class CacheSizes {
025:
026: /**
027: * Returns the size in bytes of a basic Object. This method should only
028: * be used for actual Object objects and not classes that extend Object.
029: *
030: * @return the size of an Object.
031: */
032: public static int sizeOfObject() {
033: return 4;
034: }
035:
036: /**
037: * Returns the size in bytes of a String.
038: *
039: * @param string the String to determine the size of.
040: * @return the size of a String.
041: */
042: public static int sizeOfString(String string) {
043: if (string == null) {
044: return 0;
045: }
046: return 4 + string.length() * 2;
047: }
048:
049: /**
050: * Returns the size in bytes of a primitive int.
051: *
052: * @return the size of a primitive int.
053: */
054: public static int sizeOfInt() {
055: return 4;
056: }
057:
058: /**
059: * Returns the size in bytes of a primitive char.
060: *
061: * @return the size of a primitive char.
062: */
063: public static int sizeOfChar() {
064: return 2;
065: }
066:
067: /**
068: * Returns the size in bytes of a primitive boolean.
069: *
070: * @return the size of a primitive boolean.
071: */
072: public static int sizeOfBoolean() {
073: return 1;
074: }
075:
076: /**
077: * Returns the size in bytes of a primitive long.
078: *
079: * @return the size of a primitive long.
080: */
081: public static int sizeOfLong() {
082: return 8;
083: }
084:
085: /**
086: * Returns the size in bytes of a primitive double.
087: *
088: * @return the size of a primitive double.
089: */
090: public static int sizeOfDouble() {
091: return 8;
092: }
093:
094: /**
095: * Returns the size in bytes of a Date.
096: *
097: * @return the size of a Date.
098: */
099: 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 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[] values = map.values().toArray();
118: for (int i = 0; i < values.length; i++) {
119: size += sizeOfString((String) values[i]);
120: }
121: Object[] keys = map.keySet().toArray();
122: // Add in each key
123: for (int i = 0; i < keys.length; i++) {
124: size += sizeOfString((String) 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 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[] values = list.toArray();
144: for (int i = 0; i < values.length; i++) {
145: Object obj = values[i];
146: if (obj instanceof String) {
147: size += sizeOfString((String) obj);
148: } else if (obj instanceof Long) {
149: size += sizeOfLong() + sizeOfObject();
150: } else {
151: size += ((Cacheable) obj).getCachedSize();
152: }
153: }
154: return size;
155: }
156: }
|