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