1 | /* This file is part of the project "Hilbert II" - http://www.qedeq.org |
2 | * |
3 | * Copyright 2000-2014, Michael Meyling <mime@qedeq.org>. |
4 | * |
5 | * "Hilbert II" is free software; you can redistribute |
6 | * it and/or modify it under the terms of the GNU General Public |
7 | * License as published by the Free Software Foundation; either |
8 | * version 2 of the License, or (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | */ |
15 | |
16 | package org.qedeq.kernel.se.common; |
17 | |
18 | import org.qedeq.base.utility.EqualsUtility; |
19 | |
20 | /** |
21 | * Data validation error for a QEDEQ module. An error has always a reference to its |
22 | * location. Maybe an additional reference for another location is provided. |
23 | * <br/> |
24 | * An error code, message and location should fix a certain data validation error, so only these |
25 | * informations are used for {@link #hashCode()} and {@link #equals(Object)}. |
26 | * |
27 | * @author Michael Meyling |
28 | */ |
29 | public abstract class ModuleDataException extends QedeqException { |
30 | |
31 | /** Error location. */ |
32 | private final ModuleContext context; |
33 | |
34 | /** Reference location to explain the error. */ |
35 | private final ModuleContext referenceContext; |
36 | |
37 | /** |
38 | * Constructor. |
39 | * |
40 | * @param errorCode Error code of this message. |
41 | * @param message Error message. |
42 | * @param context Error location. |
43 | * @param referenceContext Reference location. |
44 | * @param cause Detailed exception information. |
45 | */ |
46 | public ModuleDataException(final int errorCode, final String message, |
47 | final ModuleContext context, final ModuleContext referenceContext, |
48 | final Exception cause) { |
49 | super(errorCode, message, cause); |
50 | // use copy constructor |
51 | this.context = (context == null ? null : new ModuleContext(context)); |
52 | this.referenceContext = (referenceContext == null ? null |
53 | : new ModuleContext(referenceContext)); |
54 | } |
55 | |
56 | /** |
57 | * Constructor. |
58 | * |
59 | * @param errorCode Error code of this message. |
60 | * @param message Error message. |
61 | * @param context Error location. |
62 | * @param referenceContext Reference location. |
63 | */ |
64 | public ModuleDataException(final int errorCode, final String message, |
65 | final ModuleContext context, final ModuleContext referenceContext) { |
66 | super(errorCode, message); |
67 | // use copy constructor |
68 | this.context = (context == null ? null : new ModuleContext(context)); |
69 | this.referenceContext = (referenceContext == null ? null |
70 | : new ModuleContext(referenceContext)); |
71 | } |
72 | |
73 | /** |
74 | * Constructor. |
75 | * |
76 | * @param errorCode Error code of this message. |
77 | * @param message Error message. |
78 | * @param context Error location. |
79 | * @param cause Detailed exception information. |
80 | */ |
81 | public ModuleDataException(final int errorCode, final String message, |
82 | final ModuleContext context, final Exception cause) { |
83 | super(errorCode, message, cause); |
84 | // use copy constructor |
85 | this.context = (context == null ? null : new ModuleContext(context)); |
86 | this.referenceContext = null; |
87 | } |
88 | |
89 | /** |
90 | * Constructor. Copies module context, so original can be changed. |
91 | * |
92 | * @param errorCode Error code of this message. |
93 | * @param message Error message. |
94 | * @param context Error location. |
95 | */ |
96 | public ModuleDataException(final int errorCode, final String message, |
97 | final ModuleContext context) { |
98 | super(errorCode, message); |
99 | // use copy constructor |
100 | this.context = (context == null ? null : new ModuleContext(context)); |
101 | this.referenceContext = null; |
102 | } |
103 | |
104 | /** |
105 | * Get context information about error location. |
106 | * |
107 | * @return Error location context. |
108 | */ |
109 | public final ModuleContext getContext() { |
110 | return context; |
111 | } |
112 | |
113 | /** |
114 | * Get additional context information about another associated location. |
115 | * |
116 | * @return Additional error location context. |
117 | */ |
118 | public final ModuleContext getReferenceContext() { |
119 | return referenceContext; |
120 | } |
121 | |
122 | public final int hashCode() { |
123 | return getErrorCode() ^ context.hashCode() ^ getMessage().hashCode(); |
124 | } |
125 | |
126 | public final boolean equals(final Object obj) { |
127 | if (!(obj instanceof ModuleDataException)) { |
128 | return false; |
129 | } |
130 | final ModuleDataException other = (ModuleDataException) obj; |
131 | return (getErrorCode() == other.getErrorCode()) |
132 | && EqualsUtility.equals(getMessage(), other.getMessage()) |
133 | && EqualsUtility.equals(context, other.context); |
134 | } |
135 | |
136 | |
137 | } |