CVS Update: xmlpull-api-v1/addons/java/parser_pool/src/org/xmlpull/v1/parser_pool

Aleksander Andrzej Slominski <[email protected]> Wed, 23 Apr 2003 08:36:56 -0500 (EST)
Newsgroups gmane.text.xml.xmlpull.devel
Message-ID <[email protected]>
aslom       03/04/23 08:36:56

  Added:       addons/java/parser_pool/src/org/xmlpull/v1/parser_pool
                        XmlPullParserPool.java
  Log:
  parser_pool addon
  
  Revision  Changes    Path
  1.1                  xmlpull-api-v1/addons/java/parser_pool/src/org/xmlpull/v1/parser_pool/XmlPullParserPool.java
  
  Index: XmlPullParserPool.java
  ===================================================================
  /* -*-             c-basic-offset: 4; indent-tabs-mode: nil; -*-  //------100-columns-wide------>|*/
  // for license please see accompanying LICENSE.txt file (available also at http://www.xmlpull.org/)
  
  package org.xmlpull.v1.parser_pool;
  
  import java.util.ArrayList;
  import java.util.List;
  import org.xmlpull.v1.XmlPullParser;
  import org.xmlpull.v1.XmlPullParserException;
  import org.xmlpull.v1.XmlPullParserFactory;
  
  /**
   * Very simple utility to make pooling of XmlPull parsers easy.
   *
   * @author <a href="http://www.extreme.indiana.edu/~aslom/">Aleksander Slominski</a>
   */
  public class XmlPullParserPool {
      protected List pool = new ArrayList();
      protected XmlPullParserFactory factory;
  
  
      public XmlPullParserPool() throws XmlPullParserException {
          this(XmlPullParserFactory.newInstance());
      }
  
      public XmlPullParserPool(XmlPullParserFactory factory) {
          if(factory == null) throw new IllegalArgumentException();
          this.factory = factory;
      }
  
  
      public XmlPullParser getPullParserFromPool()
          throws XmlPullParserException
      {
          XmlPullParser pp = null;
          if(pool.size() > 0) {
              synchronized(pool) {
                  if(pool.size() > 0) {
                      pp = (XmlPullParser) pool.remove(pool.size() - 1 );
                  }
              }
          }
          if(pp == null) {
              pp = factory.newPullParser();
              //System.err.println("new parser instance created pp="+pp);
          }
          return pp;
      }
  
      public void returnPullParserToPool(XmlPullParser pp) {
          if(pp == null) throw new IllegalArgumentException();
          synchronized(pool) {
              pool.add(pp);
          }
      }
  
  
      // simple inline test
      public static void main(String[] args) throws Exception
      {
          //XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
          //XmlPullParserPool pool = new XmlPullParserPool(factory);
          XmlPullParserPool pool = new XmlPullParserPool();
          XmlPullParser p1 = pool.getPullParserFromPool();
          pool.returnPullParserToPool(p1);
          XmlPullParser p2 = pool.getPullParserFromPool();
          //assert p1 == p2;
          if(p1 != p2) throw new RuntimeException();
          pool.returnPullParserToPool(p2);
          System.out.println(pool.getClass()+" OK");
      }
  }
  
  
  
  


------------------------ Yahoo! Groups Sponsor ---------------------~-->
Get 128 Bit SSL Encryption!
http://us.click.yahoo.com/W7NydA/hdqFAA/bW3JAA/2U_rlB/TM
---------------------------------------------------------------------~->

To unsubscribe from this group, send an email to:
[email protected]

 

Your use of Yahoo! Groups is subject to http://docs.yahoo.com/info/terms/