/* Copyright 2004, 2005 Acegi Technology Pty Limited
 *
 * 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.acegisecurity.acl.basic;

import org.springframework.util.Assert;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;


/**
 * Simple implementation of {@link AclObjectIdentity}.
 * 
 * <P>
 * Uses <code>String</code>s to store the identity of the domain object
 * instance. Also offers a constructor that uses reflection to build the
 * identity information.
 * </p>
 *
 * @author Ben Alex
 * @version $Id$
 */
public class NamedEntityObjectIdentity implements AclObjectIdentity {
    //~ Instance fields ========================================================

    private String classname;
    private String id;

    //~ Constructors ===========================================================

    public NamedEntityObjectIdentity(String classname, String id) {
        if ((classname == null) || "".equals(classname)) {
            throw new IllegalArgumentException("classname required");
        }

        if ((id == null) || "".equals(id)) {
            throw new IllegalArgumentException("id required");
        }

        this.classname = classname;
        this.id = id;
    }

    /**
     * Creates the <code>NamedEntityObjectIdentity</code> based on the passed
     * object instance. The passed object must provide a <code>getId()</code>
     * method, otherwise an exception will be thrown.
     *
     * @param object the domain object instance to create an identity for
     *
     * @throws IllegalAccessException
     * @throws InvocationTargetException
     * @throws IllegalArgumentException
     */
    public NamedEntityObjectIdentity(Object object)
        throws IllegalAccessException, InvocationTargetException {
        Assert.notNull(object, "object cannot be null");

        this.classname = object.getClass().getName();

        Class clazz = object.getClass();

        try {
            Method method = clazz.getMethod("getId", new Class[] {});
            Object result = method.invoke(object, new Object[] {});
            this.id = result.toString();
        } catch (NoSuchMethodException nsme) {
            throw new IllegalArgumentException("Object of class '" + clazz
                + "' does not provide the required getId() method: " + object);
        }
    }

    protected NamedEntityObjectIdentity() {
        throw new IllegalArgumentException("Cannot use default constructor");
    }

    //~ Methods ================================================================

    /**
     * Indicates the classname portion of the object identity.
     *
     * @return the classname (never <code>null</code>)
     */
    public String getClassname() {
        return classname;
    }

    /**
     * Indicates the instance identity portion of the object identity.
     *
     * @return the instance identity (never <code>null</code>)
     */
    public String getId() {
        return id;
    }

    /**
     * Important so caching operates properly.
     * 
     * <P>
     * Considers an object of the same class equal if it has the same
     * <code>classname</code> and <code>id</code> properties.
     * </p>
     *
     * @param arg0 object to compare
     *
     * @return <code>true</code> if the presented object matches this object
     */
    public boolean equals(Object arg0) {
        if (arg0 == null) {
            return false;
        }

        if (!(arg0 instanceof NamedEntityObjectIdentity)) {
            return false;
        }

        NamedEntityObjectIdentity other = (NamedEntityObjectIdentity) arg0;

        if (this.getId().equals(other.getId())
            && this.getClassname().equals(other.getClassname())) {
            return true;
        }

        return false;
    }

    /**
     * Important so caching operates properly.
     *
     * @return the hash of the classname and id
     */
    public int hashCode() {
        StringBuffer sb = new StringBuffer();
        sb.append(this.classname).append(this.id);

        return sb.toString().hashCode();
    }

    public String toString() {
        StringBuffer sb = new StringBuffer();
        sb.append(this.getClass().getName()).append("[");
        sb.append("Classname: ").append(this.classname);
        sb.append("; Identity: ").append(this.id).append("]");

        return sb.toString();
    }
}

@@ -1027,48 +1027,8 @@
 /p%3E%0A
- *%0A * @author Ben Alex%0A * @version $Id$%0A
  */%0A
@@ -1395,315 +1395,285 @@
     
-if ((classname == null) %7C%7C %22%22.equals(classname)) %7B%0A            throw new IllegalArgumentException(%22classname required%22);%0A        %7D%0A%0A        if ((id == null) %7C%7C %22%22.equals(id)) %7B%0A            throw new IllegalArgumentException(%22id required%22);%0A        %7D%0A%0A        this.classname = classname;%0A        this.id = id
+Assert.hasText(classname, %22classname required%22);%0A        Assert.hasText(id, %22id required%22);%0A        this.classname = classname;%0A        this.id = id;%0A    %7D%0A%0A    protected NamedEntityObjectIdentity() %7B%0A        throw new IllegalArgumentException(%22Cannot use default constructor%22)
 ;%0A  
@@ -2804,633 +2804,84 @@
     
-protected NamedEntityObjectIdentity() %7B%0A        throw new IllegalArgumentException(%22Cannot use default constructor%22);%0A    %7D%0A%0A    //~ Methods ================================================================%0A%0A    /**%0A     * Indicates the classname portion of the object identity.%0A     *%0A     * @return the classname (never %3Ccode%3Enull%3C/code%3E)%0A     */%0A    public String getClassname() %7B%0A        return classname;%0A    %7D%0A%0A    /**%0A     * Indicates the instance identity portion of the object identity.%0A     *%0A     * @return the instance identity (never %3Ccode%3Enull%3C/code%3E)%0A     */%0A    public String getId() %7B%0A        return id;%0A    %7D
+//~ Methods ================================================================
 %0A%0A  
@@ -3683,32 +3683,452 @@
 n false;%0A    %7D%0A%0A
+    /**%0A     * Indicates the classname portion of the object identity.%0A     *%0A     * @return the classname (never %3Ccode%3Enull%3C/code%3E)%0A     */%0A    public String getClassname() %7B%0A        return classname;%0A    %7D%0A%0A    /**%0A     * Indicates the instance identity portion of the object identity.%0A     *%0A     * @return the instance identity (never %3Ccode%3Enull%3C/code%3E)%0A     */%0A    public String getId() %7B%0A        return id;%0A    %7D%0A%0A
     /**%0A     * I

Use Spring Assert for cleaner code.