key0-96/
key0-96/doc/key/
key0-96/doc/key/credits/
key0-96/doc/key/developers/
key0-96/doc/key/developers/resources/
key0-96/setup/caves/
key0-96/setup/help/
key0-96/setup/ruins/
key0-96/src/
key0-96/src/commands/
key0-96/src/events/
key0-96/src/hack/
key0-96/src/sql/
key0-96/src/swing/
key0-96/src/talker/forest/
key0-96/src/talker/objects/
key0-96/src/terminals/
/*
**               j###t  ########## ####   ####
**              j###t   ########## ####   ####
**             j###T               "###L J###"
**          ######P'    ##########  #########
**          ######k,    ##########   T######T
**          ####~###L   ####
**          #### q###L  ##########   .#####
**          ####  \###L ##########   #####"
**
**  $Id$
**
**  Class History
**
**  Date        Name         Description
**  ---------|------------|-----------------------------------------------
**  19Aug98     subtle       start of recorded history
**
*/

package key;

import java.util.Enumeration;
import java.util.Vector;
import java.io.IOException;
import java.io.DataInput;
import java.io.DataOutput;
import java.util.NoSuchElementException;

public class AliasListKey implements java.io.Serializable
{
	private static final long serialVersionUID = -6663255759351326195L;
	protected String primaryKey;
	protected Vector secondarys;
	protected transient int[] secondaryHashcodes;
	protected transient int primaryHashcode;
	
	public AliasListKey()
	{
		primaryKey = "";
		secondarys = null;
		secondaryHashcodes = null;
	}
	
	public String getPrimary()
	{
		return( primaryKey );
	}
	
	public void setPrimary( String pk )
	{
		primaryKey = pk.toLowerCase();
		
		if( secondaryHashcodes != null )
			primaryHashcode = primaryKey.hashCode();
	}
	
	public void addSecondary( String sk )
	{
		ensureSecondary();
		secondarys.addElement( sk.toLowerCase() );
		secondaryHashcodes = null;
	}
	
	public void removeSecondary( String sk )
	{
		if( secondarys != null )
		{
			secondarys.removeElement( sk.toLowerCase() );
			secondaryHashcodes = null;
		}
	}
	
	public void copySecondaries( AliasListKey alk )
	{
		if( alk.secondarys != null )
			secondarys = (Vector) alk.secondarys.clone();
		else
			secondarys = null;
	}
	
	public Enumeration secondaryKeys()
	{
		ensureSecondary();
		return( secondarys.elements() );
	}
	
	public boolean hasSecondaries()
	{
		return( !( secondarys == null || secondarys.size() == 0 ) );
	}
	
	private void ensureSecondary()
	{
		if( secondarys == null )
			secondarys = new Vector( 1, 3 );
	}
	
	public String toString()
	{
		return( primaryKey );
	}
	
	private void ensureHashcodes()
	{
		if( secondaryHashcodes == null )
		{
			primaryHashcode = primaryKey.hashCode();
			
			if( secondarys != null )
			{
				secondaryHashcodes = new int[ secondarys.size() ];
				
				for( int i = 0; i < secondarys.size(); i++ )
					secondaryHashcodes[i] = secondarys.elementAt( i ).hashCode();
			}
		}
	}
	
	/**
	  *  Returns the strength of this match (0 = no match, 1 = primary
	  *  match, 2 = first secondary, etc) based on the hashcode and 
	  *  string supplied.  hc = match.toLowercase().hashCode();
	 */
	public int getMatchStrength( int hc, String match )
	{
		ensureHashcodes();
		
		if( hc == primaryHashcode )
		{
			if( primaryKey.equalsIgnoreCase( match ) )
				return( 1 );
		}
		
		if( secondaryHashcodes != null )
		{
			for( int i = 0; i < secondaryHashcodes.length; i++ )
			{
				if( hc == secondaryHashcodes[i] )
				{
					if( ((String)secondarys.elementAt( i )).equalsIgnoreCase( match ) )
						return( i+2 );
				}
			}
		}
		
		return( 0 );
	}
}