RequestIdentifier.java :  » Database-ORM » TJDO » com » triactive » jdo » store » Java Open Source

Java Open Source » Database ORM » TJDO 
TJDO » com » triactive » jdo » store » RequestIdentifier.java
/*
 * Copyright 2004 (C) TJDO.
 * All rights reserved.
 *
 * This software is distributed under the terms of the TJDO License version 1.0.
 * See the terms of the TJDO License in the documentation provided with this software.
 *
 * $Id: RequestIdentifier.java,v 1.3 2004/01/18 03:01:06 jackknifebarber Exp $
 */

package com.triactive.jdo.store;

import java.util.Arrays;


class RequestIdentifier
{
    private final ClassBaseTable table;
    private final int[] fields;
    private final Type type;
    private final int hashCode;


    public RequestIdentifier(ClassBaseTable table, int[] fields, Type type)
    {
        this.table = table;
        this.type = type;

        if (fields == null)
            this.fields = null;
        else
        {
            this.fields = new int[fields.length];
            System.arraycopy(fields, 0, this.fields, 0, fields.length);

            // The key uniqueness is dependent on fields being sorted
            Arrays.sort(this.fields);
        }

        /*
         * Since we are an immutable object, pre-compute the hash code for
         * improved performance in equals().
         */
        int h = table.hashCode() ^ type.hashCode();

        if (this.fields != null)
        {
            for (int i = 0; i < this.fields.length; ++i)
                h ^= this.fields[i];
        }

        hashCode = h;
    }


    public int hashCode()
    {
        return hashCode;
    }


    public boolean equals(Object o)
    {
        if (o == this)
            return true;

        if (!(o instanceof RequestIdentifier))
            return false;

        RequestIdentifier ri = (RequestIdentifier)o;

        if (hashCode != ri.hashCode)
            return false;

        return table.equals(ri.table)
            && type.equals(ri.type)
            && Arrays.equals(fields, ri.fields);
    }


    public static class Type
    {
        private int typeId;

        public static final Type INSERT        = new Type(0);
        public static final Type LOOKUP        = new Type(1);
        public static final Type FETCH         = new Type(2);
        public static final Type UPDATE        = new Type(3);
        public static final Type DELETE        = new Type(4);

        private static final String[] typeNames =
        {
            "Insert",
            "Lookup",
            "Fetch",
            "Update",
            "Delete"
        };

        private Type(int typeId)
        {
            this.typeId = typeId;
        }

        public String toString()
        {
            return typeNames[typeId];
        }
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.