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