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.tools.info;
020    
021    import java.net.URI;
022    import java.util.Properties;
023    
024    import net.dpml.library.info.AbstractDirective;
025    
026    /**
027     * The ListenerDirective is an immutable descriptor used to define 
028     * a build listener to be attached to an Ant project.
029     *
030     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
031     * @version 1.1.3
032     */
033    public final class ListenerDirective  extends AbstractDirective implements Comparable
034    {
035        private final String m_name;
036        private final int m_priority;
037        private final URI m_uri;
038        private final String m_classname;
039        
040       /**
041        * Creation of a new listener directive.
042        * @param name the listener name
043        * @param priority the listener priority
044        * @param uri the listener codebase
045        * @param classname optional classname of the plugin instantiation target
046        * @param properties supplimentary properties
047        * @exception NullPointerException if name or dependencies are null
048        * @exception IllegalStateException if both classname and urn values are null
049        */
050        public ListenerDirective( 
051          String name, int priority, URI uri, String classname, Properties properties )
052          throws NullPointerException, IllegalStateException
053        {
054            super( properties );
055            if( null == name )
056            {
057                throw new NullPointerException( "name" );
058            }
059            if( null == uri )
060            {
061                if( null == classname )
062                {
063                    final String error = 
064                      "Listener definition [" 
065                      + name
066                      + "] does not declare a classname or uri value.";
067                    throw new IllegalStateException( error );
068                }
069            }
070            m_priority = priority;
071            m_name = name;
072            m_uri = uri;
073            m_classname = classname;
074        }
075        
076       /**
077        * Return the listener name.
078        * @return the listener name
079        */
080        public String getName()
081        {
082            return m_name;
083        }
084        
085       /**
086        * Return the listener priority.
087        * @return the priority value
088        */
089        public int getPriority()
090        {
091            return m_priority;
092        }
093        
094       /**
095        * Return the listener codebase uri.
096        * @return the urn
097        */
098        public URI getURI()
099        {
100            return m_uri;
101        }
102        
103       /**
104        * Return the listener codebase uri as a string.
105        * @return the uri value
106        */
107        public String getURISpec()
108        {
109            if( null == m_uri )
110            {
111                return null;
112            }
113            else
114            {
115                return m_uri.toASCIIString();
116            }
117        }
118        
119       /**
120        * Get the listener classname.
121        *
122        * @return the classname of the listener
123        */
124        public String getClassname()
125        {
126            return m_classname;
127        }
128        
129        /**
130         * Compares this <code>ListenerDirective</code> object to another object.
131         * If the object is an <code>ListenerDirective</code>, this function behaves
132         * like <code>compareTo(Integer)</code>.  Otherwise, it throws a
133         * <code>ClassCastException</code>.
134         *
135         * @param other the <code>Object</code> to be compared.
136         * @return the value <code>0</code> if the argument is a 
137         * <code>ListenerDirective</code> with a priority numerically equal to this 
138         * <code>ListenerDirective</code>; a value less than <code>0</code> 
139         * if the argument is a <code>ListenerDirective</code> numerically 
140         * greater than this <code>ListenerDirective</code>; and a value 
141         * greater than <code>0</code> if the argument is a 
142         * <code>ListenerDirective</code> numerically less than this 
143         * <code>ListenerDirective</code>.
144         * @exception ClassCastException if the argument is not an
145         * <code>ListenerDirective</code>.
146         * @see java.lang.Comparable
147         */
148        public int compareTo( Object other ) throws ClassCastException
149        {
150            ListenerDirective directive = (ListenerDirective) other;
151            Integer p1 = new Integer( m_priority );
152            Integer p2 = new Integer( directive.getPriority() );
153            return p1.compareTo( p2 );
154        }
155        
156       /**
157        * Compare this object with another for equality.
158        * @param other the other object
159        * @return true if equal
160        */
161        public boolean equals( Object other )
162        {
163            if( super.equals( other ) && ( other instanceof ListenerDirective ) )
164            {
165                ListenerDirective object = (ListenerDirective) other;
166                if( !equals( m_name, object.m_name ) )
167                {
168                    return false;
169                }
170                else if( m_priority != object.m_priority )
171                {
172                    return false;
173                }
174                else
175                {
176                    return equals( m_uri, object.m_uri );
177                }
178            }
179            else
180            {
181                return false;
182            }
183        }
184        
185       /**
186        * Compute the hash value.
187        * @return the hashcode value
188        */
189        public int hashCode()
190        {
191            int hash = super.hashCode();
192            hash ^= super.hashValue( m_name );
193            hash ^= super.hashValue( m_uri );
194            hash ^= m_priority;
195            return hash;
196        }
197    }