View Javadoc

1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.wizard.defaults;
11  
12  import com.sri.emo.wizard.Wizard;
13  import com.sri.emo.wizard.WizardException;
14  import com.sri.emo.wizard.WizardFactory;
15  import com.sri.emo.wizard.WizardRepository;
16  
17  import java.io.Serializable;
18  import java.util.*;
19  
20  
21  /***
22   * Default instance of wizard repository.  Stores factories and creates a new
23   * instance of each wizard when requested.  This implementation is threadsafe.
24   * This is used for testing only.
25   *
26   * @author Michael Rimov
27   */
28  public class DefaultWizardRepository implements WizardRepository, Serializable {
29      /***
30  	 * 
31  	 */
32  	private static final long serialVersionUID = 1L;
33  	/***
34       * Holds wizard factory instances keyed by wizard id.
35       */
36      private Map wizardMap;
37  
38      /***
39       * Default Constructor
40       */
41      public DefaultWizardRepository() {
42          wizardMap = new HashMap();
43      }
44  
45      public synchronized Wizard find(final Serializable id)
46              throws WizardException {
47          WizardFactory factory = (WizardFactory) wizardMap.get(id);
48          if (factory == null) {
49              throw new WizardException("Unable to locate Wizard Factory by id: "
50                      + id);
51          }
52          return factory.buildWizard();
53      }
54  
55      public synchronized List list() throws WizardException {
56          List returnValue = new ArrayList(wizardMap.size());
57  
58          for (Iterator i = wizardMap.values().iterator(); i.hasNext();) {
59              WizardFactory factory = (WizardFactory) i.next();
60              returnValue.add(factory.buildWizard());
61          }
62  
63          return returnValue;
64      }
65  
66      public synchronized void addFactory(final Serializable id,
67                                          final WizardFactory factory) {
68          wizardMap.put(id, factory);
69      }
70  
71      /***
72       * Removes a particular wizard from the repository.  Often more used
73       * for unit testing.
74       *
75       * @param id the id of the wizard.
76       */
77      public synchronized void removeWizard(final Serializable id) {
78          wizardMap.remove(id);
79      }
80  
81      public boolean equals(final Object parm1) {
82          DefaultWizardRepository other = (DefaultWizardRepository) parm1;
83  
84          return wizardMap.equals(other.wizardMap);
85      }
86  
87      public String toString() {
88          return "Wizard Repository: " + wizardMap.toString();
89      }
90  
91      public int hashCode() {
92          return wizardMap.hashCode();
93      }
94  }