@@ -0,0 +1,2927 @@
+package com.bbn.nlp.corpora.OffsetMapping;%0A%0Aimport com.google.common.base.Optional;%0Aimport com.google.common.collect.ImmutableList;%0Aimport com.google.common.collect.ImmutableSet;%0Aimport com.google.common.collect.Range;%0Aimport com.google.common.collect.Sets;%0A%0Aimport java.util.Collection;%0Aimport java.util.List;%0Aimport java.util.Set;%0A%0Aimport static com.bbn.bue.common.collections.RangeUtils.isClosed;%0Aimport static com.google.common.base.Preconditions.checkArgument;%0Aimport static com.google.common.base.Preconditions.checkState;%0A%0A/**%0A * Default implementations of certain methods. They may not match precisely the semantics of your%0A * mapper.%0A */%0Apublic abstract class AbstractOffsetMapper implements OffsetMapper %7B%0A%0A  /**%0A   * @param closedSource - a range that is closed constructed as offset, offset + length - 1%0A   * @return the cartesian product of %7Bstart mappings%7D and %7Bend mappings%7D%0A   */%0A  @Override%0A  public Collection%3CRange%3CInteger%3E%3E mapRange(final Range%3CInteger%3E closedSource) %7B%0A    checkArgument(isClosed(closedSource));%0A    final ImmutableSet.Builder%3CRange%3CInteger%3E%3E resultRanges = ImmutableSet.builder();%0A    if (isEntirelyMapped(closedSource)) %7B%0A      Set%3CInteger%3E start = ImmutableSet.copyOf(mapOffset(closedSource.lowerEndpoint()));%0A      Set%3CInteger%3E end = ImmutableSet.copyOf(mapOffset(closedSource.upperEndpoint()));%0A      for (List%3CInteger%3E pair : Sets.cartesianProduct(start, end)) %7B%0A        resultRanges.add(Range.closed(pair.get(0), pair.get(1)));%0A      %7D%0A    %7D%0A    return resultRanges.build();%0A  %7D%0A%0A  /**%0A   * throws an Exception via checkState if there's more than one mapping%0A   *%0A   * @return the single mapping for a range, Optional.absent() if there are none%0A   */%0A  public Optional%3CRange%3CInteger%3E%3E mapRangeExactlyOnce(final Range%3CInteger%3E closedSource) %7B%0A    checkArgument(isClosed(closedSource));%0A    final List%3CRange%3CInteger%3E%3E results = ImmutableList.copyOf(mapRange(closedSource));%0A    checkState(results.size() %3C= 1);%0A    if (results.size() == 0) %7B%0A      return Optional.absent();%0A    %7D%0A    return Optional.of(results.get(0));%0A  %7D%0A%0A  /**%0A   * throws an Exception via checkState if there's more than one mapping%0A   *%0A   * @return the single mapping for this, if any%0A   */%0A  public Optional%3CInteger%3E mapOffsetExactlyOnce(final int source) %7B%0A    if (isMapped(source)) %7B%0A      final List%3CInteger%3E mappings = ImmutableList.copyOf(mapOffset(source));%0A      checkState(mappings.size() %3C= 1);%0A      if (mappings.size() == 0) %7B%0A        return Optional.absent();%0A      %7D%0A      return Optional.of(mappings.get(0));%0A    %7D%0A    return Optional.absent();%0A  %7D%0A%0A  /**%0A   * returns true unless an index in this range is unmapped%0A   */%0A  @Override%0A  public boolean isEntirelyMapped(final Range%3CInteger%3E source) %7B%0A    checkArgument(isClosed(source));%0A    for (int i = source.lowerEndpoint(); i %3C source.upperEndpoint(); i++) %7B%0A      if (!isMapped(i)) %7B%0A        return false;%0A      %7D%0A    %7D%0A    return true;%0A  %7D%0A%7D%0A

add default implementation of certain functionality via an AbstractOffsetMapper