/* ======================================================================
   Parts Copyright 2006 University of Leeds, Oxford University, University of the Highlands and Islands.

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.

====================================================================== */

package org.bodington.xml;


import java.text.*;

/**
 *
 * @author  jrm
 */
public class Word implements Cloneable
{
    // The word as a string in original form.
    String source;

    // The id of the first word in the database that is equal according
    // to various strength settings of the collator.
    // The identical_id refers to the ID of the entry for this word
    // and any of the other may be equal to that or a lower id value
    // because they match a word that was put in the database before
    // this word was deposited.
    
    // The order of the array is primary, secondary, tertiary, identical
    
    int id[] = new int[4];

    // The collation keys - these are not stored in the database, they are
    // regenerated when the word is loaded from the database or when a new
    // word is deposited.  The system administrator will need to rebuild the
    // word records if the collation scheme is changed because these need
    // to work with the stored id values.

    // The order of the array is primary, secondary, tertiary,
    CollationKey[] key = new CollationKey[3];

    private Word()
    {
    }
    
    /** Creates a new instance of Word */
    public Word( String s, int id1, int id2, int id3, int idi, CollationKey k1, CollationKey k2, CollationKey k3 )
    {
        source = s;
        
        id[0] = id1;
        id[1] = id2;
        id[2] = id3;
        id[3] = idi;
        
        key[0] =  k1;
        key[1] =  k2;
        key[2] =  k3;
    }
    
    public String getSource()
    {
        return source;
    }
    
    public void setSource( String s )
    {
        source =s;
    }
    
    public CollationKey getCollationKey( int strength )
    {
        switch ( strength )
        {
            case Collator.PRIMARY:
                return key[0];
            case Collator.SECONDARY:
                return key[1];
            case Collator.TERTIARY:
                return key[2];
        }
        throw new IllegalArgumentException( "Unknown collation strength." );
    }

    public void setCollationKey( CollationKey k, int strength )
    {
        switch ( strength )
        {
            case Collator.PRIMARY:
                key[0]=k;
                return;
            case Collator.SECONDARY:
                key[1]=k;
                return;
            case Collator.TERTIARY:
                key[2]=k;
                return;
        }
        throw new IllegalArgumentException( "Unknown collation strength." );
    }
    
    public int getID( int strength )
    {
        switch ( strength )
        {
            case Collator.PRIMARY:
                return id[0];
            case Collator.SECONDARY:
                return id[1];
            case Collator.TERTIARY:
                return id[2];
            case Collator.IDENTICAL:
                return id[3];
        }
        throw new IllegalArgumentException( "Unknown collation strength." );
    }

    public void setID( int i, int strength )
    {
        switch ( strength )
        {
            case Collator.PRIMARY:
                id[0] = i;
                return;
            case Collator.SECONDARY:
                id[1] = i;
                return;
            case Collator.TERTIARY:
                id[2] = i;
                return;
            case Collator.IDENTICAL:
                id[3] = i;
                return;
        }
        throw new IllegalArgumentException( "Unknown collation strength." );
    }

    public Object clone()
    throws CloneNotSupportedException
    {
        Word w = new Word();
        System.arraycopy( this.id, 0, w.id, 0, 4 );
        System.arraycopy( this.key, 0, w.key, 0, 3 );
        w.source = this.source;
        return w;
    }
    
    public int hashCode()
    {
        return source.hashCode();
    }
    
    public boolean equals( Object other )
    {
        if ( !(other instanceof Word) )
            return false;
        Word other_word = (Word)other;
        return this.source.equals( other_word.getSource() );
    }
}
