View Javadoc

1   /*
2    *  Copyright (C) 2003-2005 SINTEF
3    *  Author:  Fredrik Vraalsen (fredrik dot vraalsen at sintef dot no)
4    *  Webpage: http://coras.sourceforge.net/
5    *
6    *  This program is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public License
8    *  as published by the Free Software Foundation; either version 2.1
9    *  of the License, or (at your option) any later version.
10   *
11   *  This program is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this program; if not, write to the Free
18   *  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
19   *  02111-1307 USA
20   */
21  package coras.structure;
22  
23  import java.io.InvalidObjectException;
24  import java.io.ObjectStreamException;
25  import java.io.Serializable;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.Iterator;
30  import java.util.List;
31  
32  /***
33   * CORAS subprocess enumeration and helper methods.
34   * 
35   * @author Fredrik Vraalsen
36   */
37  public final class SubProcessEnum implements Serializable {
38  	
39  	public static final SubProcessEnum CONTEXT_IDENTIFICATION = new SubProcessEnum("Context identification");
40  	public static final SubProcessEnum RISK_IDENTIFICATION = new SubProcessEnum("Risk identification");
41  	public static final SubProcessEnum RISK_ANALYSIS = new SubProcessEnum("Risk analysis");
42  	public static final SubProcessEnum RISK_EVALUATION = new SubProcessEnum("Risk evaluation");
43  	public static final SubProcessEnum RISK_TREATMENT = new SubProcessEnum("Risk treatment");
44  	private static final SubProcessEnum[] VALS = { 
45  			CONTEXT_IDENTIFICATION, 
46  			RISK_IDENTIFICATION, 
47  			RISK_ANALYSIS, 
48  			RISK_EVALUATION, 
49  			RISK_TREATMENT };
50  	public static final List VALUES = Collections.unmodifiableList(Arrays.asList(VALS));
51  	private static final List NAMES = new ArrayList(); 
52  	
53  	static {
54  		for (Iterator i = VALUES.iterator(); i.hasNext();) {
55  			NAMES.add(i.next().toString());
56  		}
57  	}
58  		
59  	private final String name;
60  	
61  	/***
62  	 * Private constructor.
63  	 * 
64  	 * @param name
65  	 *            the name of the subprocess
66  	 */
67  	private SubProcessEnum(String name) {
68  		this.name = name;
69  	}
70  	
71  	/***
72  	 * Get the SubProcessEnum corresponding to the specified name.
73  	 * 
74  	 * @param name
75  	 *            the subprocess name
76  	 * @return the SubProcessEnum, or null if not found
77  	 */
78  	public static SubProcessEnum forName(String name) {
79  		int index = NAMES.indexOf(name);
80  		if (index < 0 || index >= VALS.length) {
81  			return null;
82  		} else {
83  			return VALS[index];
84  		}
85  	}
86  	
87  	/***
88  	 * Get the String representation.
89  	 * 
90  	 * @return the name of the subprocess
91  	 */
92  	public String toString() {
93  		return name;
94  	}
95  
96  	/***
97  	 * Enumeration pattern: Ensure that a.equals(b) <=> (a == b) for all
98  	 * SubProcessEnums a and b when deserializing.
99  	 * 
100 	 * @return the SubProcessEnum corresponding to the subprocess string
101 	 * @throws ObjectStreamException
102 	 *             if no SubProcessEnum can be found for that subprocess string
103 	 */
104 	private Object readResolve() throws ObjectStreamException {
105 		SubProcessEnum result = forName(name);
106 		if (result == null) {
107 			throw new InvalidObjectException("Illegal SubProcessEnum instance: " + name);
108 		}
109 		return result;
110 	}
111 
112 }