Package org.jamesii.core.util

Examples of org.jamesii.core.util.CharSequenceCharacterIterator


    // next() shouldn't change the index when running off the end.
    assertEquals(endIndex, it.getIndex());
  }

  public void testPrevious() {
    it = new CharSequenceCharacterIterator(seq);
    it.last();
    int lastIndex = it.getIndex();
    assertEquals(seq.charAt(lastIndex), it.current());
    assertEquals(seq.charAt(lastIndex - 1), it.previous());
    assertEquals(lastIndex - 1, it.getIndex());
View Full Code Here


    assertEquals(0, it.getIndex());
  }

  public void testSetIndex() {
    int[] indexes = new int[] { 0, 1, 2, 3, 4, 5, 6 };
    it = new CharSequenceCharacterIterator(seq);
    for (int index : indexes) {
      assertEquals(seq.charAt(index), it.setIndex(index));
      assertEquals(index, it.getIndex());
    }
View Full Code Here

    assertEquals(CharacterIterator.DONE, it.setIndex(it.getEndIndex()));
    assertEquals(it.getEndIndex(), it.getIndex());
  }

  public void testSetIndexExceptions() {
    it = new CharSequenceCharacterIterator(seq);
    try {
      it.setIndex(-1);
      fail();
    } catch (IllegalArgumentException e) {
    }
View Full Code Here

  public void testGetBeginIndex() {
    int[] beginIndexes = new int[] { 0, 1, 2, 3, 4, 5, 6 };
    for (int beginIndex : beginIndexes) {
      it =
          new CharSequenceCharacterIterator(seq, beginIndex, seq.length(),
              beginIndex);
      assertEquals(beginIndex, it.getBeginIndex());
    }
  }
View Full Code Here

  }

  public void testGetEndIndex() {
    int[] endIndexes = new int[] { 1, 2, 3, 4, 5, 6 };
    for (int endIndex : endIndexes) {
      it = new CharSequenceCharacterIterator(seq, 0, endIndex, 0);
      assertEquals(endIndex, it.getEndIndex());
    }
  }
View Full Code Here

      assertEquals(endIndex, it.getEndIndex());
    }
  }

  public void testGetIndex() {
    it = new CharSequenceCharacterIterator(seq);
    assertEquals(0, it.getIndex());
    it.next();
    assertEquals(1, it.getIndex());
    it.next();
    assertEquals(2, it.getIndex());
View Full Code Here

  public void testClone() {
    for (int beginIndex : new int[] { 0, 1, 2 }) {
      for (int endIndex : new int[] { 4, 5, 6 }) {
        // Try it with an iterator from start to finish.
        it =
            new CharSequenceCharacterIterator(seq, beginIndex, endIndex,
                beginIndex + 1);

        // Clone the iterator, should have the same sequence, the same begin and
        // end positions, the same index.
        CharSequenceCharacterIterator it2 =
            (CharSequenceCharacterIterator) it.clone();
        CharSequenceCharacterIterator it3 =
            (CharSequenceCharacterIterator) it.clone();

        assertEquals(it.getBeginIndex(), it2.getBeginIndex());
        assertEquals(it.getEndIndex(), it2.getEndIndex());
        assertEquals(it.getIndex(), it2.getIndex());

        while (it.next() != CharacterIterator.DONE) {
          assertEquals(it.current(), it2.next());
        }

        // The second clone shouldn't have changed.
        assertEquals(it.getBeginIndex(), it3.getBeginIndex());
        assertEquals(it.getEndIndex(), it3.getEndIndex());
        assertEquals(it.getBeginIndex() + 1, it3.getIndex());
      }
    }
  }
View Full Code Here

   *           (since it operates on single lines of input).
   *           </ul>
   */
  public String[] parse(CharSequence line) {
    List<String> tokens = new ArrayList<>();
    CharacterIterator ci = new CharSequenceCharacterIterator(line);
    StringBuilder tok = new StringBuilder();
    State state = State.BEGIN;
    loop: for (char c = ci.first();; c = ci.next()) {
      switch (c) {
      case '"':
        switch (state) {
        case BEGIN:
          // start of a quoted string
          state = State.QUOTEDSTRING;
          break;
        case QUOTEDSTRING:
          // either end of the quoted string or start of a quoted
          // quote
          char c2 = ci.next();
          if (c2 == '"') {
            // double double-quotes – a quoted double-quote
            tok.append('"');
          } else if (c2 == CharacterIterator.DONE || c2 == delimiter) {
            // terminating quote for the quoted string
            tokens.add(tok.toString());
            tok = new StringBuilder();
            state = State.BEGIN;
            // avoid running the main loop body over the DONE (which would add
            // the now empty token to the list of tokens although no token
            // started)
            if (c2 == CharacterIterator.DONE) {
              break loop;
            }
          } else {
            throw new CSVFormatException(
                "Data encountered outside a quoted string.", ci.getIndex());
          }
          break;
        case STRING:
          // stray double-quote in the middle of an unquoted string
          throw new CSVFormatException(
              "Stray double-quote within unquoted field", ci.getIndex());
        default:
        }
        break;
      case CharacterIterator.DONE:
        if (state == State.QUOTEDSTRING) {
          // end of string in the middle of a quoted string – not
          // good
          throw new CSVFormatException(
              "Encountered end of line in a quoted string.", ci.getIndex());
        }
        tokens.add(tok.toString());
        break loop;
      default:
        if (c == getDelimiter()) {
View Full Code Here

TOP

Related Classes of org.jamesii.core.util.CharSequenceCharacterIterator

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.