/** * 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; }
/** * Subtracts some {@link Fragment} from this fragment list. * * @param fragment The fragment to subtract. * @return A list of remaining fragments. */ public FragmentList subtract(final Fragment fragment) { final FragmentList result = new FragmentList(); try { for (final Fragment oldFragment : this.fragments) { if (oldFragment.overlaps(fragment)) { result.addFragmentList(oldFragment.subtract(fragment)); } else { result.addFragment(oldFragment); } } } catch (final IncompatibleFragmentException e) { throw new Error(e); } return result; }