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.library.info;
020    
021    import java.util.Arrays;
022    import java.util.Properties;
023    
024    /**
025     * The ModuleDirective class describes a module data-structure.
026     *
027     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
028     * @version 1.1.2
029     */
030    public final class ModuleDirective extends ResourceDirective
031    {
032        private final ResourceDirective[] m_resources;
033        
034       /**
035        * Creation of a new module directive.  If the resource name if composite
036        * then the resource directive will be a module directive instance that either 
037        * encloses the resource or enclosed a resource containing the resource.
038        *
039        * @param name the resource name
040        * @param version the resource version
041        * @param classifier LOCAL or EXTERNAL classifier
042        * @param basedir the project basedir
043        * @param info info descriptor
044        * @param data datatypes produced by the resource
045        * @param dependencies resource dependencies
046        * @param properties suppliementary properties
047        * @param filters project filters
048        * @param resources subsidary resources
049        * @return the immediate enclosing resource
050        */
051        public static ModuleDirective createModuleDirective( 
052          String name, String version, Classifier classifier, String basedir, 
053          InfoDirective info, DataDirective[] data, 
054          DependencyDirective[] dependencies, Properties properties, 
055          FilterDirective[] filters, ResourceDirective[] resources )
056        {
057            int n = name.indexOf( "/" );
058            if( n > -1 )
059            {
060                ModuleDirective enclosing = null;
061                String[] elements = name.split( "/", -1 );
062                for( int i = ( elements.length-1 ); i>-1; i-- )
063                {
064                    String elem = elements[i];
065                    if( i == ( elements.length-1 ) )
066                    {
067                        enclosing =  
068                          new ModuleDirective(
069                            elem, version, classifier, basedir, info, data, dependencies,
070                            resources, properties, filters );
071                    }
072                    else
073                    {
074                        enclosing = 
075                          new ModuleDirective(
076                            elem, null, Classifier.EXTERNAL, null, null,
077                            new DataDirective[0], new DependencyDirective[0],
078                            new ResourceDirective[]{enclosing}, null, null );
079                    }
080                }
081                return enclosing;
082            }
083            else
084            {
085                return new ModuleDirective(
086                  name, version, classifier, basedir, info, data, dependencies,
087                  resources, properties, filters );
088            }
089        }
090        
091       /**
092        * Creation of a new module directive supporting the establishment
093        * of an anonymous resource.
094        *
095        * @param name the module name
096        * @param version the module version
097        * @param resource resource contained within the module
098        */
099        public ModuleDirective( String name, String version, ResourceDirective resource )
100        {
101            this(
102              name, version, Classifier.ANONYMOUS, null, null,
103              new DataDirective[0], new DependencyDirective[0],
104              new ResourceDirective[]{resource}, null, null );
105        }
106        
107       /**
108        * Creation of a new module directive.
109        * @param name the resource name
110        * @param version the resource version
111        * @param classifier LOCAL or EXTERNAL classifier
112        * @param basedir the project basedir
113        * @param info info descriptor
114        * @param data datatypes produced by the resource
115        * @param dependencies resource dependencies
116        * @param resources resource included within the module
117        * @param properties suppliementary properties
118        * @param filters project filters
119        */
120        public ModuleDirective(
121          String name, String version, Classifier classifier, String basedir, 
122          InfoDirective info, DataDirective[] data,
123          DependencyDirective[] dependencies, ResourceDirective[] resources,
124          Properties properties, FilterDirective[] filters )
125        {
126            super( name, version, classifier, basedir, info, data, dependencies, properties, filters );
127            
128            if( null == resources )
129            {
130                throw new NullPointerException( "resources" );
131            }
132            for( int i=0; i<resources.length; i++ )
133            {
134                if( null == resources[i] )
135                {
136                    throw new NullPointerException( "resource [" + i + "]" );
137                }
138            }
139            m_resources = resources;
140        }
141        
142       /**
143        * Return an array of resource directives representing the resources within 
144        * the module.
145        * @return the nested resource directives
146        */
147        public ResourceDirective[] getResourceDirectives()
148        {
149            return m_resources;
150        }
151        
152       /**
153        * Compare this object with another for equality.
154        * @param other the other object
155        * @return true if equal
156        */
157        public boolean equals( Object other )
158        {
159            if( super.equals( other ) && ( other instanceof ModuleDirective ) )
160            {
161                ModuleDirective object = (ModuleDirective) other;
162                return Arrays.equals( m_resources, object.m_resources );
163            }
164            else
165            {
166                return false;
167            }
168        }
169        
170       /**
171        * Compute the hash value.
172        * @return the hashcode value
173        */
174        public int hashCode()
175        {
176            int hash = super.hashCode();
177            hash ^= super.hashArray( m_resources );
178            return hash;
179        }
180    }