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