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.IntCollection;
22
23 /**
24 * Provides utility methods and constants for <code>int</code>.
25 *
26 * @author Stephen Colebourne
27 * @author Jason Tiscione
28 * @version CODE GENERATED
29 * @since 1.0
30 */
31 public class IntUtils {
32 // This file is CODE GENERATED. Do not change manually.
33
34 /**
35 * Immutable empty array.
36 */
37 public static final int[] EMPTY_INT_ARRAY = new int[0];
38
39 /**
40 * Constructor that should not usually be used.
41 */
42 public IntUtils() {
43 super();
44 }
45
46 /**
47 * Wraps an <code>int</code> with an Object wrapper.
48 *
49 * @param value the primitive value
50 * @return the Object wrapper
51 */
52 public static Integer toObject(int value) {
53 return new Integer(value);
54 }
55
56 /**
57 * Unwraps the <code>Integer</code> to retrieve the primitive <code>int</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>Integer</code>
63 */
64 public static int toPrimitive(Object value) {
65 return ((Integer) value).intValue();
66 }
67
68 /**
69 * Unwraps a <code>Collection</code> to retrieve the primitive <code>int</code>.
70 *
71 * @param coll the Collection of Integer, 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>Integer</code>
75 */
76 public static int[] toPrimitiveArray(Collection <?> coll) {
77 if (coll instanceof IntCollection) {
78 return ((IntCollection) coll).toIntArray();
79 }
80 int[] result = new int[coll.size()];
81 int i = 0;
82 for (Iterator<?> it = coll.iterator(); it.hasNext(); i++) {
83 Integer value = (Integer) it.next();
84 result[i] = value.intValue();
85 }
86 return result;
87 }
88
89 }