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