1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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 }