public class DefaultMessageSizeEstimator implements IoEventSizeEstimator {
private final ConcurrentMap<Class<?>, Integer> class2size = new ConcurrentHashMap<Class<?>, Integer>();
public DefaultMessageSizeEstimator() {
class2size.put(boolean.class, 4); // Probably an integer.
class2size.put(byte.class, 1);
class2size.put(char.class, 2);
class2size.put(short.class, 2);
class2size.put(int.class, 4);
class2size.put(long.class, 8);
class2size.put(float.class, 4);
class2size.put(double.class, 8);
class2size.put(void.class, 0);
}
/**
* {@inheritDoc}
*/
public int estimateSize(IoEvent event) {
return estimateSize((Object) event) + estimateSize(event.getParameter());
}
public int estimateSize(Object message) {
if (message == null) {
return 8;
}
int answer = 8 + estimateSize(message.getClass(), null);
if (message instanceof IoBuffer) {
answer += ((IoBuffer) message).remaining();
} else if (message instanceof WriteRequest) {
answer += estimateSize(((WriteRequest) message).getMessage());
} else if (message instanceof CharSequence) {
answer += ((CharSequence) message).length() << 1;
} else if (message instanceof Iterable<?>) {
for (Object m : (Iterable<?>) message) {
answer += estimateSize(m);
}
}
return align(answer);
}
private int estimateSize(Class<?> clazz, Set<Class<?>> visitedClasses) {
Integer objectSize = class2size.get(clazz);
if (objectSize != null) {
return objectSize;
}
if (visitedClasses != null) {
if (visitedClasses.contains(clazz)) {
return 0;
}
} else {
visitedClasses = new HashSet<Class<?>>();
}
visitedClasses.add(clazz);
int answer = 8; // basic overhead
for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
Field[] fields = c.getDeclaredFields();
for (Field f : fields) {
// ignore static fields
if ((f.getModifiers() & Modifier.STATIC) != 0) {
continue;
}
answer += estimateSize(f.getType(), visitedClasses);
}
}
visitedClasses.remove(clazz);
// some alignment
answer = align(answer);
// put the final answer
Integer tmpAnswer = class2size.putIfAbsent(clazz, answer);
if (tmpAnswer != null) {
answer = tmpAnswer;
}
return answer;
}
private static int align(int size) {
if (size % 8 != 0) {
size /= 8;
size++;
size *= 8;
}
return size;
}
}