001: /*
002: * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/ParserCursor.java $
003: * $Revision:589325 $
004: * $Date:2007-10-28 11:37:56 +0100 (Sun, 28 Oct 2007) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.message;
033:
034: import org.apache.http.util.CharArrayBuffer;
035:
036: /**
037: * This class represents a context of a parsing operation:
038: * <ul>
039: * <li>the current position the parsing operation is expected to start at</li>
040: * <li>the bounds limiting the scope of the parsing operation</li>
041: * </ul>
042: *
043: * @author <a href="mailto:oleg at ural.com">Oleg Kalnichevski</a>
044: */
045: public class ParserCursor {
046:
047: private final int lowerBound;
048: private final int upperBound;
049: private int pos;
050:
051: public ParserCursor(int lowerBound, int upperBound) {
052: super ();
053: if (lowerBound < 0) {
054: throw new IndexOutOfBoundsException(
055: "Lower bound cannot be negative");
056: }
057: if (lowerBound > upperBound) {
058: throw new IndexOutOfBoundsException(
059: "Lower bound cannot be greater then upper bound");
060: }
061: this .lowerBound = lowerBound;
062: this .upperBound = upperBound;
063: this .pos = lowerBound;
064: }
065:
066: public int getLowerBound() {
067: return this .lowerBound;
068: }
069:
070: public int getUpperBound() {
071: return this .upperBound;
072: }
073:
074: public int getPos() {
075: return this .pos;
076: }
077:
078: public void updatePos(int pos) {
079: if (pos < this .lowerBound) {
080: throw new IndexOutOfBoundsException();
081: }
082: if (pos > this .upperBound) {
083: throw new IndexOutOfBoundsException();
084: }
085: this .pos = pos;
086: }
087:
088: public boolean atEnd() {
089: return this .pos >= this .upperBound;
090: }
091:
092: public String toString() {
093: CharArrayBuffer buffer = new CharArrayBuffer(16);
094: buffer.append('[');
095: buffer.append(Integer.toString(this .lowerBound));
096: buffer.append('>');
097: buffer.append(Integer.toString(this .pos));
098: buffer.append('>');
099: buffer.append(Integer.toString(this .upperBound));
100: buffer.append(']');
101: return buffer.toString();
102: }
103:
104: }
|