1 11 12 package org.jivesoftware.util; 13 14 import java.util.Map ; 15 import java.util.Collection ; 16 17 24 public class CacheSizes { 25 26 32 public static int sizeOfObject() { 33 return 4; 34 } 35 36 42 public static int sizeOfString(String string) { 43 if (string == null) { 44 return 0; 45 } 46 return 4 + string.length() * 2; 47 } 48 49 54 public static int sizeOfInt() { 55 return 4; 56 } 57 58 63 public static int sizeOfChar() { 64 return 2; 65 } 66 67 72 public static int sizeOfBoolean() { 73 return 1; 74 } 75 76 81 public static int sizeOfLong() { 82 return 8; 83 } 84 85 90 public static int sizeOfDouble() { 91 return 8; 92 } 93 94 99 public static int sizeOfDate() { 100 return 12; 101 } 102 103 110 public static int sizeOfMap(Map map) { 111 if (map == null) { 112 return 0; 113 } 114 int size = 36; 116 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 for (int i = 0; i < keys.length; i++) { 124 size += sizeOfString((String )keys[i]); 125 } 126 return size; 127 } 128 129 136 public static int sizeOfCollection(Collection list) { 137 if (list == null) { 138 return 0; 139 } 140 int size = 36; 142 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 } 149 else if (obj instanceof Long ) { 150 size += sizeOfLong() + sizeOfObject(); 151 } 152 else { 153 size += ((Cacheable)obj).getCachedSize(); 154 } 155 } 156 return size; 157 } 158 } | Popular Tags |