1. /*
  2. * $Header: /home/cvs/jakarta-commons/primitives/src/java/org/apache/commons/collections/primitives/CharCollections.java,v 1.1 2003/10/29 19:20:08 rwaldhoff Exp $
  3. * ====================================================================
  4. * The Apache Software License, Version 1.1
  5. *
  6. * Copyright (c) 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 CharERRUPTION) 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 org.apache.commons.collections.primitives.decorators.UnmodifiableCharIterator;
  59. import org.apache.commons.collections.primitives.decorators.UnmodifiableCharList;
  60. import org.apache.commons.collections.primitives.decorators.UnmodifiableCharListIterator;
  61. /**
  62. * This class consists exclusively of static methods that operate on or
  63. * return CharCollections.
  64. * <p>
  65. * The methods of this class all throw a NullPoCharerException if the
  66. * provided collection is null.
  67. *
  68. * @version $Revision: 1.1 $ $Date: 2003/10/29 19:20:08 $
  69. *
  70. * @author Rodney Waldhoff
  71. */
  72. public final class CharCollections {
  73. /**
  74. * Returns an unmodifiable CharList containing only the specified element.
  75. * @param value the single value
  76. * @return an unmodifiable CharList containing only the specified element.
  77. */
  78. public static CharList singletonCharList(char value) {
  79. // TODO: a specialized implementation of CharList may be more performant
  80. CharList list = new ArrayCharList(1);
  81. list.add(value);
  82. return UnmodifiableCharList.wrap(list);
  83. }
  84. /**
  85. * Returns an unmodifiable CharIterator containing only the specified element.
  86. * @param value the single value
  87. * @return an unmodifiable CharIterator containing only the specified element.
  88. */
  89. public static CharIterator singletonCharIterator(char value) {
  90. return singletonCharList(value).iterator();
  91. }
  92. /**
  93. * Returns an unmodifiable CharListIterator containing only the specified element.
  94. * @param value the single value
  95. * @return an unmodifiable CharListIterator containing only the specified element.
  96. */
  97. public static CharListIterator singletonCharListIterator(char value) {
  98. return singletonCharList(value).listIterator();
  99. }
  100. /**
  101. * Returns an unmodifiable version of the given non-null CharList.
  102. * @param list the non-null CharList to wrap in an unmodifiable decorator
  103. * @return an unmodifiable version of the given non-null CharList
  104. * @throws NullPoCharerException if the given CharList is null
  105. * @see org.apache.commons.collections.primitives.decorators.UnmodifiableCharList#wrap
  106. */
  107. public static CharList unmodifiableCharList(CharList list) throws NullPointerException {
  108. if(null == list) {
  109. throw new NullPointerException();
  110. }
  111. return UnmodifiableCharList.wrap(list);
  112. }
  113. /**
  114. * Returns an unmodifiable version of the given non-null CharIterator.
  115. * @param iter the non-null CharIterator to wrap in an unmodifiable decorator
  116. * @return an unmodifiable version of the given non-null CharIterator
  117. * @throws NullPoCharerException if the given CharIterator is null
  118. * @see org.apache.commons.collections.primitives.decorators.UnmodifiableCharIterator#wrap
  119. */
  120. public static CharIterator unmodifiableCharIterator(CharIterator iter) {
  121. if(null == iter) {
  122. throw new NullPointerException();
  123. }
  124. return UnmodifiableCharIterator.wrap(iter);
  125. }
  126. /**
  127. * Returns an unmodifiable version of the given non-null CharListIterator.
  128. * @param iter the non-null CharListIterator to wrap in an unmodifiable decorator
  129. * @return an unmodifiable version of the given non-null CharListIterator
  130. * @throws NullPoCharerException if the given CharListIterator is null
  131. * @see org.apache.commons.collections.primitives.decorators.UnmodifiableCharListIterator#wrap
  132. */
  133. public static CharListIterator unmodifiableCharListIterator(CharListIterator iter) {
  134. if(null == iter) {
  135. throw new NullPointerException();
  136. }
  137. return UnmodifiableCharListIterator.wrap(iter);
  138. }
  139. /**
  140. * Returns an unmodifiable, empty CharList.
  141. * @return an unmodifiable, empty CharList.
  142. * @see #EMPTY_CHAR_LIST
  143. */
  144. public static CharList getEmptyCharList() {
  145. return EMPTY_CHAR_LIST;
  146. }
  147. /**
  148. * Returns an unmodifiable, empty CharIterator
  149. * @return an unmodifiable, empty CharIterator.
  150. * @see #EMPTY_CHAR_ITERATOR
  151. */
  152. public static CharIterator getEmptyCharIterator() {
  153. return EMPTY_CHAR_ITERATOR;
  154. }
  155. /**
  156. * Returns an unmodifiable, empty CharListIterator
  157. * @return an unmodifiable, empty CharListIterator.
  158. * @see #EMPTY_CHAR_LIST_ITERATOR
  159. */
  160. public static CharListIterator getEmptyCharListIterator() {
  161. return EMPTY_CHAR_LIST_ITERATOR;
  162. }
  163. /**
  164. * An unmodifiable, empty CharList
  165. * @see #getEmptyCharList
  166. */
  167. public static final CharList EMPTY_CHAR_LIST = unmodifiableCharList(new ArrayCharList(0));
  168. /**
  169. * An unmodifiable, empty CharIterator
  170. * @see #getEmptyCharIterator
  171. */
  172. public static final CharIterator EMPTY_CHAR_ITERATOR = unmodifiableCharIterator(EMPTY_CHAR_LIST.iterator());
  173. /**
  174. * An unmodifiable, empty CharListIterator
  175. * @see #getEmptyCharListIterator
  176. */
  177. public static final CharListIterator EMPTY_CHAR_LIST_ITERATOR = unmodifiableCharListIterator(EMPTY_CHAR_LIST.listIterator());
  178. }