1. /*
  2. * $Header: /home/cvs/jakarta-commons/primitives/src/java/org/apache/commons/collections/primitives/ByteCollections.java,v 1.1 2003/10/29 18:33:11 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 ByteERRUPTION) 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.UnmodifiableByteIterator;
  59. import org.apache.commons.collections.primitives.decorators.UnmodifiableByteList;
  60. import org.apache.commons.collections.primitives.decorators.UnmodifiableByteListIterator;
  61. /**
  62. * This class consists exclusively of static methods that operate on or
  63. * return ByteCollections.
  64. * <p>
  65. * The methods of this class all throw a NullPoByteerException if the
  66. * provided collection is null.
  67. *
  68. * @version $Revision: 1.1 $ $Date: 2003/10/29 18:33:11 $
  69. *
  70. * @author Rodney Waldhoff
  71. */
  72. public final class ByteCollections {
  73. /**
  74. * Returns an unmodifiable ByteList containing only the specified element.
  75. * @param value the single value
  76. * @return an unmodifiable ByteList containing only the specified element.
  77. */
  78. public static ByteList singletonByteList(byte value) {
  79. // TODO: a specialized implementation of ByteList may be more performant
  80. ByteList list = new ArrayByteList(1);
  81. list.add(value);
  82. return UnmodifiableByteList.wrap(list);
  83. }
  84. /**
  85. * Returns an unmodifiable ByteIterator containing only the specified element.
  86. * @param value the single value
  87. * @return an unmodifiable ByteIterator containing only the specified element.
  88. */
  89. public static ByteIterator singletonByteIterator(byte value) {
  90. return singletonByteList(value).iterator();
  91. }
  92. /**
  93. * Returns an unmodifiable ByteListIterator containing only the specified element.
  94. * @param value the single value
  95. * @return an unmodifiable ByteListIterator containing only the specified element.
  96. */
  97. public static ByteListIterator singletonByteListIterator(byte value) {
  98. return singletonByteList(value).listIterator();
  99. }
  100. /**
  101. * Returns an unmodifiable version of the given non-null ByteList.
  102. * @param list the non-null ByteList to wrap in an unmodifiable decorator
  103. * @return an unmodifiable version of the given non-null ByteList
  104. * @throws NullPoByteerException if the given ByteList is null
  105. * @see org.apache.commons.collections.primitives.decorators.UnmodifiableByteList#wrap
  106. */
  107. public static ByteList unmodifiableByteList(ByteList list) throws NullPointerException {
  108. if(null == list) {
  109. throw new NullPointerException();
  110. }
  111. return UnmodifiableByteList.wrap(list);
  112. }
  113. /**
  114. * Returns an unmodifiable version of the given non-null ByteIterator.
  115. * @param iter the non-null ByteIterator to wrap in an unmodifiable decorator
  116. * @return an unmodifiable version of the given non-null ByteIterator
  117. * @throws NullPoByteerException if the given ByteIterator is null
  118. * @see org.apache.commons.collections.primitives.decorators.UnmodifiableByteIterator#wrap
  119. */
  120. public static ByteIterator unmodifiableByteIterator(ByteIterator iter) {
  121. if(null == iter) {
  122. throw new NullPointerException();
  123. }
  124. return UnmodifiableByteIterator.wrap(iter);
  125. }
  126. /**
  127. * Returns an unmodifiable version of the given non-null ByteListIterator.
  128. * @param iter the non-null ByteListIterator to wrap in an unmodifiable decorator
  129. * @return an unmodifiable version of the given non-null ByteListIterator
  130. * @throws NullPoByteerException if the given ByteListIterator is null
  131. * @see org.apache.commons.collections.primitives.decorators.UnmodifiableByteListIterator#wrap
  132. */
  133. public static ByteListIterator unmodifiableByteListIterator(ByteListIterator iter) {
  134. if(null == iter) {
  135. throw new NullPointerException();
  136. }
  137. return UnmodifiableByteListIterator.wrap(iter);
  138. }
  139. /**
  140. * Returns an unmodifiable, empty ByteList.
  141. * @return an unmodifiable, empty ByteList.
  142. * @see #EMPTY_BYTE_LIST
  143. */
  144. public static ByteList getEmptyByteList() {
  145. return EMPTY_BYTE_LIST;
  146. }
  147. /**
  148. * Returns an unmodifiable, empty ByteIterator
  149. * @return an unmodifiable, empty ByteIterator.
  150. * @see #EMPTY_BYTE_ITERATOR
  151. */
  152. public static ByteIterator getEmptyByteIterator() {
  153. return EMPTY_BYTE_ITERATOR;
  154. }
  155. /**
  156. * Returns an unmodifiable, empty ByteListIterator
  157. * @return an unmodifiable, empty ByteListIterator.
  158. * @see #EMPTY_BYTE_LIST_ITERATOR
  159. */
  160. public static ByteListIterator getEmptyByteListIterator() {
  161. return EMPTY_BYTE_LIST_ITERATOR;
  162. }
  163. /**
  164. * An unmodifiable, empty ByteList
  165. * @see #getEmptyByteList
  166. */
  167. public static final ByteList EMPTY_BYTE_LIST = unmodifiableByteList(new ArrayByteList(0));
  168. /**
  169. * An unmodifiable, empty ByteIterator
  170. * @see #getEmptyByteIterator
  171. */
  172. public static final ByteIterator EMPTY_BYTE_ITERATOR = unmodifiableByteIterator(EMPTY_BYTE_LIST.iterator());
  173. /**
  174. * An unmodifiable, empty ByteListIterator
  175. * @see #getEmptyByteListIterator
  176. */
  177. public static final ByteListIterator EMPTY_BYTE_LIST_ITERATOR = unmodifiableByteListIterator(EMPTY_BYTE_LIST.listIterator());
  178. }