1. /*
  2. * $Header: /home/cvs/jakarta-commons/primitives/src/java/org/apache/commons/collections/primitives/ArrayCharList.java,v 1.3 2003/10/16 20:49:35 scolebourne Exp $
  3. * ====================================================================
  4. * The Apache Software License, Version 1.1
  5. *
  6. * Copyright (c) 2002-2003 The Apache Software Foundation. All rights
  7. * reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions
  11. * are met:
  12. *
  13. * 1. Redistributions of source code must retain the above copyright
  14. * notice, this list of conditions and the following disclaimer.
  15. *
  16. * 2. Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in
  18. * the documentation and/or other materials provided with the
  19. * distribution.
  20. *
  21. * 3. The end-user documentation included with the redistribution, if
  22. * any, must include the following acknowledgement:
  23. * "This product includes software developed by the
  24. * Apache Software Foundation (http://www.apache.org/)."
  25. * Alternately, this acknowledgement may appear in the software itself,
  26. * if and wherever such third-party acknowledgements normally appear.
  27. *
  28. * 4. The names "The Jakarta Project", "Commons", and "Apache Software
  29. * Foundation" must not be used to endorse or promote products derived
  30. * from this software without prior written permission. For written
  31. * permission, please contact apache@apache.org.
  32. *
  33. * 5. Products derived from this software may not be called "Apache"
  34. * nor may "Apache" appear in their names without prior written
  35. * permission of the Apache Software Foundation.
  36. *
  37. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  38. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  39. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  40. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  41. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  42. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  43. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  44. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  45. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  46. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  47. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. * SUCH DAMAGE.
  49. * ====================================================================
  50. *
  51. * This software consists of voluntary contributions made by many
  52. * individuals on behalf of the Apache Software Foundation. For more
  53. * information on the Apache Software Foundation, please see
  54. * <http://www.apache.org/>.
  55. *
  56. */
  57. package org.apache.commons.collections.primitives;
  58. import java.io.IOException;
  59. import java.io.ObjectInputStream;
  60. import java.io.ObjectOutputStream;
  61. import java.io.Serializable;
  62. /**
  63. * An {@link CharList} backed by an array of <code>char</code>s.
  64. * This implementation supports all optional methods.
  65. *
  66. * @since Commons Primitives 1.0
  67. * @version $Revision: 1.3 $ $Date: 2003/10/16 20:49:35 $
  68. *
  69. * @author Rodney Waldhoff
  70. */
  71. public class ArrayCharList extends RandomAccessCharList implements CharList, Serializable {
  72. // constructors
  73. //-------------------------------------------------------------------------
  74. /**
  75. * Construct an empty list with the default
  76. * initial capacity.
  77. */
  78. public ArrayCharList() {
  79. this(8);
  80. }
  81. /**
  82. * Construct an empty list with the given
  83. * initial capacity.
  84. * @throws IllegalArgumentException when <i>initialCapacity</i> is negative
  85. */
  86. public ArrayCharList(int initialCapacity) {
  87. if(initialCapacity < 0) {
  88. throw new IllegalArgumentException("capacity " + initialCapacity);
  89. }
  90. _data = new char[initialCapacity];
  91. _size = 0;
  92. }
  93. /**
  94. * Constructs a list containing the elements of the given collection,
  95. * in the order they are returned by that collection's iterator.
  96. *
  97. * @see ArrayCharList#addAll(org.apache.commons.collections.primitives.CharCollection)
  98. * @param that the non-<code>null</code> collection of <code>char</code>s
  99. * to add
  100. * @throws NullPointerException if <i>that</i> is <code>null</code>
  101. */
  102. public ArrayCharList(CharCollection that) {
  103. this(that.size());
  104. addAll(that);
  105. }
  106. // CharList methods
  107. //-------------------------------------------------------------------------
  108. public char get(int index) {
  109. checkRange(index);
  110. return _data[index];
  111. }
  112. public int size() {
  113. return _size;
  114. }
  115. /**
  116. * Removes the element at the specified position in
  117. * (optional operation). Any subsequent elements
  118. * are shifted to the left, subtracting one from their
  119. * indices. Returns the element that was removed.
  120. *
  121. * @param index the index of the element to remove
  122. * @return the value of the element that was removed
  123. *
  124. * @throws UnsupportedOperationException when this operation is not
  125. * supported
  126. * @throws IndexOutOfBoundsException if the specified index is out of range
  127. */
  128. public char removeElementAt(int index) {
  129. checkRange(index);
  130. incrModCount();
  131. char oldval = _data[index];
  132. int numtomove = _size - index - 1;
  133. if(numtomove > 0) {
  134. System.arraycopy(_data,index+1,_data,index,numtomove);
  135. }
  136. _size--;
  137. return oldval;
  138. }
  139. /**
  140. * Replaces the element at the specified
  141. * position in me with the specified element
  142. * (optional operation).
  143. *
  144. * @param index the index of the element to change
  145. * @param element the value to be stored at the specified position
  146. * @return the value previously stored at the specified position
  147. *
  148. * @throws UnsupportedOperationException when this operation is not
  149. * supported
  150. * @throws IndexOutOfBoundsException if the specified index is out of range
  151. */
  152. public char set(int index, char element) {
  153. checkRange(index);
  154. incrModCount();
  155. char oldval = _data[index];
  156. _data[index] = element;
  157. return oldval;
  158. }
  159. /**
  160. * Inserts the specified element at the specified position
  161. * (optional operation). Shifts the element currently
  162. * at that position (if any) and any subsequent elements to the
  163. * right, increasing their indices.
  164. *
  165. * @param index the index at which to insert the element
  166. * @param element the value to insert
  167. *
  168. * @throws UnsupportedOperationException when this operation is not
  169. * supported
  170. * @throws IllegalArgumentException if some aspect of the specified element
  171. * prevents it from being added to me
  172. * @throws IndexOutOfBoundsException if the specified index is out of range
  173. */
  174. public void add(int index, char element) {
  175. checkRangeIncludingEndpoint(index);
  176. incrModCount();
  177. ensureCapacity(_size+1);
  178. int numtomove = _size-index;
  179. System.arraycopy(_data,index,_data,index+1,numtomove);
  180. _data[index] = element;
  181. _size++;
  182. }
  183. // capacity methods
  184. //-------------------------------------------------------------------------
  185. /**
  186. * Increases my capacity, if necessary, to ensure that I can hold at
  187. * least the number of elements specified by the minimum capacity
  188. * argument without growing.
  189. */
  190. public void ensureCapacity(int mincap) {
  191. incrModCount();
  192. if(mincap > _data.length) {
  193. int newcap = (_data.length * 3)/2 + 1;
  194. char[] olddata = _data;
  195. _data = new char[newcap < mincap ? mincap : newcap];
  196. System.arraycopy(olddata,0,_data,0,_size);
  197. }
  198. }
  199. /**
  200. * Reduce my capacity, if necessary, to match my
  201. * current {@link #size size}.
  202. */
  203. public void trimToSize() {
  204. incrModCount();
  205. if(_size < _data.length) {
  206. char[] olddata = _data;
  207. _data = new char[_size];
  208. System.arraycopy(olddata,0,_data,0,_size);
  209. }
  210. }
  211. // private methods
  212. //-------------------------------------------------------------------------
  213. private void writeObject(ObjectOutputStream out) throws IOException{
  214. out.defaultWriteObject();
  215. out.writeInt(_data.length);
  216. for(int i=0;i<_size;i++) {
  217. out.writeChar(_data[i]);
  218. }
  219. }
  220. private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
  221. in.defaultReadObject();
  222. _data = new char[in.readInt()];
  223. for(int i=0;i<_size;i++) {
  224. _data[i] = in.readChar();
  225. }
  226. }
  227. private final void checkRange(int index) {
  228. if(index < 0 || index >= _size) {
  229. throw new IndexOutOfBoundsException("Should be at least 0 and less than " + _size + ", found " + index);
  230. }
  231. }
  232. private final void checkRangeIncludingEndpoint(int index) {
  233. if(index < 0 || index > _size) {
  234. throw new IndexOutOfBoundsException("Should be at least 0 and at most " + _size + ", found " + index);
  235. }
  236. }
  237. // attributes
  238. //-------------------------------------------------------------------------
  239. private transient char[] _data = null;
  240. private int _size = 0;
  241. }