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.Properties;
022    
023    import net.dpml.lang.Category;
024    import net.dpml.lang.Enum;
025    
026    /**
027     * The IncludeDirective class describes a dependency on a named resource.
028     *
029     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
030     * @version 1.2.0
031     */
032    public class IncludeDirective extends AbstractDirective
033    {
034       /**
035        * Current module scoped key mode constant.
036        */
037        public static final Mode KEY = Mode.KEY;
038        
039       /**
040        * Absolute resource reference mode constant.
041        */
042        public static final Mode REF = Mode.REF;
043        
044       /**
045        * URN resource reference mode constant.
046        */
047        public static final Mode URI = Mode.URI;
048        
049        private final Mode m_mode;
050        private final String m_value;
051        private final Category m_category;
052        
053       /**
054        * Creation of a new include directive.
055        * @param mode the include mode
056        * @param category the runtime category
057        * @param value the value (key or reference address depending on mode)
058        * @param properties supplimentary properties
059        */
060        public IncludeDirective( Mode mode, Category category, String value, Properties properties )
061        {
062            super( properties );
063            
064            if( null == mode )
065            {
066                throw new NullPointerException( "mode" );
067            }
068            if( null == value )
069            {
070                throw new NullPointerException( "value" );
071            }
072            if( null == category )
073            {
074                m_category = Category.UNDEFINED; // was PRIVATE
075            }
076            else
077            {
078                m_category = category;
079            }
080            m_mode = mode;
081            m_value = value;
082        }
083        
084       /**
085        * Return the include mode.
086        * @return the mode
087        */
088        public Mode getMode()
089        {
090            return m_mode;
091        }
092        
093       /**
094        * Return the category associated with the include.
095        * @return the category
096        */
097        public Category getCategory()
098        {
099            return m_category;
100        }
101    
102       /**
103        * Return the include value.
104        * @return the value
105        */
106        public String getValue()
107        {
108            return m_value;
109        }
110    
111       /**
112        * Compare this object with another for equality.
113        * @param other the other object
114        * @return true if equal
115        */
116        public boolean equals( Object other )
117        {
118            if( super.equals( other ) && ( other instanceof IncludeDirective ) )
119            {
120                IncludeDirective include = (IncludeDirective) other;
121                if( !equals( m_mode, include.m_mode ) )
122                {
123                    return false;
124                }
125                if( !equals( m_category, include.m_category ) )
126                {
127                    return false;
128                }
129                else
130                {
131                    return equals( m_value, include.m_value );
132                }
133            }
134            else
135            {
136                return false;
137            }
138        }
139        
140       /**
141        * Compute the hash value.
142        * @return the hascode value
143        */
144        public int hashCode()
145        {
146            int hash = super.hashCode();
147            hash ^= super.hashValue( m_mode );
148            hash ^= super.hashValue( m_category );
149            hash ^= super.hashValue( m_value );
150            return hash;
151        }
152       
153       /**
154        * Return a string representation of the include.
155        * @return the string value
156        */
157        public String toString()
158        {
159            return "include:" + m_mode + ":" + m_category + ":" + m_value;
160        }
161        
162       /**
163        * Mode of inclusion.
164        */
165        public static final class Mode extends Enum
166        {
167            static final long serialVersionUID = 1L;
168    
169           /**
170            * Include by reference to a local key.
171            */
172            public static final Mode KEY = new Mode( "key" );
173    
174           /**
175            * Include by reference to an absolute resource address.
176            */
177            public static final Mode REF = new Mode( "ref" );
178        
179           /**
180            * Include by urn definition.
181            */
182            public static final Mode URI = new Mode( "uri" );
183        
184           /**
185            * Internal constructor.
186            * @param label the enumeration label.
187            */
188            private Mode( String label )
189            {
190                super( label );
191            }
192                    
193           /**
194            * Create a now mode using a supplied mode name.
195            * @param value the mode name
196            * @return the mode
197            */
198            public static Mode parse( String value )
199            {
200                if( value.equalsIgnoreCase( "key" ) )
201                {
202                    return KEY;
203                }
204                else if( value.equalsIgnoreCase( "ref" ) )
205                {
206                    return REF;
207                }
208                else if( value.equalsIgnoreCase( "uri" ) )
209                {
210                    return URI;
211                }
212                else
213                {
214                    final String error =
215                      "Unrecognized resource mode argument [" + value + "]";
216                    throw new IllegalArgumentException( error );
217                }
218            }
219        }
220    }