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    import java.util.Arrays;
023    
024    import net.dpml.metro.info.Priority;
025    
026    /**
027     * Description of the configuration of a set of categories.
028     *
029     * @see CategoryDirective
030     * @author <a href="http://www.dpml.net">Digital Product Meta Library</a>
031     * @version 1.2.0
032     */
033    public class CategoriesDirective extends CategoryDirective implements Serializable
034    {
035       /**
036        * Serial version identifier.
037        */
038        static final long serialVersionUID = 1L;
039    
040        /**
041         * The root category hierachy.
042         */
043        private final CategoryDirective[] m_categories;
044    
045        /**
046         * Create a CategoriesDirective instance.
047         */
048        public CategoriesDirective()
049        {
050            this( "" );
051        }
052    
053        /**
054         * Create a CategoriesDirective instance.
055         *
056         * @param name the base category name
057         */
058        public CategoriesDirective( final String name )
059        {
060            this( name, null, null, new CategoryDirective[0] );
061        }
062    
063        /**
064         * Create a CategoriesDirective instance.
065         *
066         * @param categories the categories to include in the directive
067         */
068        public CategoriesDirective( CategoryDirective[] categories )
069        {
070            this( "", null, null, categories );
071        }
072    
073    
074        /**
075         * Create a CategoriesDirective instance.
076         *
077         * @param name the base category name
078         * @param priority the default logging priority
079         * @param target the default logging target
080         * @param categories the logging category descriptors
081         * @exception NullPointerException if a category array value is null
082         */
083        public CategoriesDirective( 
084          final String name, final Priority priority, final String target, 
085          final CategoryDirective[] categories ) throws NullPointerException
086        {
087            super( name, priority, target );
088            if( categories == null )
089            {
090                m_categories = new CategoryDirective[0];
091            }
092            else
093            {
094                for( int i=0; i<categories.length; i++ )
095                {
096                    CategoryDirective category = categories[i];
097                    if( null == category )
098                    {
099                        throw new NullPointerException( "category" );
100                    }
101                }
102                m_categories = categories;
103            }
104        }
105    
106        /**
107         * Return the set of logging categories.
108         *
109         * @return the set of category declarations
110         */
111        public CategoryDirective[] getCategories()
112        {
113            return m_categories;
114        }
115    
116        /**
117         * Return a named category.
118         *
119         * @param name the category name
120         * @return the category declaration
121         */
122        public CategoryDirective getCategoryDirective( String name )
123        {
124            for( int i = 0; i < m_categories.length; i++ )
125            {
126                final CategoryDirective category = m_categories[ i ];
127                if( category.getName().equalsIgnoreCase( name ) )
128                {
129                    return category;
130                }
131            }
132            return null;
133        }
134    
135       /**
136        * Test this object for equality with the suppplied object.
137        * @param other the other object
138        * @return TRUE if this object equals the supplied object
139        *   else FALSE
140        */
141        public boolean equals( Object other )
142        {
143            if( !super.equals( other ) )
144            {
145                return false;
146            }
147            else if( !( other instanceof CategoriesDirective ) )
148            {
149                return false;
150            }
151            else
152            {
153                CategoriesDirective directive = (CategoriesDirective) other;
154                return Arrays.equals( m_categories, directive.m_categories );
155            }
156        }
157    
158       /**
159        * Return the hashcode for the object.
160        * @return the hashcode
161        */
162        public int hashCode()
163        {
164            int hash = super.hashCode();
165            hash ^= hashArray( m_categories );
166            return hash;
167        }
168    }