FileDocCategorySizeDatePackage
StringInterner.javaAPI DocAzureus 3.0.3.47275Mon Jun 25 16:48:30 BST 2007org.gudy.azureus2.core3.util

StringInterner

public class StringInterner extends Object

Fields Summary
private static final int
MAX_MAP_SIZE
private static final int
TICK_PERIOD
private static final int
TOTAL_CLEAN_TIME
private static final int
TOTAL_CLEAN_TICKS
private static Map
map
private static final String[]
COMMON_KEYS
private static final ByteArrayHashMap
byte_map
private static boolean
general_interning_enabled
Constructors Summary
Methods Summary
public static java.lang.Stringintern(byte[] bytes)

		
	
		
			// can't use config here as too early in init!
		
		String	str = System.getProperty( "azureus.general.interning.enable" );
		
		if ( str != null && str.equals( "1" )){
			
			general_interning_enabled = true;
			
			Thread t = 
				new Thread("StringInterner:cleaner")
				{
					private int	tick_count;
					
					public void
					run()
					{
						while( true ){
							try{
								Thread.sleep( TICK_PERIOD );
								
								tick_count++;
								
								tidy( tick_count == TOTAL_CLEAN_TICKS );
								
							}catch( Throwable e ){
								
							}
						}
					}
				};
				
			t.setDaemon( true );
			
			t.start();
		}
	
		String res = (String)byte_map.get( bytes );
		
		// System.out.println( new String( bytes ) + " -> " + res );
		
		return( res );
	
public static java.lang.Stringintern(java.lang.String str)

		if ( !general_interning_enabled ){
			
			return( str );
		}
		
		synchronized( StringInterner.class ){
			
			
			entryDetails entry;
			
			/*
			while( ( entry = (entryDetails)queue.poll() ) != null ){
			}
			*/
						
			if ( map.size() > MAX_MAP_SIZE ){
				
				tidy( false );
			}
			
			entry = (entryDetails)map.get( str );
			
			if ( entry != null ){
				
				String	s = (String)entry.get();
				
				if ( s != null ){
					
					if ( entry.hit_count < Short.MAX_VALUE ){
					
						entry.hit_count++;
					}
										
					return( s );
				}
			}
			
			map.put( str, new entryDetails( str ));
						
			return( str );
		}
	
private static voidtidy(boolean clear)

		if ( !clear ){
			
			Iterator	it = map.values().iterator();
			
			while( it.hasNext()){
				
				entryDetails entry = (entryDetails)it.next();
		
					// random guess: size of an entry is 
					// Object: 8
					// Reference: 4*8
					// entryDetails: 4
					// Map.Entry: 2*8 + 4
					// = say 90 bytes (testing shows 90 :))
				
					// a String is 24 bytes + chars
				
				final int overhead	= 90;
				final int str_size	= 24 + entry.size;
				
				if ( entry.hit_count * str_size < overhead ){
					
					it.remove();
				}
			}
			
			if ( map.size() > MAX_MAP_SIZE / 2 ){
			
				// didn't compact enough, dump the whole thing and start again!
			
				clear = true;
			}
		}
		
		if ( clear ){
			
			map = new WeakHashMap( MAX_MAP_SIZE );
		}
		
		/*
		System.out.println( "trimmed down to " + map.size());
		
		List l = new ArrayList(map.values());
	
		Collections.sort(
				l,
				new Comparator()
				{
					public int 
					compare(
						Object o1, 
						Object o2 ) 
					{
						entryDetails	e1 = (entryDetails)o1;
						entryDetails	e2 = (entryDetails)o2;
						
						return( e2.hit_count - e1.hit_count );
					}
				});
		
		String	line = "";
		
		for (int i=0;i<Math.min( 128, l.size());i++){
			
			entryDetails	e = (entryDetails)l.get(i);
			
			line += "\"" + e.get() + "\",";
			
			if ( (i+1) % 8 == 0 ){
				
				System.out.println( line );
				
				line = "";
			}
		}
		
		System.out.println( line );
		*/