<s> package org . eclipse . jdt . internal . core ; import java . util . HashMap ; import java . util . Map ; import org . eclipse . jdt . core . IJavaElement ; import org . eclipse . jdt . internal . core . util . LRUCache ; import org . eclipse . jdt . internal . core . util . Util ; public class JavaModelCache { public static boolean VERBOSE = false ; public static final int DEFAULT_PROJECT_SIZE = 5 ; public static final int DEFAULT_ROOT_SIZE = 50 ; public static final int DEFAULT_PKG_SIZE = 500 ; public static final int DEFAULT_OPENABLE_SIZE = 250 ; public static final int DEFAULT_CHILDREN_SIZE = 250 * 20 ; public static final String RATIO_PROPERTY = "" ; public static final Object NON_EXISTING_JAR_TYPE_INFO = new Object ( ) ; protected double memoryRatio = - 1 ; protected Object modelInfo ; protected HashMap projectCache ; protected ElementCache rootCache ; protected ElementCache pkgCache ; protected ElementCache openableCache ; protected Map childrenCache ; protected LRUCache jarTypeCache ; public JavaModelCache ( ) { double ratio = getMemoryRatio ( ) ; double openableRatio = getOpenableRatio ( ) ; this . projectCache = new HashMap ( DEFAULT_PROJECT_SIZE ) ; if ( VERBOSE ) { this . rootCache = new VerboseElementCache ( ( int ) ( DEFAULT_ROOT_SIZE * ratio ) , "Root cache" ) ; this . pkgCache = new VerboseElementCache ( ( int ) ( DEFAULT_PKG_SIZE * ratio ) , "" ) ; this . openableCache = new VerboseElementCache ( ( int ) ( DEFAULT_OPENABLE_SIZE * ratio * openableRatio ) , "" ) ; } else { this . rootCache = new ElementCache ( ( int ) ( DEFAULT_ROOT_SIZE * ratio ) ) ; this . pkgCache = new ElementCache ( ( int ) ( DEFAULT_PKG_SIZE * ratio ) ) ; this . openableCache = new ElementCache ( ( int ) ( DEFAULT_OPENABLE_SIZE * ratio * openableRatio ) ) ; } this . childrenCache = new HashMap ( ( int ) ( DEFAULT_CHILDREN_SIZE * ratio * openableRatio ) ) ; resetJarTypeCache ( ) ; } private double getOpenableRatio ( ) { String property = System . getProperty ( RATIO_PROPERTY ) ; if ( property != null ) { try { return Double . parseDouble ( property ) ; } catch ( NumberFormatException e ) { Util . log ( e , "" + RATIO_PROPERTY + ": " + property ) ; } } return 1.0 ; } public Object getInfo ( IJavaElement element ) { switch ( element . getElementType ( ) ) { case IJavaElement . JAVA_MODEL : return this . modelInfo ; case IJavaElement . JAVA_PROJECT : return this . projectCache . get ( element ) ; case IJavaElement . PACKAGE_FRAGMENT_ROOT : return this . rootCache . get ( element ) ; case IJavaElement . PACKAGE_FRAGMENT : return this . pkgCache . get ( element ) ; case IJavaElement . COMPILATION_UNIT : case IJavaElement . CLASS_FILE : return this . openableCache . get ( element ) ; case IJavaElement . TYPE : Object result = this . jarTypeCache . get ( element ) ; if ( result != null ) return result ; else return this . childrenCache . get ( element ) ; default : return this . childrenCache . get ( element ) ; } } public IJavaElement getExistingElement ( IJavaElement element ) { switch ( element . getElementType ( ) ) { case IJavaElement . JAVA_MODEL : return element ; case IJavaElement . JAVA_PROJECT : return element ; case IJavaElement . PACKAGE_FRAGMENT_ROOT : return ( IJavaElement ) this . rootCache . getKey ( element ) ; case IJavaElement . PACKAGE_FRAGMENT : return ( IJavaElement ) this . pkgCache . getKey ( element ) ; case IJavaElement . COMPILATION_UNIT : case IJavaElement . CLASS_FILE : return ( IJavaElement ) this . openableCache . getKey ( element ) ; case IJavaElement . TYPE : return element ; default : return element ; } } protected double getMemoryRatio ( ) { if ( ( int ) this . memoryRatio == - 1 ) { long maxMemory = Runtime . getRuntime ( ) . maxMemory ( ) ; this . memoryRatio = maxMemory == Long . MAX_VALUE ? 4d : ( ( double ) maxMemory ) / ( 64 * 0x100000 ) ; } return this . memoryRatio ; } protected Object peekAtInfo ( IJavaElement element ) { switch ( element . getElementType ( ) ) { case IJavaElement . JAVA_MODEL : return this . modelInfo ; case IJavaElement . JAVA_PROJECT : return this . projectCache . get ( element ) ; case IJavaElement . PACKAGE_FRAGMENT_ROOT : return this . rootCache . peek ( element ) ; case IJavaElement . PACKAGE_FRAGMENT : return this . pkgCache . peek ( element ) ; case IJavaElement . COMPILATION_UNIT : case IJavaElement . CLASS_FILE : return this . openableCache . peek ( element ) ; case IJavaElement . TYPE : Object result = this . jarTypeCache . peek ( element ) ; if ( result != null ) return result ; else return this . childrenCache . get ( element ) ; default : return this . childrenCache . get ( element ) ; } } protected void putInfo ( IJavaElement element , Object info ) { switch ( element . getElementType ( ) ) { case IJavaElement . JAVA_MODEL : this . modelInfo = info ; break ; case IJavaElement . JAVA_PROJECT : this . projectCache . put ( element , info ) ; this . rootCache . ensureSpaceLimit ( info , element ) ; break ; case IJavaElement . PACKAGE_FRAGMENT_ROOT : this . rootCache . put ( element , info ) ; this . pkgCache . ensureSpaceLimit ( info , element ) ; break ; case IJavaElement . PACKAGE_FRAGMENT : this . pkgCache . put ( element , info ) ; this . openableCache . ensureSpaceLimit ( info , element ) ; break ; case IJavaElement . COMPILATION_UNIT : case IJavaElement . CLASS_FILE : this . openableCache . put ( element , info ) ; break ; default : this . childrenCache . put ( element , info ) ; } } protected void removeInfo ( JavaElement element ) { switch ( element . getElementType ( ) ) { case IJavaElement . JAVA_MODEL : this . modelInfo = null ; break ; case IJavaElement . JAVA_PROJECT : this . projectCache . remove ( element ) ; this . rootCache . resetSpaceLimit ( ( int ) ( DEFAULT_ROOT_SIZE * getMemoryRatio ( ) ) , element ) ; break ; case IJavaElement . PACKAGE_FRAGMENT_ROOT : this . rootCache . remove ( element ) ; this . pkgCache . resetSpaceLimit ( ( int ) ( DEFAULT_PKG_SIZE * getMemoryRatio ( ) ) , element ) ; break ; case IJavaElement . PACKAGE_FRAGMENT : this . pkgCache . remove ( element ) ; this . openableCache . resetSpaceLimit ( ( int ) ( DEFAULT_OPENABLE_SIZE * getMemoryRatio ( ) * getOpenableRatio ( ) ) , element ) ; break ; case IJavaElement . COMPILATION_UNIT : case IJavaElement . CLASS_FILE : this . openableCache . remove ( element ) ; break ; default : this . childrenCache . remove ( element ) ; } } protected void resetJarTypeCache ( ) { this . jarTypeCache = new LRUCache ( ( int ) ( DEFAULT_OPENABLE_SIZE * getMemoryRatio ( ) ) ) ; } public String toString ( ) { return toStringFillingRation ( "" ) ; } public String toStringFillingRation ( String prefix ) { StringBuffer buffer = new StringBuffer ( ) ; buffer . append ( prefix ) ; buffer . append ( "" ) ; buffer . append ( this . projectCache . size ( ) ) ; buffer . append ( " projectsn" ) ; buffer . append ( prefix ) ; buffer . append ( this . rootCache . toStringFillingRation ( "Root cache" ) ) ; buffer . append ( '\n' ) ; buffer . append ( prefix ) ; buffer . append ( this . pkgCache . toStringFillingRation ( "" ) ) ; buffer . append ( '\n' ) ; buffer . append ( prefix ) ; buffer . append ( this . openableCache . toStringFillingRation ( "" ) ) ; buffer . append ( '\n' ) ; buffer . append ( prefix ) ; buffer . append ( this . jarTypeCache . toStringFillingRation ( "" ) ) ; buffer . append ( '\n' ) ; return buffer . toString ( ) ; } } </s>