Class LocationMap<D,E>

java.lang.Object
eu.bandm.tools.location.LocationMap<D,E>
Type Parameters:
D - the document identifier type of the domain locations
E - the document identifier type of the range locations

public class LocationMap<D,E> extends Object
Facility to translate Locations. Is needed in case of re-arranging text fragments, and location info must be adjusted accordingly.

Eg. when fragents from a source text are concatenated into a StringBuilder, which is then subject to a lexical sanner, the scanner will produce error messages which must be re-mapped to the original context to become meaningful for the user.

All locations entered into this map must be of the "region" flavour (see Location) and be contained in a single line (see Location.isContiguous().

All locations must be entered pairwise, with identical lengths.

  • Field Details

    • leftlocs

      protected List<Location<D>> leftlocs
      Locations making up the translation's domain. List is in one-to-one-correspondance to rightlocs.
    • rightlocs

      protected List<Location<E>> rightlocs
      Locations making up the translation's range. List is in one-to-one-correspondance to leftlocs.
    • transparent

      protected boolean transparent
      Whether locatiosn not in the domain are delivered unmodified, as their own Translation. If not set, null is returned in this case.
    • mapLocations

      public final Function<Location<D>,Location<E>> mapLocations
      Total function, which translates null to null and maps every location according to the containing LocationMap.
  • Constructor Details

    • LocationMap

      public LocationMap()
      Construct a new instance.
  • Method Details

    • clear

      public void clear()
      Clear the map completely, in both directions.
    • put

      public void put(Location<D> left, Location<E> right)
      Establishes the mapping between both locations. left and right must be of atom type and must have equal length.
      Parameters:
      left - the region on the left side of the map
      right - the region on the right of the map
    • setTransparent

      public boolean setTransparent(boolean trans)
      Sets the transparent attribute and returns its old value. Id decides whether a location which is not in the domain is returned unmodified (as its own translation, in case transparent == true). Else null will be returned in this case. This is only sensible when type parameters D and E have the same value. Otherwise a type error will occur at runtime.
      Parameters:
      trans - the new value for transparent behaviour.
      Returns:
      the old value for transparent behaviour.
    • resolve

      @Opt public @Opt Location<E> resolve(Location<D> loc)
      Resolve a point or a region from the left-side regions to the right-side regions. If a point is not in a domain region return null (iff transparent not set) or the unmodified point.

      If only one end of a region is in a domain region, then return this end. If no end of a region is in a domain region, return null or the original, depending on transparent.

      Can currently only deal with points and intervals, not with sets.

      Parameters:
      loc - the Location to translate (from the left to the right side of the map.)
      Returns:
      the translated location, or null if not in domain.
    • resolveBackward

      @Opt public @Opt Location<D> resolveBackward(Location<E> loc)
      Resolve a point or a region from the right-side regions to the left-side regions. If a point is not in a domain region return null (iff transparent not set) or the unmodified point.

      If only one end of a region is in a domain region, then return this end. If no end of a region is in a domain region, return null or the original, depending on transparent.

      Can currently only deal with points and intervals, not with sets.

      Parameters:
      loc - the Location to translate (from the right to the left side of the map.)
      Returns:
      the translated location, or null if not in domain.
    • doResolve

      @Opt protected <P, Q> @Opt Location<Q> doResolve(List<Location<P>> from, List<Location<Q>> to, Location<P> loc)
      Resolve a point or a region. If a point is not in a domain region return null (iff transparent not set) or the unmodified point.

      If only one end of a region is in a domain region, then return this end. If no end of a region is in a domain region, return null or the original, depending on transparent.

      Type Parameters:
      P - the document type of the input location.
      Q - the document type of the target location.
      Parameters:
      from - the list of the input locations, must be in sync with the list of the output locations.
      to - the list of the output locations.
      loc - the Location to translate.
      Returns:
      the translated location, possibly null or the unchanged input.
    • resolvePoint

      @Opt protected static <P, Q> @Opt Location<Q> resolvePoint(List<Location<P>> from, List<Location<Q>> to, Location<P> loc)
      Translates a location point at offset p in domain region n to the point at the same offset in the range region n.
      Type Parameters:
      P - the document type of the input location.
      Q - the document type of the target location.
      Parameters:
      from - the list of the input locations, must be in sync with the list of the output locations.
      to - the list of the output locations.
      loc - the Location to translate.
      Returns:
      the translated location, possibly null if the input location is not contained in a domain region.
    • dump

      public void dump(PrintStream p)
      Auxiliary debugging routine. Prints all pairs of regions to the given output.
      Parameters:
      p - the drain of the outputs.