| /******************************************************************************* |
| * Copyright (c) 2000, 2018 IBM Corporation and others. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v. 2.0 which is available at |
| * http://www.eclipse.org/legal/epl-2.0. |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.dltk.internal.ui.text; |
| |
| import java.text.CharacterIterator; |
| |
| import org.eclipse.core.runtime.Assert; |
| |
| /** |
| * A <code>CharSequence</code> based implementation of |
| * <code>CharacterIterator</code>. |
| */ |
| public class SequenceCharacterIterator implements CharacterIterator { |
| |
| private int fIndex = -1; |
| private final CharSequence fSequence; |
| private final int fFirst; |
| private final int fLast; |
| |
| private void invariant() { |
| Assert.isTrue(fIndex >= fFirst); |
| Assert.isTrue(fIndex <= fLast); |
| } |
| |
| /** |
| * Creates an iterator for the entire sequence. |
| * |
| * @param sequence |
| * the sequence backing this iterator |
| */ |
| public SequenceCharacterIterator(CharSequence sequence) { |
| this(sequence, 0); |
| } |
| |
| /** |
| * Creates an iterator. |
| * |
| * @param sequence |
| * the sequence backing this iterator |
| * @param first |
| * the first character to consider |
| * @throws IllegalArgumentException |
| * if the indices are out of bounds |
| */ |
| public SequenceCharacterIterator(CharSequence sequence, int first) |
| throws IllegalArgumentException { |
| this(sequence, first, sequence.length()); |
| } |
| |
| /** |
| * Creates an iterator. |
| * |
| * @param sequence |
| * the sequence backing this iterator |
| * @param first |
| * the first character to consider |
| * @param last |
| * the last character index to consider |
| * @throws IllegalArgumentException |
| * if the indices are out of bounds |
| */ |
| public SequenceCharacterIterator(CharSequence sequence, int first, int last) |
| throws IllegalArgumentException { |
| if (sequence == null) |
| throw new NullPointerException(); |
| if (first < 0 || first > last) |
| throw new IllegalArgumentException(); |
| if (last > sequence.length()) |
| throw new IllegalArgumentException(); |
| fSequence = sequence; |
| fFirst = first; |
| fLast = last; |
| fIndex = first; |
| invariant(); |
| } |
| |
| @Override |
| public char first() { |
| return setIndex(getBeginIndex()); |
| } |
| |
| @Override |
| public char last() { |
| if (fFirst == fLast) { |
| return setIndex(getEndIndex()); |
| } |
| return setIndex(getEndIndex() - 1); |
| } |
| |
| @Override |
| public char current() { |
| if (fIndex >= fFirst && fIndex < fLast) { |
| return fSequence.charAt(fIndex); |
| } |
| return DONE; |
| } |
| |
| @Override |
| public char next() { |
| return setIndex(Math.min(fIndex + 1, getEndIndex())); |
| } |
| |
| @Override |
| public char previous() { |
| if (fIndex > getBeginIndex()) { |
| return setIndex(fIndex - 1); |
| } |
| return DONE; |
| } |
| |
| @Override |
| public char setIndex(int position) { |
| if (position >= getBeginIndex() && position <= getEndIndex()) |
| fIndex = position; |
| else |
| throw new IllegalArgumentException(); |
| |
| invariant(); |
| return current(); |
| } |
| |
| @Override |
| public int getBeginIndex() { |
| return fFirst; |
| } |
| |
| @Override |
| public int getEndIndex() { |
| return fLast; |
| } |
| |
| @Override |
| public int getIndex() { |
| return fIndex; |
| } |
| |
| @Override |
| public Object clone() { |
| try { |
| return super.clone(); |
| } catch (CloneNotSupportedException e) { |
| throw new InternalError(); |
| } |
| } |
| } |