Пример #1
0
 private static void checkIsBoundaryException(BreakIterator bi, int offset) {
   try {
     bi.isBoundary(offset);
   } catch (IllegalArgumentException e) {
     return; // OK
   }
   throw new RuntimeException(bi + ": isBoundary() doesn't throw an IAE with offset " + offset);
 }
Пример #2
0
  private void makeLayoutWindow(int localStart) {

    int compStart = localStart;
    int compLimit = fChars.length;

    // If we've already gone past the layout window, format to end of paragraph
    if (layoutCount > 0 && !haveLayoutWindow) {
      float avgLineLength = Math.max(layoutCharCount / layoutCount, 1);
      compLimit = Math.min(localStart + (int) (avgLineLength * EST_LINES), fChars.length);
    }

    if (localStart > 0 || compLimit < fChars.length) {
      if (charIter == null) {
        charIter = new CharArrayIterator(fChars);
      } else {
        charIter.reset(fChars);
      }
      if (fLineBreak == null) {
        fLineBreak = BreakIterator.getLineInstance();
      }
      fLineBreak.setText(charIter);
      if (localStart > 0) {
        if (!fLineBreak.isBoundary(localStart)) {
          compStart = fLineBreak.preceding(localStart);
        }
      }
      if (compLimit < fChars.length) {
        if (!fLineBreak.isBoundary(compLimit)) {
          compLimit = fLineBreak.following(compLimit);
        }
      }
    }

    ensureComponents(compStart, compLimit);
    haveLayoutWindow = true;
  }
Пример #3
0
  /**
   * If <code>offset</code> is within a word, returns the index of the last character of that word
   * plus one, otherwise returns BreakIterator.DONE.
   *
   * <p>The offsets that are considered to be part of a word are the indexes of its characters,
   * <i>as well as</i> the index of its last character plus one. If offset is the index of a low
   * surrogate character, BreakIterator.DONE will be returned.
   *
   * <p>Valid range for offset is [0..textLength] (note the inclusive upper bound). The returned
   * value is within [offset..textLength] or BreakIterator.DONE.
   *
   * @throws IllegalArgumentException is offset is not valid.
   */
  public int getEnd(int offset) {
    final int shiftedOffset = offset - mOffsetShift;
    checkOffsetIsValid(shiftedOffset);

    if (isAfterLetterOrDigit(shiftedOffset)) {
      if (mIterator.isBoundary(shiftedOffset)) {
        return shiftedOffset + mOffsetShift;
      } else {
        return mIterator.following(shiftedOffset) + mOffsetShift;
      }
    } else {
      if (isOnLetterOrDigit(shiftedOffset)) {
        return mIterator.following(shiftedOffset) + mOffsetShift;
      }
    }
    return BreakIterator.DONE;
  }
Пример #4
0
  public static void main(String[] args) {
    BreakIterator bi = BreakIterator.getWordInstance();
    bi.setText(text);
    MirroredBreakIterator mirror = new MirroredBreakIterator(bi);
    final int first = bi.first();
    if (first != 0) {
      throw new RuntimeException("first != 0: " + first);
    }
    final int last = bi.last();
    bi = BreakIterator.getWordInstance();
    bi.setText(text);
    int length = text.length();

    /*
     * following(int)
     */
    for (int i = 0; i <= length; i++) {
      if (i == length) {
        check(bi.following(i), DONE);
      }
      check(bi.following(i), mirror.following(i));
      check(bi.current(), mirror.current());
    }
    for (int i = -length; i < 0; i++) {
      checkFollowingException(bi, i);
      checkFollowingException(mirror, i);
      check(bi.current(), mirror.current());
    }
    for (int i = 1; i < length; i++) {
      checkFollowingException(bi, length + i);
      checkFollowingException(mirror, length + i);
      check(bi.current(), mirror.current());
    }

    /*
     * preceding(int)
     */
    for (int i = length; i >= 0; i--) {
      if (i == 0) {
        check(bi.preceding(i), DONE);
      }
      check(bi.preceding(i), mirror.preceding(i));
      check(bi.current(), mirror.current());
    }
    for (int i = -length; i < 0; i++) {
      checkPrecedingException(bi, i);
      checkPrecedingException(mirror, i);
      check(bi.current(), mirror.current());
    }
    for (int i = 1; i < length; i++) {
      checkPrecedingException(bi, length + i);
      checkPrecedingException(mirror, length + i);
      check(bi.current(), mirror.current());
    }

    /*
     * isBoundary(int)
     */
    for (int i = 0; i <= length; i++) {
      check(bi.isBoundary(i), mirror.isBoundary(i));
      check(bi.current(), mirror.current());
    }
    for (int i = -length; i < 0; i++) {
      checkIsBoundaryException(bi, i);
      checkIsBoundaryException(mirror, i);
    }
    for (int i = 1; i < length; i++) {
      checkIsBoundaryException(bi, length + i);
      checkIsBoundaryException(mirror, length + i);
    }
  }
 /** {@inheritDoc} */
 public boolean isBoundary(int offset) {
   int shiftedOffset = offset - mOffsetShift;
   checkOffsetIsValid(shiftedOffset);
   return mIterator.isBoundary(shiftedOffset);
 }