Following on from yesterdays post, here's a patch that tries to get this
class to conform to the coding standards chosen by the Derby developers
- remove unused import
- change static final variables from camelCase to CONST_NAMES (except
where they are public and could be used elsewhere BWC concern)
- add {} for conditionals
- strip lots of extra white space
As for the CLA/ICLA, I've already signed one (for ant), so do I need to
sign another for Derby?
Thanks
Kev
Index:
D:/java_projects/derby-trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java
===================================================================
---
D:/java_projects/derby-trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java
(revision 375440)
+++
D:/java_projects/derby-trunk/java/engine/org/apache/derby/iapi/services/cache/ClassSize.java
(working copy)
@@ -2,7 +2,7 @@
Derby - Class org.apache.derby.iapi.services.cache.ClassSize
- Copyright 2002, 2004 The Apache Software Foundation or its licensors, as
applicable.
+ Copyright 2002, 2006 The Apache Software Foundation or its licensors, as
applicable.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
@@ -25,21 +25,23 @@
import java.lang.Class;
import java.lang.reflect.Field;
import java.lang.Runtime;
-import java.lang.InterruptedException;
import java.lang.reflect.Modifier;
-public class ClassSize
-{
+public class ClassSize {
+
+ //cannot change this due to BC concerns as it's declared public
public static final int refSize;
- private static final int objectOverhead = 2; // references, not bytes!
- private static final int booleanSize = 4;
- private static final int charSize = 4; // Unicode
- private static final int shortSize = 4;
- private static final int intSize = 4;
- private static final int longSize = 8;
- private static final int floatSize = 4;
- private static final int doubleSize = 8;
- private static final int minObjectSize;
+
+ // these have been changed to reflect the common standard for naming
static final values
+ private static final int OBJECT_OVERHEAD = 2; // references, not bytes!
+ private static final int BOOLEAN_SIZE = 4;
+ private static final int CHAR_SIZE = 4; // Unicode
+ private static final int SHORT_SIZE = 4;
+ private static final int INT_SIZE = 4;
+ private static final int LONG_SIZE = 8;
+ private static final int FLOAT_SIZE = 4;
+ private static final int DOUBLE_SIZE = 8;
+ private static final int MIN_OBJECT_SIZE;
private static boolean dummyCatalog = false; // Used when constructing the
catalog to prevent recursion
@@ -49,7 +51,7 @@
static boolean unitTest = false;
// unitTest is used in unit testing
- private static final int[] wildGuess = {0,16};
+ private static final int[] WILD_GUESS = {0,16};
/* The standard wild guess of the size of an unknown class, the size of 16
references.
* Used when the security manager will not let us look at the class fields.
*/
@@ -59,14 +61,14 @@
* until everything else has been compiled. Bury ClassSizeCatalog in a
string.
*/
private static java.util.Hashtable catalog;
- static
- {
- try
- {
+
+ static {
+ try {
catalog = (java.util.Hashtable)
- Class.forName(
"org.apache.derby.iapi.services.cache.ClassSizeCatalog").newInstance();
+
Class.forName("org.apache.derby.iapi.services.cache.ClassSizeCatalog").newInstance();
+ } catch(Exception e){
+ //why is this caught and then not either rethrown or logged?
}
- catch( Exception e){};
// Figure out whether this is a 32 or 64 bit machine.
Runtime runtime = Runtime.getRuntime();
@@ -74,33 +76,31 @@
Object[] junk = new Object[10000];
long memUsed = runtime.totalMemory() - runtime.freeMemory() - memBase;
int sz = (int)((memUsed + junk.length/2)/junk.length);
- refSize = ( 4 > sz) ? 4 : sz;
- minObjectSize = 4*refSize;
+ refSize = (4 > sz) ? 4 : sz;
+ MIN_OBJECT_SIZE = 4*refSize;
}
/**
* do not try to use the catalog.
*/
- public static void setDummyCatalog()
- {
+ public static void setDummyCatalog() {
dummyCatalog = true;
}
+
/**
* Get the estimate of the size of an object reference.
*
* @return the estimate in bytes.
*/
- public static int getRefSize()
- {
+ public static int getRefSize() {
return refSize;
}
/**
* @return the estimate of the size of a primitive int
*/
- public static int getIntSize()
- {
- return intSize;
+ public static int getIntSize() {
+ return INT_SIZE;
}
/**
@@ -117,46 +117,38 @@
* @return an array of 2 integers. The first integer is the constant part
of the function,
* the second is the reference size coefficient.
*/
- public static int[] getSizeCoefficients( Class cl)
- {
- int[] coeff = {0, objectOverhead};
-
-
+ public static int[] getSizeCoefficients(Class cl) {
+ int[] coeff = {0, OBJECT_OVERHEAD};
- for( ; null != cl; cl = cl.getSuperclass())
- {
- Field[] field = cl.getDeclaredFields();
- if( null != field)
- {
- for( int i = 0; i < field.length; i++)
- {
- if( ! Modifier.isStatic( field[i].getModifiers()))
- {
- Class fieldClass = field[i].getType();
- if( fieldClass.isArray() || ! fieldClass.isPrimitive())
+ for(; null != cl; cl = cl.getSuperclass()) {
+ Field[] fields = cl.getDeclaredFields();
+ if(null != fields) {
+ for(int i = 0; i < fields.length; i++) {
+ if(!Modifier.isStatic(fields[i].getModifiers())) {
+ Class fieldClass = fields[i].getType();
+ if(fieldClass.isArray() || !fieldClass.isPrimitive()) {
coeff[1]++;
- else // Is simple primitive
- {
+ } else { //Is simple primitive
String name = fieldClass.getName();
-
- if( name.equals( "int") || name.equals( "I"))
- coeff[0] += intSize;
- else if( name.equals( "long") || name.equals( "J"))
- coeff[0] += longSize;
- else if( name.equals( "boolean") || name.equals(
"Z"))
- coeff[0] += booleanSize;
- else if( name.equals( "short") || name.equals(
"S"))
- coeff[0] += shortSize;
- else if( name.equals( "byte") || name.equals( "B"))
+ if(name.equals("int") || name.equals("I")) {
+ coeff[0] += INT_SIZE;
+ } else if(name.equals("long") || name.equals("J"))
{
+ coeff[0] += LONG_SIZE;
+ } else if(name.equals("boolean") ||
name.equals("Z")) {
+ coeff[0] += BOOLEAN_SIZE;
+ } else if(name.equals("short") ||
name.equals("S")) {
+ coeff[0] += SHORT_SIZE;
+ } else if(name.equals("byte") || name.equals("B"))
{
coeff[0] += 1;
- else if( name.equals( "char") || name.equals( "C"))
- coeff[0] += charSize;
- else if( name.equals( "float") || name.equals(
"F"))
- coeff[0] += floatSize;
- else if( name.equals( "double") || name.equals(
"D"))
- coeff[0] += doubleSize;
- else // What is this??
+ } else if(name.equals("char") || name.equals("C"))
{
+ coeff[0] += CHAR_SIZE;
+ } else if(name.equals("float") ||
name.equals("F")) {
+ coeff[0] += FLOAT_SIZE;
+ } else if(name.equals("double") ||
name.equals("D")) {
+ coeff[0] += DOUBLE_SIZE;
+ } else { // What is this??
coeff[1]++; // Make a guess: one reference (?)
+ }
}
}
}
@@ -173,13 +165,12 @@
*
* @return the size estimate, in bytes
*/
- public static int estimateBaseFromCoefficients( int[] coeff)
- {
+ public static int estimateBaseFromCoefficients(int[] coeff) {
int size = coeff[0] + coeff[1]*refSize;
// Round up to a multiple of 8
size = (size + 7)/8;
size *= 8;
- return (size < minObjectSize) ? minObjectSize : size;
+ return (size < MIN_OBJECT_SIZE) ? MIN_OBJECT_SIZE : size;
} // end of estimateBaseFromCoefficients
/**
@@ -193,36 +184,33 @@
* @see #getSizeCoefficients
* see org.apache.derbyBuild.ClassSizeCrawler
*/
- public static int estimateBaseFromCatalog( Class cls)
- {
+ public static int estimateBaseFromCatalog(Class cls) {
return estimateBaseFromCatalog( cls, false);
}
- private static int estimateBaseFromCatalog( Class cls, boolean
addToCatalog)
- {
- if( dummyCatalog)
+ private static int estimateBaseFromCatalog( Class cls, boolean
addToCatalog) {
+ if(dummyCatalog) {
return 0;
-
- if( SanityManager.DEBUG)
- SanityManager.ASSERT( catalog != null, "The class size
catalog could not be initialized.");
-
- int[] coeff = (int[]) catalog.get( cls.getName());
- if( coeff == null)
- {
- try
- {
- coeff = getSizeCoefficients( cls);
+ }
+ if(SanityManager.DEBUG) {
+ SanityManager.ASSERT(catalog != null, "The class size catalog
could not be initialized.");
+ }
+ int[] coeff = (int[]) catalog.get(cls.getName());
+ if(null == coeff) {
+ try {
+ coeff = getSizeCoefficients(cls);
}
- catch( Throwable t)
- {
- if( noGuess)
+ catch(Throwable t) {
+ if(noGuess) {
return -2;
- coeff = wildGuess;
+ }
+ coeff = WILD_GUESS;
}
- if( addToCatalog)
- catalog.put( cls.getName(), coeff);
+ if(addToCatalog) {
+ catalog.put(cls.getName(), coeff);
+ }
}
- return estimateBaseFromCoefficients( coeff);
+ return estimateBaseFromCoefficients(coeff);
} // end of estimateBaseFromCatalog
@@ -238,9 +226,8 @@
* @see #getSizeCoefficients
* see org.apache.derbyBuild.ClassSizeCrawler
*/
- public static int estimateAndCatalogBase( Class cls)
- {
- return estimateBaseFromCatalog( cls, true);
+ public static int estimateAndCatalogBase(Class cls) {
+ return estimateBaseFromCatalog(cls, true);
} // end of estimateAndCatalogBase
/**
@@ -257,18 +244,16 @@
* see org.apache.derbyBuild.ClassSizeCrawler
* @see #estimateBaseFromCatalog
*/
- public static int estimateBase( Class cl)
- {
- return estimateBaseFromCoefficients( getSizeCoefficients( cl));
+ public static int estimateBase(Class cl) {
+ return estimateBaseFromCoefficients(getSizeCoefficients(cl));
} // End of estimateBase
/**
* @return the estimated overhead of an array. The estimated size of an
x[n] array is
* estimateArrayOverhead() + n*sizeOf(x).
*/
- public static int estimateArrayOverhead()
- {
- return minObjectSize;
+ public static int estimateArrayOverhead() {
+ return MIN_OBJECT_SIZE;
}
/**
@@ -277,9 +262,8 @@
*
* @return the estimate, in bytes
*/
- public static int estimateHashEntrySize()
- {
- return objectOverhead + 3*refSize;
+ public static int estimateHashEntrySize() {
+ return OBJECT_OVERHEAD + 3*refSize;
}
/**
@@ -287,11 +271,11 @@
*
* @return the estimated size, in bytes
*/
- public static int estimateMemoryUsage( String str)
- {
- if( null == str)
+ public static int estimateMemoryUsage(String str) {
+ if(null == str) {
return 0;
+ }
// Since Java uses Unicode assume that each character takes 2 bytes
return 2*str.length();
}
-}
+}
\ No newline at end of file