litesoft
@ 942
litesoft / trunk / Java / core / Anywhere / src / org / litesoft / commonfoundation / charstreams / CharSourceFromSequence.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
// This Source Code is in the Public Domain per: http://litesoft.org/License.txt package org.litesoft.commonfoundation.charstreams; import org.litesoft.commonfoundation.annotations.*; import org.litesoft.commonfoundation.typeutils.*; public class CharSourceFromSequence implements CharSource { private String mSource; private int mFrom, mTo; public CharSourceFromSequence( @Nullable CharSequence pSource, int pFrom ) { mSource = Strings.deNull( pSource ); if ( (mFrom = pFrom) < 0 ) { throw new IllegalArgumentException( "'From' not allowed to be negative" ); } mTo = mSource.length(); } public CharSourceFromSequence( @Nullable CharSequence pSource ) { this( pSource, 0 ); } public CharSourceFromSequence( @Nullable CharSequence pSource, int pFrom, int pTo ) { this( pSource, pFrom ); if ( pTo > mTo ) { throw new IllegalArgumentException( "'To' not allowed to be greater than the length of the 'Source' string" ); } if ( pTo < 0 ) { mTo += pTo; } else { mTo = pTo; } } @Override public boolean anyRemaining() { return (mFrom < mTo); } @Override public int peek() { return anyRemaining() ? mSource.charAt( mFrom ) : -1; } @Override public int get() { return anyRemaining() ? mSource.charAt( mFrom++ ) : -1; } @Override public char getRequired() { if ( anyRemaining() ) { return mSource.charAt( mFrom++ ); } throw new IllegalArgumentException( "Unexpected EOD encountered at index " + mFrom + " in " + mSource ); } @Override public int getNextOffset() { return mFrom; } @Override public int getLastOffset() { return mFrom - 1; } @Override public String toString() { return mSource.substring( mFrom, mTo ); } } |