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