/* ======================================================================
   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.server.realm;

import org.bodington.database.PrimaryKey;
import org.bodington.database.IndexKey;

public class MemberIndexKey
    implements IndexKey
    {
    PrimaryKey group_id;
    PrimaryKey user_id;

    MemberIndexKey()
        {
        group_id = null;
        user_id = null;
        }
    
    public boolean cacheNull()
        {
        return true;
        }
        
    public String whereClause()
        {
        StringBuffer where = new StringBuffer();
        
        where.append( "group_id = " );
        if ( group_id == null )
            where.append( "NULL" );
        else
            where.append( group_id.toString() );
        where.append( " AND user_id = " );
        if ( user_id == null )
            where.append( "NULL" );
        else
            where.append( user_id.toString() );
        
        return where.toString();
        }
    
    public void setGroupId(PrimaryKey key)
    	{
    	group_id = key;
    	}

    public PrimaryKey getGroupId()
    	{
    	return group_id;
	    }

    public void setUserId(PrimaryKey key)
    	{
    	user_id = key;
    	}

    public PrimaryKey getUserId()
    	{
    	return user_id;
	    }

    
    public boolean equals(Object obj)
        {
        if ( !(obj instanceof MemberIndexKey) )
            return false;

        MemberIndexKey other = (MemberIndexKey)obj;
        
        if ( other.group_id==null || this.group_id==null || 
             other.user_id==null || this.user_id==null    )
            return false;
        
        return  other.group_id.equals( this.group_id  ) && 
                other.user_id.equals( this.user_id  );
        }

    public int hashCode()
        {
        int a = user_id.intValue();
        int b = group_id.intValue();
        // XOR the two IDs to avoid a single user having all the membership
        // objects in the same hash bucket.
        return a ^ b;
        }

    }
    
