ModuleContext.java
001 /* This file is part of the project "Hilbert II" - http://www.qedeq.org
002  *
003  * Copyright 2000-2011,  Michael Meyling <mime@qedeq.org>.
004  *
005  * "Hilbert II" is free software; you can redistribute
006  * it and/or modify it under the terms of the GNU General Public
007  * License as published by the Free Software Foundation; either
008  * version 2 of the License, or (at your option) any later version.
009  *
010  * This program is distributed in the hope that it will be useful,
011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013  * GNU General Public License for more details.
014  */
015 
016 package org.qedeq.kernel.se.common;
017 
018 import org.qedeq.base.io.SourcePosition;
019 import org.qedeq.base.trace.Trace;
020 import org.qedeq.base.utility.EqualsUtility;
021 
022 
023 /**
024  * Define context for an instance of {@link org.qedeq.kernel.se.base.module.Qedeq}.
025  * It consists of a location information: where is this module located.
026  * Also the location within the {@link org.qedeq.kernel.se.base.module.Qedeq} object
027  * should be described in an XPath like manner.
028  <p>
029  * The idea behind this context is a caller perspective. The caller sets the
030  * context (at least the module location information) and if the called method
031  * throws an exception a try/catch block can retrieve the context information.
032  *
033  @author  Michael Meyling
034  */
035 public class ModuleContext {
036 
037     /** This class. */
038     private static final Class CLASS = ModuleContext.class;
039 
040     /** Module location. */
041     private ModuleAddress moduleLocation;
042 
043     /** Location within the module. */
044     private String locationWithinModule;
045 
046     /** Skip position (relative to location start). Could be <code>null</code>. */
047     private final SourcePosition startDelta;
048 
049     /** Mark until this column (relative to location start). Could be <code>null</code>. */
050     private final SourcePosition endDelta;
051 
052     /**
053      * Constructor.
054      *
055      @param   moduleLocation          Module location information. Must not be <code>null</code>.
056      @param   locationWithinModule    Location within module. Must not be <code>null</code>.
057      @param   startDelta              Skip position (relative to location start). Could be
058      *                                  <code>null</code>.
059      @param   endDelta                Mark until this column (relative to location start). Could
060      *                                  be <code>null</code>.
061      @throws  NullPointerException    At least one parameter is null.
062      @throws  IllegalArgumentException    One parameter is below its allowed minimum.
063      */
064     public ModuleContext(final ModuleAddress moduleLocation, final String locationWithinModule,
065             final SourcePosition startDelta, final SourcePosition endDelta) {
066         if (moduleLocation == null) {
067             throw new NullPointerException("module adress should not be null");
068         }
069         if (locationWithinModule == null) {
070             throw new NullPointerException("location within module should not be null");
071         }
072         this.moduleLocation = moduleLocation;
073         this.locationWithinModule = locationWithinModule;
074         this.startDelta = startDelta;
075         this.endDelta = endDelta;
076     }
077 
078     /**
079      * Constructor.
080      *
081      @param   moduleLocation  Module location information. Must not be <code>null</code>.
082      @param   locationWithinModule    Location within module. Must not be <code>null</code>.
083      @throws  NullPointerException At least one parameter is null.
084      */
085     public ModuleContext(final ModuleAddress moduleLocation, final String locationWithinModule) {
086         this(moduleLocation, locationWithinModule, null, null);
087     }
088 
089     /**
090      * Constructor.
091      *
092      @param   moduleLocation  Module location information.
093      */
094     public ModuleContext(final ModuleAddress moduleLocation) {
095         this(moduleLocation, "");
096     }
097 
098     /**
099      * 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 = (ModuleContextobj;
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 }