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 */
016package org.joda.primitives;
017
018import java.util.Collection;
019import java.util.Iterator;
020
021import org.joda.primitives.collection.ShortCollection;
022
023/**
024 * Provides utility methods and constants for <code>short</code>.
025 * 
026 * @author Stephen Colebourne
027 * @author Jason Tiscione
028 * @version CODE GENERATED
029 * @since 1.0
030 */
031public class ShortUtils {
032    // This file is CODE GENERATED. Do not change manually.
033    
034    /**
035     * Immutable empty array.
036     */
037    public static final short[] EMPTY_SHORT_ARRAY = new short[0];
038    
039    /**
040     * Constructor that should not usually be used.
041     */
042    public ShortUtils() {
043        super();
044    }
045
046    /**
047     * Wraps an <code>short</code> with an Object wrapper.
048     * 
049     * @param value  the primitive value
050     * @return the Object wrapper
051     */
052    public static Short toObject(short value) {
053        return new Short(value);
054    }
055
056    /**
057     * Unwraps the <code>Short</code> to retrieve the primitive <code>short</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>Short</code>
063     */
064    public static short toPrimitive(Object value) {
065        return ((Short) value).shortValue();
066    }
067
068    /**
069     * Unwraps a <code>Collection</code> to retrieve the primitive <code>short</code>.
070     * 
071     * @param coll  the Collection of Short, 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>Short</code>
075     */
076    public static short[] toPrimitiveArray(Collection <?> coll) {
077        if (coll instanceof ShortCollection) {
078            return ((ShortCollection) coll).toShortArray();
079        }
080        short[] result = new short[coll.size()];
081        int i = 0;
082        for (Iterator<?> it = coll.iterator(); it.hasNext(); i++) {
083            Short value = (Short) it.next();
084            result[i] = value.shortValue();
085        }
086        return result;
087    }
088
089}