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