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