1 /* This file is part of the project "Hilbert II" - http://www.qedeq.org" target="alexandria_uri">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.io.SourcePosition;
19 import org.qedeq.base.trace.Trace;
20 import org.qedeq.base.utility.EqualsUtility;
21
22
23 /**
24 * Define context for an instance of {@link org.qedeq.kernel.se.base.module.Qedeq}.
25 * It consists of a location information: where is this module located.
26 * Also the location within the {@link org.qedeq.kernel.se.base.module.Qedeq} object
27 * should be described in an XPath like manner.
28 * <p>
29 * The idea behind this context is a caller perspective. The caller sets the
30 * context (at least the module location information) and if the called method
31 * throws an exception a try/catch block can retrieve the context information.
32 *
33 * @author Michael Meyling
34 */
35 public class ModuleContext {
36
37 /** This class. */
38 private static final Class CLASS = ModuleContext.class;
39
40 /** Module location. */
41 private ModuleAddress moduleLocation;
42
43 /** Location within the module. */
44 private String locationWithinModule;
45
46 /** Skip position (relative to location start). Could be <code>null</code>. */
47 private final SourcePosition startDelta;
48
49 /** Mark until this column (relative to location start). Could be <code>null</code>. */
50 private final SourcePosition endDelta;
51
52 /**
53 * Constructor.
54 *
55 * @param moduleLocation Module location information. Must not be <code>null</code>.
56 * @param locationWithinModule Location within module. Must not be <code>null</code>.
57 * @param startDelta Skip position (relative to location start). Could be
58 * <code>null</code>.
59 * @param endDelta Mark until this column (relative to location start). Could
60 * be <code>null</code>.
61 * @throws NullPointerException At least one parameter is null.
62 * @throws IllegalArgumentException One parameter is below its allowed minimum.
63 */
64 public ModuleContext(final ModuleAddress moduleLocation, final String locationWithinModule,
65 final SourcePosition startDelta, final SourcePosition endDelta) {
66 if (moduleLocation == null) {
67 throw new NullPointerException("module adress should not be null");
68 }
69 if (locationWithinModule == null) {
70 throw new NullPointerException("location within module should not be null");
71 }
72 this.moduleLocation = moduleLocation;
73 this.locationWithinModule = locationWithinModule;
74 this.startDelta = startDelta;
75 this.endDelta = endDelta;
76 }
77
78 /**
79 * Constructor.
80 *
81 * @param moduleLocation Module location information. Must not be <code>null</code>.
82 * @param locationWithinModule Location within module. Must not be <code>null</code>.
83 * @throws NullPointerException At least one parameter is null.
84 */
85 public ModuleContext(final ModuleAddress moduleLocation, final String locationWithinModule) {
86 this(moduleLocation, locationWithinModule, null, null);
87 }
88
89 /**
90 * Constructor.
91 *
92 * @param moduleLocation Module location information.
93 */
94 public ModuleContext(final ModuleAddress moduleLocation) {
95 this(moduleLocation, "");
96 }
97
98 /**
99 * Copy constructor.
100 *
101 * @param original Original context.
102 */
103 public ModuleContext(final ModuleContext original) {
104 this(original.getModuleLocation(), original.getLocationWithinModule(),
105 original.getStartDelta(), original.getEndDelta());
106 }
107
108 /**
109 * Constructor.
110 *
111 * @param main Main context. Must not be <code>null</code>.
112 * @param moduleLocation Module location information. Must not be <code>null</code>.
113 */
114 public ModuleContext(final ModuleContext main, final String moduleLocation) {
115 this(main.getModuleLocation(), moduleLocation);
116 }
117
118 /**
119 * Get location information about module.
120 *
121 * @return Module location information.
122 */
123 public final ModuleAddress getModuleLocation() {
124 return moduleLocation;
125 }
126
127 /**
128 * Get location information where are we within the module.
129 *
130 * @return Location within module.
131 */
132 public final String getLocationWithinModule() {
133 return locationWithinModule;
134 }
135
136 /**
137 * Set location information where are we within the module.
138 *
139 * @param locationWithinModule Location within module.
140 */
141 public final void setLocationWithinModule(final String locationWithinModule) {
142 final String method = "setLocationWithinModule(String)";
143 this.locationWithinModule = locationWithinModule;
144 Trace.param(CLASS, this, method, "locationWithinModule", locationWithinModule);
145 }
146
147 /**
148 * Get delta position (relative to location start). This describes the precise
149 * location start.
150 * Could be <code>null</code>.
151 *
152 * @return Delta for precise location start.
153 */
154 public final SourcePosition getStartDelta() {
155 return startDelta;
156 }
157
158 /**
159 * Get delta position (relative to location start). This describes the precise
160 * location end.
161 * Could be <code>null</code>.
162 *
163 * @return Delta for precise location end.
164 */
165 public final SourcePosition getEndDelta() {
166 return endDelta;
167 }
168
169 public final int hashCode() {
170 return getModuleLocation().hashCode() ^ getLocationWithinModule().hashCode()
171 ^ (startDelta != null ? startDelta.hashCode() : 7)
172 ^ (endDelta != null ? endDelta.hashCode() : 11);
173 }
174
175 public final boolean equals(final Object obj) {
176 if (!(obj instanceof ModuleContext)) {
177 return false;
178 }
179 final ModuleContext other = (ModuleContext) obj;
180 return getModuleLocation().equals(other.getModuleLocation())
181 && getLocationWithinModule().equals(other.getLocationWithinModule())
182 && EqualsUtility.equals(startDelta, other.startDelta)
183 && EqualsUtility.equals(endDelta, other.endDelta);
184 }
185
186 public final String toString() {
187 return getModuleLocation() + ":" + getLocationWithinModule()
188 + ":" + startDelta + ":" + endDelta;
189 }
190
191 }