Source code: com/thermidor/util/exception/ExceptionDescriptor.java
1 package com.thermidor.util.exception;
2 /*@LEGAL@*/
3 import java.io.Serializable;
4 import java.util.LinkedList;
5 import java.util.Iterator;
6 import java.util.List;
7 /**
8 * The purpose of the ExceptionDescriptor is to provide a container for
9 * additional exceptioninformation that can be included in exception subclasses
10 * in those circumstances that the variety of exceptional situations is so
11 * great that the number of exception subclasses would become unmanageable.
12 * Alternatively the descriptor may be used as part of an exception processor.
13 * @version 1.0
14 * @author Edward Turnock
15 */
16 public class ExceptionDescriptor implements Serializable {
17 /**
18 * The list of error elements for this descriptor
19 */
20 private LinkedList elements = new LinkedList();
21 /**
22 * The exception major code.
23 */
24 private int major;
25
26 /**
27 * The exception minor
28 */
29 private int minor;
30
31 /**
32 * The subject of the exception
33 */
34 private String subject;
35
36 /**
37 * The human meaningful description ofthe exception and its causes.
38 */
39 private String description;
40
41 /**
42 * Construct a default instance of the ExceptionDescriptor.
43 */
44 public ExceptionDescriptor() {}
45
46 /**
47 * Construct a new instance of an ExceptionDescriptor with the specifed
48 * details.
49 * @param major the major code.
50 * @param minor the minor code.
51 * @param subject the subject of the exception
52 * @param description the human readable description of the causes of the
53 * exception.
54 */
55 public ExceptionDescriptor(int major,
56 int minor,
57 String subject,
58 String description) {
59 this.major = major;
60 this.minor = minor;
61 this.subject = subject;
62 this.description = description;
63 }
64
65 /**
66 * Return a string representation of this descriptor.
67 * @return the string representation of this descriptor
68 <pre><code>
69 <tt>
70 Exception 304.10 {
71 subject:{
72 some GTS subsystem
73 }
74 description:{
75 A fatal database error occured
76 }
77 }
78 </tt>
79 </code><pre>
80 */
81 public String toString() {
82 String ls = System.getProperty("line.separator");
83 StringBuffer retval = new StringBuffer("Exception " + major +
84 "." + minor +
85 "{" + ls);
86 retval.append(" subject:{" + ls);
87 retval.append(" " + subject + ls);
88 retval.append(" }");
89 retval.append(" description:{" + ls);
90 retval.append(" " + description + ls);
91 retval.append(" }");
92 retval.append(" errors:{");
93 Iterator it = elements.iterator();
94
95 while (it.hasNext()) {
96 retval.append(" element:{" + it.next() + "}" + ls);
97 }
98
99 retval.append(" }");
100 retval.append("}");
101 return retval.toString();
102 }
103
104 /**
105 * Return the major exception code.
106 * @return the major code
107 */
108 public int getMajor() {
109 return major;
110 }
111
112 /**
113 * Return the minor exception code
114 * @return the minor code
115 */
116 public int getMinor() {
117 return minor;
118 }
119
120 /**
121 * Retrieve the description of the exception
122 * @return the description;
123 */
124 public String getDescription() {
125 return subject;
126 }
127
128 /**
129 * Retrieve the subject of the exception
130 * @return the subject
131 */
132 public String getSubject() {
133 return description;
134 }
135
136 /**
137 * Retrieve the list of error elements
138 * @return the list of error elements
139 */
140 public List getErrorElements() {
141 return elements;
142 }
143
144 /**
145 * Add an error element to this descriptor. In this case an error element
146 * may be a validation error that was encounterd when validing input, or
147 * similar
148 * @param toAdd the element to add
149 */
150 public void addErrorElement(String toAdd) {
151 elements.add(toAdd);
152 }
153
154 /**
155 * Does this instance have any error elements assigned to it.
156 * @return true is error elements have been assigned.
157 */
158 public boolean hasErrorElements() {
159 return !elements.isEmpty();
160 }
161 }