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.metro.data;
020    
021    import java.io.Serializable;
022    import java.net.URI;
023    import java.net.URISyntaxException;
024    
025    /**
026     * Abstract base class for directives.
027     *
028     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
029     * @version 1.0.4
030     */
031    public abstract class AbstractDirective implements Serializable
032    {
033        //--------------------------------------------------------------------------
034        // static
035        //--------------------------------------------------------------------------
036    
037       /**
038        * Serial version identifier.
039        */
040        static final long serialVersionUID = 1L;
041        
042        //--------------------------------------------------------------------------
043        // utilities
044        //--------------------------------------------------------------------------
045        
046       /**
047        * Return the hashcode for the instance.
048        * @return the instance hashcode
049        */
050        public int hashCode()
051        {
052            return getClass().hashCode();
053        }
054        
055       /**
056        * Utility to hash an array.
057        * @param array the array
058        * @return the hash value
059        */
060        int hashArray( Object[] array )
061        {
062            if( null == array )
063            {
064                return 0;
065            }
066            int hash = 0;
067            for( int i=0; i<array.length; i++ )
068            {
069                Object object = array[i];
070                hash ^= hashValue( object );
071            }
072            return hash;
073        }
074        
075       /**
076        * Utility to hash an object.
077        * @param value the object
078        * @return the hash value
079        */
080        int hashValue( Object value )
081        {
082            if( null == value )
083            {
084                return 0;
085            }
086            else if( value instanceof Object[] )
087            {
088                return hashArray( (Object[]) value );
089            }
090            else
091            {
092                return value.hashCode();
093            }
094        }
095        
096       /**
097        * Test if the supplied object is equal to this object.
098        * @param other the object to compare with this instance
099        * @return TRUE if the supplied object is equal to this object
100        */
101        public boolean equals( Object other )
102        {
103            if( null == other )
104            {
105                return false;
106            }
107            else
108            {
109                return ( other instanceof AbstractDirective );
110            }
111        }
112        
113       /**
114        * Utility to compare two object for equality.
115        * @param a the first object
116        * @param b the second object
117        * @return true if the objects are equal
118        */
119        boolean equals( Object a, Object b )
120        {
121            if( null == a )
122            {
123                return ( null == b );
124            }
125            else
126            {
127                return a.equals( b );
128            }
129        }
130        
131        //--------------------------------------------------------------------------
132        // Part
133        //--------------------------------------------------------------------------
134    
135       /**
136        * Return the part handler uri.
137        * @return the uri of the part handler
138        */
139        public URI getPartHandlerURI()
140        {
141            return PART_HANDLER_URI;
142        }
143    
144       /**
145        * Static utility to create the part handler uri.
146        * @param spec the part handler uri string
147        * @return the constant part handler uri
148        */
149        protected static URI setupURI( String spec )
150        {
151            try
152            {
153                return new URI( spec );
154            }
155            catch( URISyntaxException ioe )
156            {
157                return null;
158            }
159        }
160        
161        private static final URI PART_HANDLER_URI = setupURI( "artifact:part:dpml/metro/dpml-metro-runtime#1.0.4" );
162    
163    }