View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ErrorRegistry.java
13   * Created: 2008-10-07
14   * Author: kpietak
15   * $Id: ErrorRegistry.java 2308 2009-01-12 22:33:45Z kpietak $
16   */
17  
18  package pl.edu.agh.cast.rawdata.stat;
19  
20  import java.util.Collection;
21  import java.util.HashMap;
22  
23  import org.apache.log4j.Logger;
24  
25  /**
26   * Registry for raw data errors. All errors handlers should use this class to fetch appropriate error types.
27   * 
28   * @author AGH CAST Team
29   * 
30   */
31  public final class ErrorRegistry {
32  
33  	private static final ErrorRegistry INSTANCE = new ErrorRegistry();
34  
35  	private static Logger log = Logger.getLogger(ErrorRegistry.class);
36  
37  	private HashMap<Integer, ErrorType> errors = new HashMap<Integer, ErrorType>();
38  
39  	private ErrorRegistry() {
40  		// to avoid direct instantiation
41  	}
42  
43  	public static ErrorRegistry getInstance() {
44  		return INSTANCE;
45  	}
46  
47  	/**
48  	 * Registers a given error type.
49  	 * 
50  	 * @param err
51  	 *            error type to register
52  	 * @throws ErrorAlreadyExistsException
53  	 *             when error to register already exists in registry
54  	 */
55  	public void register(ErrorType err) throws ErrorAlreadyExistsException {
56  		if (errors.containsKey(err.getCode())) {
57  			throw new ErrorAlreadyExistsException(err);
58  		}
59  		errors.put(err.getCode(), err);
60  
61  		log.debug("New error type registered: error code: " + err.getCode() + //$NON-NLS-1$
62  		        "; error message" + err.getMessage() + ";"); //$NON-NLS-1$ //$NON-NLS-2$
63  
64  	}
65  
66  	/**
67  	 * Checks if registry contains an error by given code.
68  	 * 
69  	 * @param code
70  	 *            error code
71  	 * @return true if error exists in registry, false otherwise
72  	 */
73  	public boolean containsErrorCode(Integer code) {
74  		return errors.containsKey(code);
75  	}
76  
77  	/**
78  	 * Gets error type by given error code.
79  	 * 
80  	 * @param code
81  	 *            error code
82  	 * @return error type with given code, null if there is no error with the code
83  	 */
84  	public ErrorType get(Integer code) {
85  		return errors.get(code);
86  	}
87  
88  	/**
89  	 * Returns all error types.
90  	 * 
91  	 * @return all error types.
92  	 */
93  	public Collection<ErrorType> getAll() {
94  		return errors.values();
95  	}
96  
97  }