1 /*
   2  * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 /*
  26  * COMPONENT_NAME: idl.parser
  27  *
  28  * ORIGINS: 27
  29  *
  30  * Licensed Materials - Property of IBM
  31  * 5639-D57 (C) COPYRIGHT International Business Machines Corp. 1997, 1999
  32  * RMI-IIOP v1.0
  33  *
  34  */
  35 
  36 package com.sun.tools.corba.se.idl;
  37 
  38 // NOTES:
  39 
  40 class TokenBuffer
  41 {
  42   private final int DEFAULT_SIZE = 10;
  43 
  44   private int   _size      = 0;
  45   private Token _buffer [] = null;
  46   private int   _currPos   = -1;
  47 
  48   TokenBuffer ()
  49   {
  50     _size    = DEFAULT_SIZE;
  51     _buffer  = new Token [_size];
  52     _currPos = -1;
  53   } // ctor
  54 
  55   TokenBuffer (int size) throws Exception
  56   {
  57     _size    = size;   // _size == 0 is legal, but useless and problematic
  58     _buffer  = new Token [_size];
  59     _currPos = -1;
  60   } // ctor
  61 
  62   /** Inserts a token at the head of the buffer. */
  63   void insert (Token token)
  64   {
  65     // _size == 0 ==> ArithmeticException: divide by zero
  66     _currPos = ++_currPos % _size;
  67     _buffer [_currPos] = token;
  68   }
  69 
  70   /** Returns the token residing "i" elements from the head of the buffer. */
  71   Token lookBack (int i)
  72   {
  73     // Beware: i > _size ==> idx < 0 ==> ArrayOutOfBoundsException
  74     return _buffer [(_currPos - i) >= 0 ? _currPos - i : _currPos - i + _size];
  75   }
  76 
  77   /** Return the token most recently inserted into the buffer (i.e., the head of the buffer.) */
  78   Token current ()
  79   {
  80     // Beware: _buffer empty || _size == 0 ==> ArrayOutOfBoundsException
  81     return _buffer [_currPos];
  82   }
  83 }   // class TokenBuffer
  84 
  85 
  86 /*==================================================================================
  87   DATE<AUTHOR>   ACTION
  88   ----------------------------------------------------------------------------------
  89   11aug1997<daz> Initial version completed.  Buffer used to maintain history of
  90                  comments extracted from source file during parse.
  91   ==================================================================================*/