UserCacheKey.java :  » Database-ORM » Speedo_1.4.5 » org » objectweb » speedo » usercache » lib » Java Open Source

Java Open Source » Database ORM » Speedo_1.4.5 
Speedo_1.4.5 » org » objectweb » speedo » usercache » lib » UserCacheKey.java
/**
 * Copyright (C) 2001-2005 France Telecom R&D
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *
 * Authors: S.Chassande-Barrioz.
 * Created on 19 mars 2005
 *
 */
package org.objectweb.speedo.usercache.lib;

/**
 * Defines a key of an user cache. Several elements can compose the key.
 * The comparaison (equals and hashCode) are based on the key elements.  
 *  
 * @author S.Chassande-Barrioz
 */
public class UserCacheKey {

    /**
     * The elements composing the key
     */
    private Object[] elements; 
    
    public UserCacheKey(int size) {
        elements = new Object[size];
    }
    
    public UserCacheKey(Object[] elements) {
        this.elements = elements;
    }
    
    public void setPart(int index, Object o) {
        elements[index] = o; 
    }
    
    /**
     * 
     * @return the hashCode of the first element 
     */
    public int hashCode() {
        if (elements.length > 0 && elements[0] != null) {
            return elements[0].hashCode();
        } else {
            return elements.length;
        }
    }
    
    /**
     * Compares to an UserCacheKey instance.
     * @see java.lang.Object#equals(java.lang.Object)
     */
    public boolean equals(Object o) {
        if (!(o instanceof UserCacheKey)) {
            return false;
        }
        UserCacheKey uck = (UserCacheKey) o;
        if (uck.elements.length != elements.length) {
            return false;
        }
        for (int i = 0; i < elements.length; i++) {
            if ((elements[i] == null && uck.elements[i] != null) 
                    || (elements[i] != null && !elements[i].equals(uck.elements[i]))) {
                return false;
            }
        }
        return true;
    }
    

}
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.