Java Code Examples for java.util.Objects#checkFromToIndex()

The following examples show how to use java.util.Objects#checkFromToIndex() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Grapheme.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/**
 * Look for the next extended grapheme cluster boundary in a CharSequence. It assumes
 * the start of the char sequence is a boundary.
 * <p>
 * See Unicode Standard Annex #29 Unicode Text Segmentation for the specification
 * for the extended grapheme cluster boundary rules. The following implementation
 * is based on version 12.0 of the annex.
 * (http://www.unicode.org/reports/tr29/tr29-35.html)
 *
 * @param src the {@code CharSequence} to be scanned
 * @param off offset to start looking for the next boundary in the src
 * @param limit limit offset in the src (exclusive)
 * @return the next possible boundary
 */
static int nextBoundary(CharSequence src, int off, int limit) {
    Objects.checkFromToIndex(off, limit, src.length());

    int ch0 = Character.codePointAt(src, 0);
    int ret = Character.charCount(ch0);
    int ch1;
    // indicates whether gb11 or gb12 is underway
    int t0 = getGraphemeType(ch0);
    int riCount = t0 == RI ? 1 : 0;
    boolean gb11 = t0 == EXTENDED_PICTOGRAPHIC;
    while (ret < limit) {
        ch1 = Character.codePointAt(src, ret);
        int t1 = getGraphemeType(ch1);

        if (gb11 && t0 == ZWJ && t1 == EXTENDED_PICTOGRAPHIC) {
            gb11 = false;
        } else if (riCount % 2 == 1 && t0 == RI && t1 == RI) {
            // continue for gb12
        } else if (rules[t0][t1]) {
            if (ret > off) {
                break;
            } else {
                gb11 = t1 == EXTENDED_PICTOGRAPHIC;
                riCount = 0;
            }
        }

        riCount += (t1 == RI) ? 1 : 0;
        t0 = t1;

        ret += Character.charCount(ch1);
    }
    return ret;
}
 
Example 2
Source File: StringCharBuffer.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
StringCharBuffer(CharSequence s, int start, int end) { // package-private
    super(-1, start, end, s.length(), null);
    int n = s.length();
    Objects.checkFromToIndex(start, end, n);
    str = s;
    this.isReadOnly = true;
}
 
Example 3
Source File: BlockTreeTermsWriter.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private boolean allEqual(byte[] b, int startOffset, int endOffset, byte value) {
  Objects.checkFromToIndex(startOffset, endOffset, b.length);
  for (int i = startOffset; i < endOffset; ++i) {
    if (b[i] != value) {
      return false;
    }
  }
  return true;
}
 
Example 4
Source File: CharTermAttributeImpl.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
public final CharTermAttribute append(CharSequence csq, int start, int end) {
  if (csq == null) // needed for Appendable compliance
    csq = "null";
  // TODO: the optimized cases (jdk methods) will already do such checks, maybe re-organize this?
  Objects.checkFromToIndex(start, end, csq.length());
  final int len = end - start;
  if (len == 0)
    return this;
  resizeBuffer(termLength + len);
  if (len > 4) { // only use instanceof check series for longer CSQs, else simply iterate
    if (csq instanceof String) {
      ((String) csq).getChars(start, end, termBuffer, termLength);
    } else if (csq instanceof StringBuilder) {
      ((StringBuilder) csq).getChars(start, end, termBuffer, termLength);
    } else if (csq instanceof CharTermAttribute) {
      System.arraycopy(((CharTermAttribute) csq).buffer(), start, termBuffer, termLength, len);
    } else if (csq instanceof CharBuffer && ((CharBuffer) csq).hasArray()) {
      final CharBuffer cb = (CharBuffer) csq;
      System.arraycopy(cb.array(), cb.arrayOffset() + cb.position() + start, termBuffer, termLength, len);
    } else if (csq instanceof StringBuffer) {
      ((StringBuffer) csq).getChars(start, end, termBuffer, termLength);
    } else {
      while (start < end)
        termBuffer[termLength++] = csq.charAt(start++);
      // no fall-through here, as termLength is updated!
      return this;
    }
    termLength += len;
    return this;
  } else {
    while (start < end)
      termBuffer[termLength++] = csq.charAt(start++);
    return this;
  }
}
 
Example 5
Source File: Function.java    From Java-Coding-Problems with MIT License 4 votes vote down vote up
public int yMinusX(int x, int y) {
    Objects.checkFromToIndex(x, y, n);

    return y - x;
}
 
Example 6
Source File: CharTermAttributeImpl.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public final CharSequence subSequence(final int start, final int end) {
  Objects.checkFromToIndex(start, end, termLength);
  return new String(termBuffer, start, end - start);
}
 
Example 7
Source File: CharsRef.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Override
public CharSequence subSequence(int start, int end) {
  // NOTE: must do a real check here to meet the specs of CharSequence
  Objects.checkFromToIndex(start, end, length);
  return new CharsRef(chars, offset + start, end - start);
}
 
Example 8
Source File: Java9ObjectsAPIUnitTest.java    From tutorials with MIT License 4 votes vote down vote up
@Test(expected = IndexOutOfBoundsException.class)
public void givenInvalidSubRange_whenCheckFromToIndex_thenException(){
    int length = 6;
    Objects.checkFromToIndex(2,7,length);
}