Home | History | Annotate | Download | only in message
      1 /*
      2  * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-main/src/main/java/org/apache/http/message/ParserCursor.java $
      3  * $Revision:589325 $
      4  * $Date:2007-10-28 11:37:56 +0100 (Sun, 28 Oct 2007) $
      5  *
      6  * ====================================================================
      7  * Licensed to the Apache Software Foundation (ASF) under one
      8  * or more contributor license agreements.  See the NOTICE file
      9  * distributed with this work for additional information
     10  * regarding copyright ownership.  The ASF licenses this file
     11  * to you under the Apache License, Version 2.0 (the
     12  * "License"); you may not use this file except in compliance
     13  * with the License.  You may obtain a copy of the License at
     14  *
     15  *   http://www.apache.org/licenses/LICENSE-2.0
     16  *
     17  * Unless required by applicable law or agreed to in writing,
     18  * software distributed under the License is distributed on an
     19  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
     20  * KIND, either express or implied.  See the License for the
     21  * specific language governing permissions and limitations
     22  * under the License.
     23  * ====================================================================
     24  *
     25  * This software consists of voluntary contributions made by many
     26  * individuals on behalf of the Apache Software Foundation.  For more
     27  * information on the Apache Software Foundation, please see
     28  * <http://www.apache.org/>.
     29  *
     30  */
     31 
     32 package org.apache.http.message;
     33 
     34 import org.apache.http.util.CharArrayBuffer;
     35 
     36 /**
     37  * This class represents a context of a parsing operation:
     38  * <ul>
     39  *  <li>the current position the parsing operation is expected to start at</li>
     40  *  <li>the bounds limiting the scope of the parsing operation</li>
     41  * </ul>
     42  *
     43  * @author <a href="mailto:oleg at ural.com">Oleg Kalnichevski</a>
     44  */
     45 public class ParserCursor {
     46 
     47     private final int lowerBound;
     48     private final int upperBound;
     49     private int pos;
     50 
     51     public ParserCursor(int lowerBound, int upperBound) {
     52         super();
     53         if (lowerBound < 0) {
     54             throw new IndexOutOfBoundsException("Lower bound cannot be negative");
     55         }
     56         if (lowerBound > upperBound) {
     57             throw new IndexOutOfBoundsException("Lower bound cannot be greater then upper bound");
     58         }
     59         this.lowerBound = lowerBound;
     60         this.upperBound = upperBound;
     61         this.pos = lowerBound;
     62     }
     63 
     64     public int getLowerBound() {
     65         return this.lowerBound;
     66     }
     67 
     68     public int getUpperBound() {
     69         return this.upperBound;
     70     }
     71 
     72     public int getPos() {
     73         return this.pos;
     74     }
     75 
     76     public void updatePos(int pos) {
     77         if (pos < this.lowerBound) {
     78             throw new IndexOutOfBoundsException();
     79         }
     80         if (pos > this.upperBound) {
     81             throw new IndexOutOfBoundsException();
     82         }
     83         this.pos = pos;
     84     }
     85 
     86     public boolean atEnd() {
     87         return this.pos >= this.upperBound;
     88     }
     89 
     90     public String toString() {
     91         CharArrayBuffer buffer = new CharArrayBuffer(16);
     92         buffer.append('[');
     93         buffer.append(Integer.toString(this.lowerBound));
     94         buffer.append('>');
     95         buffer.append(Integer.toString(this.pos));
     96         buffer.append('>');
     97         buffer.append(Integer.toString(this.upperBound));
     98         buffer.append(']');
     99         return buffer.toString();
    100     }
    101 
    102 }
    103