View Javadoc

1   /*
2    *  Copyright 2001-2010 Stephen Colebourne
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.joda.primitives;
17  
18  import java.util.Collection;
19  import java.util.Iterator;
20  
21  import org.joda.primitives.collection.ByteCollection;
22  
23  /**
24   * Provides utility methods and constants for <code>byte</code>.
25   * 
26   * @author Stephen Colebourne
27   * @author Jason Tiscione
28   * @version CODE GENERATED
29   * @since 1.0
30   */
31  public class ByteUtils {
32      // This file is CODE GENERATED. Do not change manually.
33      
34      /**
35       * Immutable empty array.
36       */
37      public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
38      
39      /**
40       * Constructor that should not usually be used.
41       */
42      public ByteUtils() {
43          super();
44      }
45  
46      /**
47       * Wraps an <code>byte</code> with an Object wrapper.
48       * 
49       * @param value  the primitive value
50       * @return the Object wrapper
51       */
52      public static Byte toObject(byte value) {
53          return new Byte(value);
54      }
55  
56      /**
57       * Unwraps the <code>Byte</code> to retrieve the primitive <code>byte</code>.
58       * 
59       * @param value  the Object wrapper, must not be null
60       * @return the primitive value
61       * @throws NullPointerException if the value if null
62       * @throws ClassCastException if the object is not <code>Byte</code>
63       */
64      public static byte toPrimitive(Object value) {
65          return ((Byte) value).byteValue();
66      }
67  
68      /**
69       * Unwraps a <code>Collection</code> to retrieve the primitive <code>byte</code>.
70       * 
71       * @param coll  the Collection of Byte, must not be null
72       * @return the primitive value array
73       * @throws NullPointerException if the collection if null
74       * @throws ClassCastException if any object is not <code>Byte</code>
75       */
76      public static byte[] toPrimitiveArray(Collection <?> coll) {
77          if (coll instanceof ByteCollection) {
78              return ((ByteCollection) coll).toByteArray();
79          }
80          byte[] result = new byte[coll.size()];
81          int i = 0;
82          for (Iterator<?> it = coll.iterator(); it.hasNext(); i++) {
83              Byte value = (Byte) it.next();
84              result[i] = value.byteValue();
85          }
86          return result;
87      }
88  
89  }