001    /*
002     * Copyright 2005 Stephen J. McConnell
003     *
004     * Licensed  under the  Apache License,  Version 2.0  (the "License");
005     * you may not use  this file  except in  compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     *   http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed  under the  License is distributed on an "AS IS" BASIS,
012     * WITHOUT  WARRANTIES OR CONDITIONS  OF ANY KIND, either  express  or
013     * implied.
014     *
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    
019    package net.dpml.lang;
020    
021    import java.io.Serializable;
022    
023    /**
024     * Abstract directive base class.
025     *
026     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
027     * @version 1.1.0
028     */
029    public abstract class AbstractDirective implements Serializable
030    {
031       /**
032        * Compare this object to the supplied object for equality.
033        * @param other the other object
034        * @return true if equal
035        */
036        public boolean equals( Object other )
037        {
038            if( null == other )
039            {
040                return false;
041            }
042            else
043            {
044                return ( other instanceof AbstractDirective );
045            }
046        }
047        
048       /**
049        * Calculate the hashcode.
050        * @return the hashcode value
051        */
052        public int hashCode()
053        {
054            return getClass().hashCode();
055        }
056        
057       /**
058        * Utility to hash an array.
059        * @param array the array
060        * @return the hash value
061        */
062        protected int hashArray( Object[] array )
063        {
064            if( null == array )
065            {
066                return 0;
067            }
068            int hash = 0;
069            for( int i=0; i<array.length; i++ )
070            {
071                Object object = array[i];
072                hash ^= hashValue( object );
073            }
074            return hash;
075        }
076        
077       /**
078        * Utility to hash an object.
079        * @param value the object
080        * @return the hash value
081        */
082        protected int hashValue( Object value )
083        {
084            if( null == value )
085            {
086                return 0;
087            }
088            else if( value instanceof Object[] )
089            {
090                return hashArray( (Object[]) value );
091            }
092            else
093            {
094                return value.hashCode();
095            }
096        }
097        
098       /**
099        * Utility to compare two object for equality.
100        * @param a the first object
101        * @param b the second object
102        * @return true if the objects are equal
103        */
104        protected boolean equals( Object a, Object b )
105        {
106            if( null == a )
107            {
108                return ( null == b );
109            }
110            else
111            {
112                return a.equals( b );
113            }
114        }
115    }