KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > beehive > netui > tools > testrecorder > shared > config > Categories


1 /*
2  * Copyright 2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  * $Header:$
17  */

18
19 package org.apache.beehive.netui.tools.testrecorder.shared.config;
20
21 import org.apache.beehive.netui.tools.testrecorder.shared.util.StringHelper;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Collections JavaDoc;
29
30 import org.apache.beehive.netui.tools.testrecorder.shared.Logger;
31
32
33 /**
34  * User: ozzy
35  * Date: Apr 9, 2004
36  * Time: 2:31:56 PM
37  */

38 public class Categories {
39
40     private static final Logger log = Logger.getInstance( Categories.class );
41
42     private Category[] categories;
43     // string -> Category
44
private HashMap JavaDoc categoryStringMap;
45     // Category -> list of TestDefinition objects
46
private HashMap JavaDoc categoryTestMap;
47
48     public Categories( Category[] categories ) {
49         this.categories = categories;
50         categoryStringMap = new HashMap JavaDoc();
51         categoryTestMap = new HashMap JavaDoc();
52         init();
53     }
54
55     private void init() {
56         for ( int i = 0; i < categories.length; i++ ) {
57             categoryStringMap.put( categories[i].getName(), categories[i] );
58             categoryTestMap.put( categories[i], new ArrayList JavaDoc() );
59         }
60     }
61
62     public Category[] getCategories() {
63         return categories;
64     }
65
66     public Category getCategory( String JavaDoc categoryString ) {
67         return (Category) getCategoryStringMap().get( categoryString );
68     }
69
70     private Map JavaDoc getCategoryStringMap() {
71         return categoryStringMap;
72     }
73
74     public List JavaDoc getTests( String JavaDoc category ) {
75         assert category != null : "ERROR: category string may not be null";
76         return getTests( getCategory( category ) );
77     }
78
79     /**
80      *
81      * @param category
82      * @return a List of TestDefinition objects, returns null if no tests exist for the category
83      */

84     public List JavaDoc getTests( Category category ) {
85         List JavaDoc list = (List JavaDoc) getCategoryTestMap().get( category );
86         if ( list == null ) {
87             return null;
88         }
89         return Collections.unmodifiableList( list);
90     }
91
92     private Map JavaDoc getCategoryTestMap() {
93         return categoryTestMap;
94     }
95
96     public void addTest( TestDefinition test ) throws ConfigException {
97         List JavaDoc testCategories = test.getCategories();
98         Category category = null;
99         for ( int i = 0; i < testCategories.size(); i++ ) {
100             category = (Category) testCategories.get( i );
101             addTest( category, test );
102         }
103     }
104
105     private void addTest( Category category, TestDefinition test ) throws ConfigException {
106         List JavaDoc list = null;
107         if ( !getCategoryTestMap().containsKey( category ) ) {
108             ConfigException ce = new ConfigException( "ERROR: unable to find category( " + category + " )" );
109             log.fatal( ce );
110             throw ce;
111         }
112         list = (List JavaDoc) getCategoryTestMap().get( category );
113         list.add( test );
114     }
115
116     public String JavaDoc toString() {
117         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 64 );
118         sb.append( "[ " );
119         sb.append( "categories( \n" + StringHelper.toString( getCategories(), "\n\t", "\n\t" ) + " ), \n" );
120         sb.append( "testMap( \n" + testMapToString() + " )" );
121         sb.append( " ]" );
122         return sb.toString();
123     }
124
125     public String JavaDoc testMapToString() {
126         Map JavaDoc map = getCategoryTestMap();
127         Iterator JavaDoc it = map.keySet().iterator();
128         StringBuffer JavaDoc sb = new StringBuffer JavaDoc( 16 * map.size() );
129         Category category = null;
130         List JavaDoc testList = null;
131         sb.append( "[\n" );
132         for ( int i = 0; it.hasNext(); i++ ) {
133             category = (Category) it.next();
134             testList = (List JavaDoc) map.get( category );
135             if ( i != 0 ) {
136                 sb.append( "\n" );
137             }
138             sb.append( "\t[" + i + "] key(" + category.getName() + "), value( " +
139                     TestDefinition.testListToString( testList ) + " )" );
140         }
141         sb.append( "\n]" );
142         return sb.toString();
143     }
144
145 }
146
Popular Tags