001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 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 * Contributors: 017 * Nuxeo - initial API and implementation 018 * 019 * $Id$ 020 */ 021 022package org.nuxeo.common.collections; 023 024import java.util.ArrayList; 025import java.util.Arrays; 026import java.util.Collection; 027import java.util.Iterator; 028import java.util.List; 029 030/** 031 * @author <a href="mailto:[email protected]">Bogdan Stefanescu</a> 032 */ 033public final class PrimitiveArrays { 034 035 // Utility class. 036 private PrimitiveArrays() { 037 } 038 039 @SuppressWarnings({ "ObjectEquality" }) 040 public static Object toPrimitiveArray(Collection<Object> col, Class primitiveArrayType) { 041 if (primitiveArrayType == Integer.TYPE) { 042 return toIntArray(col); 043 } else if (primitiveArrayType == Long.TYPE) { 044 return toLongArray(col); 045 } else if (primitiveArrayType == Double.TYPE) { 046 return toDoubleArray(col); 047 } else if (primitiveArrayType == Float.TYPE) { 048 return toFloatArray(col); 049 } else if (primitiveArrayType == Boolean.TYPE) { 050 return toBooleanArray(col); 051 } else if (primitiveArrayType == Byte.TYPE) { 052 return toByteArray(col); 053 } else if (primitiveArrayType == Character.TYPE) { 054 return toCharArray(col); 055 } else if (primitiveArrayType == Short.TYPE) { 056 return toShortArray(col); 057 } 058 return null; 059 } 060 061 public static int[] toIntArray(Collection col) { 062 int size = col.size(); 063 int[] ar = new int[size]; 064 Iterator it = col.iterator(); 065 int i = 0; 066 while (it.hasNext()) { 067 ar[i++] = (Integer) it.next(); 068 } 069 return ar; 070 } 071 072 public static long[] toLongArray(Collection col) { 073 int size = col.size(); 074 long[] ar = new long[size]; 075 Iterator it = col.iterator(); 076 int i = 0; 077 while (it.hasNext()) { 078 ar[i++] = (Long) it.next(); 079 } 080 return ar; 081 } 082 083 public static double[] toDoubleArray(Collection col) { 084 int size = col.size(); 085 double[] ar = new double[size]; 086 Iterator it = col.iterator(); 087 int i = 0; 088 while (it.hasNext()) { 089 ar[i++] = (Double) it.next(); 090 } 091 return ar; 092 } 093 094 public static float[] toFloatArray(Collection col) { 095 int size = col.size(); 096 float[] ar = new float[size]; 097 Iterator it = col.iterator(); 098 int i = 0; 099 while (it.hasNext()) { 100 ar[i++] = (Float) it.next(); 101 } 102 return ar; 103 } 104 105 public static boolean[] toBooleanArray(Collection col) { 106 int size = col.size(); 107 boolean[] ar = new boolean[size]; 108 Iterator it = col.iterator(); 109 int i = 0; 110 while (it.hasNext()) { 111 ar[i++] = (Boolean) it.next(); 112 } 113 return ar; 114 } 115 116 public static short[] toShortArray(Collection col) { 117 int size = col.size(); 118 short[] ar = new short[size]; 119 Iterator it = col.iterator(); 120 int i = 0; 121 while (it.hasNext()) { 122 ar[i++] = (Short) it.next(); 123 } 124 return ar; 125 } 126 127 public static byte[] toByteArray(Collection col) { 128 int size = col.size(); 129 byte[] ar = new byte[size]; 130 Iterator it = col.iterator(); 131 int i = 0; 132 while (it.hasNext()) { 133 ar[i++] = (Byte) it.next(); 134 } 135 return ar; 136 } 137 138 public static char[] toCharArray(Collection col) { 139 int size = col.size(); 140 char[] ar = new char[size]; 141 Iterator it = col.iterator(); 142 int i = 0; 143 while (it.hasNext()) { 144 ar[i++] = (Character) it.next(); 145 } 146 return ar; 147 } 148 149 public static Object[] toObjectArray(Object array) { 150 Class<?> arrType = array.getClass().getComponentType(); 151 if (arrType == null) { 152 throw new IllegalArgumentException("Not an array"); 153 } 154 if (arrType.isPrimitive()) { 155 if (arrType == Integer.TYPE) { 156 int[] ar = (int[]) array; 157 Integer[] result = new Integer[ar.length]; 158 for (int i = 0; i < ar.length; i++) { 159 result[i] = ar[i]; 160 } 161 return result; 162 } else if (arrType == Long.TYPE) { 163 long[] ar = (long[]) array; 164 Long[] result = new Long[ar.length]; 165 for (int i = 0; i < ar.length; i++) { 166 result[i] = ar[i]; 167 } 168 return result; 169 } else if (arrType == Double.TYPE) { 170 double[] ar = (double[]) array; 171 Double[] result = new Double[ar.length]; 172 for (int i = 0; i < ar.length; i++) { 173 result[i] = ar[i]; 174 } 175 return result; 176 } else if (arrType == Float.TYPE) { 177 float[] ar = (float[]) array; 178 Float[] result = new Float[ar.length]; 179 for (int i = 0; i < ar.length; i++) { 180 result[i] = ar[i]; 181 } 182 return result; 183 } else if (arrType == Character.TYPE) { 184 char[] ar = (char[]) array; 185 Character[] result = new Character[ar.length]; 186 for (int i = 0; i < ar.length; i++) { 187 result[i] = ar[i]; 188 } 189 return result; 190 } else if (arrType == Byte.TYPE) { 191 byte[] ar = (byte[]) array; 192 Byte[] result = new Byte[ar.length]; 193 for (int i = 0; i < ar.length; i++) { 194 result[i] = ar[i]; 195 } 196 return result; 197 } else if (arrType == Short.TYPE) { 198 short[] ar = (short[]) array; 199 Short[] result = new Short[ar.length]; 200 for (int i = 0; i < ar.length; i++) { 201 result[i] = ar[i]; 202 } 203 return result; 204 } else { 205 return null; 206 } 207 } else { 208 return (Object[]) array; 209 } 210 } 211 212 public static List<?> toList(Object array) { 213 Class<?> arrType = array.getClass().getComponentType(); 214 if (arrType.isPrimitive()) { 215 if (arrType == Integer.TYPE) { 216 int[] ar = (int[]) array; 217 List<Integer> result = new ArrayList<Integer>(ar.length); 218 for (int v : ar) { 219 result.add(v); 220 } 221 return result; 222 } else if (arrType == Long.TYPE) { 223 long[] ar = (long[]) array; 224 List<Long> result = new ArrayList<Long>(ar.length); 225 for (long v : ar) { 226 result.add(v); 227 } 228 return result; 229 } else if (arrType == Double.TYPE) { 230 double[] ar = (double[]) array; 231 List<Double> result = new ArrayList<Double>(ar.length); 232 for (double v : ar) { 233 result.add(v); 234 } 235 return result; 236 } else if (arrType == Float.TYPE) { 237 float[] ar = (float[]) array; 238 List<Float> result = new ArrayList<Float>(ar.length); 239 for (float v : ar) { 240 result.add(v); 241 } 242 return result; 243 } else if (arrType == Character.TYPE) { 244 char[] ar = (char[]) array; 245 List<Character> result = new ArrayList<Character>(ar.length); 246 for (char v : ar) { 247 result.add(v); 248 } 249 return result; 250 } else if (arrType == Byte.TYPE) { 251 byte[] ar = (byte[]) array; 252 List<Byte> result = new ArrayList<Byte>(ar.length); 253 for (byte v : ar) { 254 result.add(v); 255 } 256 return result; 257 } else if (arrType == Short.TYPE) { 258 short[] ar = (short[]) array; 259 List<Short> result = new ArrayList<Short>(ar.length); 260 for (short v : ar) { 261 result.add(v); 262 } 263 return result; 264 } else { 265 return null; 266 } 267 } else { 268 return Arrays.asList((Object[]) array); 269 } 270 } 271 272}