Example #1
0
  /**
   * Overlays this fragment list by some {@link Fragment}. That means that parts that overlap are
   * taken from the fragment passed.
   *
   * @param fragment The fragment.
   * @return A fragment list storing the overlay result.
   */
  public FragmentList overlayBy(final Fragment fragment) {
    final FragmentList result = new FragmentList();
    try {
      result.addFragment(fragment);
    } catch (final IncompatibleFragmentException e) {
      throw new Error(e);
    }

    final PositionInText posTo = fragment.getTo();
    final ListIterator<Fragment> it = this.fragments.listIterator();
    while (it.hasNext()) {
      final Fragment oldFragment = it.next();
      if (posTo.lessThan(oldFragment.getFrom())) {
        break;
      }

      final ListIterator<Fragment> fIt = result.fragments.listIterator();
      while (fIt.hasNext()) {
        final Fragment f = fIt.next();
        final FragmentList rest = f.subtract(oldFragment);
        fIt.remove();
        for (final Fragment newFragment : rest.fragments) {
          fIt.add(newFragment);
        }
      }
    }

    try {
      result.addFragmentList(this);
    } catch (final IncompatibleFragmentException e) {
      throw new Error(e);
    }

    return result;
  }
Example #2
0
 /**
  * Adds a fragment to the fragment list.
  *
  * @param fragment The fragment to add.
  * @throws IncompatibleFragmentException if the fragment overlaps some other fragment in this
  *     fragment list.
  */
 public void addFragment(final Fragment fragment) throws IncompatibleFragmentException {
   final PositionInText posTo = fragment.getTo();
   final ListIterator<Fragment> it = this.fragments.listIterator();
   while (it.hasNext()) {
     final Fragment oldFragment = it.next();
     if (oldFragment.overlaps(fragment)) {
       throw new IncompatibleFragmentException();
     } else if (posTo.compareTo(oldFragment.getFrom()) < 0) {
       it.previous();
       it.add(fragment);
       return;
     }
   }
   this.fragments.add(fragment);
 }