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