001 /*
002 * Copyright 2001-2010 Stephen Colebourne
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016 package org.joda.primitives;
017
018 import java.util.Collection;
019 import java.util.Iterator;
020
021 import org.joda.primitives.collection.IntCollection;
022
023 /**
024 * Provides utility methods and constants for <code>int</code>.
025 *
026 * @author Stephen Colebourne
027 * @author Jason Tiscione
028 * @version CODE GENERATED
029 * @since 1.0
030 */
031 public class IntUtils {
032 // This file is CODE GENERATED. Do not change manually.
033
034 /**
035 * Immutable empty array.
036 */
037 public static final int[] EMPTY_INT_ARRAY = new int[0];
038
039 /**
040 * Constructor that should not usually be used.
041 */
042 public IntUtils() {
043 super();
044 }
045
046 /**
047 * Wraps an <code>int</code> with an Object wrapper.
048 *
049 * @param value the primitive value
050 * @return the Object wrapper
051 */
052 public static Integer toObject(int value) {
053 return new Integer(value);
054 }
055
056 /**
057 * Unwraps the <code>Integer</code> to retrieve the primitive <code>int</code>.
058 *
059 * @param value the Object wrapper, must not be null
060 * @return the primitive value
061 * @throws NullPointerException if the value if null
062 * @throws ClassCastException if the object is not <code>Integer</code>
063 */
064 public static int toPrimitive(Object value) {
065 return ((Integer) value).intValue();
066 }
067
068 /**
069 * Unwraps a <code>Collection</code> to retrieve the primitive <code>int</code>.
070 *
071 * @param coll the Collection of Integer, must not be null
072 * @return the primitive value array
073 * @throws NullPointerException if the collection if null
074 * @throws ClassCastException if any object is not <code>Integer</code>
075 */
076 public static int[] toPrimitiveArray(Collection <?> coll) {
077 if (coll instanceof IntCollection) {
078 return ((IntCollection) coll).toIntArray();
079 }
080 int[] result = new int[coll.size()];
081 int i = 0;
082 for (Iterator<?> it = coll.iterator(); it.hasNext(); i++) {
083 Integer value = (Integer) it.next();
084 result[i] = value.intValue();
085 }
086 return result;
087 }
088
089 }