001 /** 002 * ========================================= 003 * LibFormula : a free Java formula library 004 * ========================================= 005 * 006 * Project Info: http://reporting.pentaho.org/libformula/ 007 * 008 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors. 009 * 010 * This library is free software; you can redistribute it and/or modify it under the terms 011 * of the GNU Lesser General Public License as published by the Free Software Foundation; 012 * either version 2.1 of the License, or (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; 015 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 016 * See the GNU Lesser General Public License for more details. 017 * 018 * You should have received a copy of the GNU Lesser General Public License along with this 019 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, 020 * Boston, MA 02111-1307, USA. 021 * 022 * [Java is a trademark or registered trademark of Sun Microsystems, Inc. 023 * in the United States and other countries.] 024 * 025 * 026 * ------------ 027 * $Id: ArrayConverter.java 3521 2007-10-16 10:55:14Z tmorgner $ 028 * ------------ 029 * (C) Copyright 2006-2007, by Pentaho Corporation. 030 */ 031 package org.jfree.formula.util; 032 033 import java.util.ArrayList; 034 import java.util.List; 035 import java.lang.reflect.Array; 036 037 /** 038 * Creation-Date: 08.10.2006, 17:37:50 039 * 040 * @author Thomas Morgner 041 */ 042 public class ArrayConverter 043 { 044 private ArrayConverter() 045 { 046 } 047 048 public static Object[] getAsList(final Object maybeArray, 049 final Class arrayType) 050 { 051 if (maybeArray == null) 052 { 053 return null; 054 } 055 056 if (maybeArray.getClass().isArray() == false) 057 { 058 return new Object[]{maybeArray}; 059 } 060 061 final ArrayList list = new ArrayList(); 062 ArrayConverter.addToList(list, maybeArray); 063 final Object o = Array.newInstance(arrayType, list.size()); 064 return list.toArray((Object[]) o); 065 } 066 067 private static void addToList (final List list, final Object array) 068 { 069 final int length = Array.getLength(array); 070 for (int i = 0; i < length; i++) 071 { 072 final Object value = Array.get(array, i); 073 if (value == null) 074 { 075 list.add(null); 076 continue; 077 } 078 079 if (value.getClass().isArray() == false) 080 { 081 list.add(value); 082 continue; 083 } 084 085 ArrayConverter.addToList(list, value); 086 } 087 } 088 089 090 /** 091 * @param maybeArray 092 * @param dimensions 093 * @return 094 */ 095 public static Object[] getArray(final Object maybeArray, 096 final Class arrayType, 097 final int dims) 098 { 099 if (maybeArray == null) 100 { 101 return null; 102 } 103 if (dims <= 0) 104 { 105 return null; 106 } 107 108 if (maybeArray.getClass().isArray() == false) 109 { 110 Object object = maybeArray; 111 for (int i = 0; i < dims; i++) 112 { 113 final Object[] array = (Object[]) Array.newInstance(arrayType, 1); 114 array[0] = object; 115 object = array; 116 } 117 return (Object[]) object; 118 } 119 120 if (ArrayConverter.getDimensionCount(maybeArray.getClass()) < dims) 121 { 122 return null; 123 } 124 return (Object[]) maybeArray; 125 } 126 127 public static int getDimensionCount(Class arrayClass) 128 { 129 int count = 0; 130 while (arrayClass != null && arrayClass.isArray()) 131 { 132 count += 1; 133 arrayClass = arrayClass.getComponentType(); 134 } 135 return count; 136 } 137 138 }