001    /*
002     * Copyright 2004 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    
023    import net.dpml.metro.info.Priority;
024    
025    /**
026     * A logging category descriptor hierachy.  The descriptor contains a category name, a
027     * optional priority value, and an optional target.  If the priority or target values
028     * null, the resulting value will be derived from the parent category desciptor. A
029     * category descriptor may 0-n subsidiary categories.  CategoryDirective names are relative.
030     * For example, the category "orb" will appear as "my-app.orb" if the parent category
031     * name is "my-app".
032     *
033     * <p><b>XML</b></p>
034     * <pre>
035     *    &lt;categories priority="<font color="darkred">INFO</font>"&gt;
036     *      &lt;category priority="<font color="darkred">DEBUG</font>"  name="<font color="darkred">loader</font>" /&gt;
037     *      &lt;category priority="<font color="darkred">WARN</font>"  name="<font color="darkred">types</font>" /&gt;
038     *      &lt;category priority="<font color="darkred">ERROR</font>"  name="<font color="darkred">types.builder</font>" target="<font color="darkred">default</font>"/&gt;
039     *      &lt;category name="<font color="darkred">profiles</font>" /&gt;
040     *      &lt;category name="<font color="darkred">lifecycle</font>" /&gt;
041     *      &lt;category name="<font color="darkred">verifier</font>" /&gt;
042     *    &lt;/categories&gt;
043     * </pre>
044     *
045     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
046     * @version 1.2.0
047     */
048    public class CategoryDirective extends AbstractDirective implements Serializable
049    {
050       /**
051        * Serial version identifier.
052        */
053        static final long serialVersionUID = 1L;
054    
055        /**
056         * The logging category name.
057         */
058        private final String m_name;
059    
060        /**
061         * The default logging priority.
062         */
063        private final Priority m_priority;
064    
065        /**
066         * The default logging target.
067         */
068        private final String m_target;
069    
070        /**
071         * Creation of a new CategoryDirective using a supplied name.
072         *
073         * @param name the category name
074         */
075        public CategoryDirective( final String name )
076        {
077            this( name, null, null );
078        }
079    
080        /**
081         * Creation of a new CategoryDirective using a supplied name and priority.
082         *
083         * @param name the category name
084         * @param priority the category priority - DEBUG, INFO, WARN, or ERROR
085         */
086        public CategoryDirective( final String name, Priority priority )
087        {
088            this( name, priority, null );
089        }
090    
091        /**
092         * Creation of a new CategoryDirective using a supplied name, priority, target and
093         * collection of subsidiary categories.
094         *
095         * @param name the category name
096         * @param priority the category priority - DEBUG, INFO, WARN, or ERROR
097         * @param target the name of a logging category target
098         *
099         */
100        public CategoryDirective(
101            final String name, final Priority priority, final String target )
102        {
103            m_name = name;
104            m_target = target;
105            m_priority = priority;
106        }
107    
108        /**
109         * Return the category name.
110         *
111         * @return the category name
112         */
113        public String getName()
114        {
115            return m_name;
116        }
117    
118        /**
119         * Return the logging priority for the category.
120         *
121         * @return the logging priority for the category
122         */
123        public Priority getPriority()
124        {
125            return m_priority;
126        }
127    
128        /**
129         * Return the default log target for the category.
130         *
131         * @return the default target name
132         */
133        public String getTarget()
134        {
135            return m_target;
136        }
137    
138       /**
139        * Test this object for equality with the supplied object.
140        * @param other the other object
141        * @return true if the objects are equal
142        */
143        public boolean equals( Object other )
144        {
145            if( null == other )
146            {
147                return false;
148            }
149    
150            if( !( other instanceof CategoryDirective ) )
151            {
152                return false;
153            }
154    
155            CategoryDirective test = (CategoryDirective) other;
156            if( !equals( m_name, test.m_name ) )
157            {
158                return false;
159            }
160            else if( !equals( m_priority, test.m_priority ) )
161            {
162                return false;
163            }
164            else
165            {
166                return equals( m_target, test.m_target );
167            }
168        }
169    
170       /**
171        * Return the instance hash code value.
172        * @return the hash value
173        */
174        public int hashCode()
175        {
176            int hash = super.hashCode();
177            hash ^= hashValue( m_name );
178            hash ^= hashValue( m_priority );
179            hash ^= hashValue( m_target );
180            return hash;
181        }
182    }