scolebourne    2003/10/20 15:19:40

  Added:       primitives/src/java/org/apache/commons/primitive/list/impl
                        StringBufferCharList.java
  Log:
  Specialised list implementatoin
  
  Revision  Changes    Path
  1.1                  
jakarta-commons-sandbox/primitives/src/java/org/apache/commons/primitive/list/impl/StringBufferCharList.java
  
  Index: StringBufferCharList.java
  ===================================================================
  /* ====================================================================
   * The Apache Software License, Version 1.1
   *
   * Copyright (c) 2003 The Apache Software Foundation.  All rights
   * reserved.
   *
   * Redistribution and use in source and binary forms, with or without
   * modification, are permitted provided that the following conditions
   * are met:
   *
   * 1. Redistributions of source code must retain the above copyright
   *    notice, this list of conditions and the following disclaimer.
   *
   * 2. Redistributions in binary form must reproduce the above copyright
   *    notice, this list of conditions and the following disclaimer in
   *    the documentation and/or other materials provided with the
   *    distribution.
   *
   * 3. The end-user documentation included with the redistribution, if
   *    any, must include the following acknowledgement:
   *       "This product includes software developed by the
   *        Apache Software Foundation (http://www.apache.org/)."
   *    Alternately, this acknowledgement may appear in the software itself,
   *    if and wherever such third-party acknowledgements normally appear.
   *
   * 4. The names "The Jakarta Project", "Commons", and "Apache Software
   *    Foundation" must not be used to endorse or promote products derived
   *    from this software without prior written permission. For written
   *    permission, please contact [EMAIL PROTECTED]
   *
   * 5. Products derived from this software may not be called "Apache"
   *    nor may "Apache" appear in their names without prior written
   *    permission of the Apache Software Foundation.
   *
   * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
   * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
   * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
   * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
   * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
   * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
   * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  package org.apache.commons.primitive.list.impl;
  
  import org.apache.commons.primitive.list.CharList;
  
  /**
   * Unmodifiable CharList wrapped around a StringBuffer.
   * <p>
   * This class implements [EMAIL PROTECTED] java.util.List List} allowing
   * seamless integration with other APIs.
   *
   * @author Stephen Colebourne
   * @version $Id: StringBufferCharList.java,v 1.1 2003/10/20 22:19:40 scolebourne Exp 
$
   * @since 1.0
   */
  public class StringBufferCharList extends AbstractCharList {
  
      /** The String being wrapped */    
      protected final StringBuffer stringBuffer;
      
      /**
       * Decorates the specified string buffer with a StringBufferCharList.
       * <p>
       * The specified buffer is used as the datasource, so changes to one
       * will affect the other.
       * 
       * @param buf  the string buffer to decorate, must not be null
       * @throws IllegalArgumentException if the string is null
       */
      public static StringBufferCharList decorate(StringBuffer buf) {
          if (buf == null) {
              throw new IllegalArgumentException("StringBuffer must not be null");
          }
          return new StringBufferCharList(buf);
      }
      
      //-----------------------------------------------------------------------
      /**
       * Constructor that uses an empty string as the datasource.
       */
      public StringBufferCharList() {
          super();
          this.stringBuffer = new StringBuffer();
      }
      
      /**
       * Constructor that copies the specified list.
       * 
       * @param list  the list to copy, must not be null
       * @throws IllegalArgumentException if the list is null
       */
      public StringBufferCharList(CharList list) {
          super();
          if (list == null) {
              throw new IllegalArgumentException("List must not be null");
          }
          this.stringBuffer = new StringBuffer(list.size());
          this.stringBuffer.append(list.toCharArray());
      }
      
      /**
       * Constructor that copies the specified string.
       * 
       * @param str  the string to copy, must not be null
       * @throws IllegalArgumentException if the string is null
       */
      public StringBufferCharList(String str) {
          super();
          if (str == null) {
              throw new IllegalArgumentException("String must not be null");
          }
          this.stringBuffer = new StringBuffer(str);
      }
      
      /**
       * Constructor that <i>decorates</i> the specified string buffer.
       * 
       * @param buf  the string buffer to decorate, must not be null
       * @throws IllegalArgumentException if the string is null
       */
      protected StringBufferCharList(StringBuffer buf) {
          super();
          if (buf == null) {
              throw new IllegalArgumentException("StringBuffer must not be null");
          }
          this.stringBuffer = buf;
      }
      
      // Implementation
      //-----------------------------------------------------------------------
      /**
       * Gets the character at the specified index.
       * 
       * @param index  the index to retrieve
       * @return the character at the specified index
       */
      public char getChar(int index) {
          checkIndexExists(index);
          return stringBuffer.charAt(index);
      }
  
      /**
       * Gets the size of the list, which is the string length.
       * 
       * @return the string length
       */
      public int size() {
          return stringBuffer.length();
      }
  
      /**
       * Adds a primitive value to this list at an index.
       *
       * @param index  the index to add at
       * @param value  the value to add to this collection
       * @return <code>true</code> if this list was modified by this method call
       * @throws IndexOutOfBoundsException if the index is invalid
       */
      public boolean add(int index, char value) {
          checkAddModifiable();
          checkIndex(index);
          stringBuffer.insert(index, value);
          return true;
      }
  
      /**
       * Removes a primitive value by index from the list.
       *
       * @param index  the index to remove from
       * @return the primitive value previously at this index
       * @throws IndexOutOfBoundsException if the index is invalid
       */
      public char removeIndex(int index) {
          checkRemoveModifiable();
          checkIndexExists(index);
          char old = stringBuffer.charAt(index);
          stringBuffer.deleteCharAt(index);
          return old;
      }
  
      /**
       * Sets the primitive value at a specified index.
       * <p>
       * This implementation throws UnsupportedOperationException.
       *
       * @param index  the index to set
       * @param value  the value to store
       * @return the previous value at the index
       * @throws IndexOutOfBoundsException if the index is invalid
       */
      public char set(int index, char value) {
          checkSetModifiable();
          checkIndexExists(index);
          char old = stringBuffer.charAt(index);
          stringBuffer.setCharAt(index, value);
          return old;
      }
  
      /**
       * Are the add methods supported.
       *
       * @return true
       */
      protected boolean isAddModifiable() {
          return true;
      }
  
      /**
       * Are the remove methods supported.
       *
       * @return true
       */
      protected boolean isRemoveModifiable() {
          return true;
      }
  
      /**
       * Are the set methods supported.
       *
       * @return true
       */
      protected boolean isSetModifiable() {
          return true;
      }
  
      /**
       * Is the collection modifiable in any way.
       *
       * @return true if supported
       */
      public boolean isModifiable() {
          return true;
      }
  
      // Optimisation
      //-----------------------------------------------------------------------
      /**
       * Adds a primitive value to this collection.
       *
       * @param value  the value to add to this collection
       * @return <code>true</code> if this collection was modified by this method call
       */
      public boolean add(char value) {
          checkAddModifiable();
          stringBuffer.append(value);
          return true;
      }
  
      /**
       * Adds an array of primitive values to this list at an index.
       *
       * @param index  the index to add at
       * @param values  the values to add to this collection, null treated as empty 
array
       * @return <code>true</code> if this list was modified by this method call
       * @throws IndexOutOfBoundsException if the index is invalid
       */
      public boolean addAll(char[] values) {
          checkAddModifiable();
          if (values == null || values.length == 0) {
              return false;
          }
          stringBuffer.append(values);
          return true;
      }
  
      /**
       * Adds an array of primitive values to this list at an index.
       *
       * @param index  the index to add at
       * @param values  the values to add to this collection, null treated as empty 
array
       * @return <code>true</code> if this list was modified by this method call
       * @throws IndexOutOfBoundsException if the index is invalid
       */
      public boolean addAll(int index, char[] values) {
          checkAddModifiable();
          checkIndex(index);
          if (values == null || values.length == 0) {
              return false;
          }
          stringBuffer.insert(index, values);
          return true;
      }
  
      /**
       * Removes a range of values from the list.
       *
       * @param fromIndexInclusive  the start of the range to remove, inclusive
       * @param toIndexExclusive  the end of the range to remove, exclusive
       * @return <code>true</code> if the collection was modified
       */
      public boolean removeRange(int fromIndexInclusive, int toIndexExclusive) {
          checkRemoveModifiable();
          checkRange(fromIndexInclusive, toIndexExclusive);
          if (fromIndexInclusive == toIndexExclusive) {
              return false;
          }
          stringBuffer.delete(fromIndexInclusive, toIndexExclusive);
          return true;
      }
  
      /**
       * Copies data from this collection into the specified array.
       * This method is pre-validated.
       * 
       * @param fromIndex  the index to start from
       * @param dest  the destination array
       * @param destIndex  the destination start index
       * @param size  the number of items to copy
       */
      protected void arrayCopy(int fromIndex, char[] dest, int destIndex, int size) {
          stringBuffer.getChars(fromIndex, fromIndex + size, dest, destIndex);
      }
  
  }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to