Source for org.jfree.formula.util.ArrayConverter

   1: /**
   2:  * =========================================
   3:  * LibFormula : a free Java formula library
   4:  * =========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/libformula/
   7:  *
   8:  * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  *
  26:  * ------------
  27:  * $Id: ArrayConverter.java 3521 2007-10-16 10:55:14Z tmorgner $
  28:  * ------------
  29:  * (C) Copyright 2006-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.formula.util;
  32: 
  33: import java.util.ArrayList;
  34: import java.util.List;
  35: import java.lang.reflect.Array;
  36: 
  37: /**
  38:  * Creation-Date: 08.10.2006, 17:37:50
  39:  *
  40:  * @author Thomas Morgner
  41:  */
  42: public class ArrayConverter
  43: {
  44:   private ArrayConverter()
  45:   {
  46:   }
  47: 
  48:   public static Object[] getAsList(final Object maybeArray,
  49:                                    final Class arrayType)
  50:   {
  51:     if (maybeArray == null)
  52:     {
  53:       return null;
  54:     }
  55: 
  56:     if (maybeArray.getClass().isArray() == false)
  57:     {
  58:       return new Object[]{maybeArray};
  59:     }
  60: 
  61:     final ArrayList list = new ArrayList();
  62:     ArrayConverter.addToList(list, maybeArray);
  63:     final Object o = Array.newInstance(arrayType, list.size());
  64:     return list.toArray((Object[]) o);
  65:   }
  66: 
  67:   private static void addToList (final List list, final Object array)
  68:   {
  69:     final int length = Array.getLength(array);
  70:     for (int i = 0; i < length; i++)
  71:     {
  72:       final Object value = Array.get(array, i);
  73:       if (value == null)
  74:       {
  75:         list.add(null);
  76:         continue;
  77:       }
  78: 
  79:       if (value.getClass().isArray() == false)
  80:       {
  81:         list.add(value);
  82:         continue;
  83:       }
  84: 
  85:       ArrayConverter.addToList(list, value);
  86:     }
  87:   }
  88: 
  89: 
  90:   /**
  91:    * @param maybeArray
  92:    * @param dimensions
  93:    * @return
  94:    */
  95:   public static Object[] getArray(final Object maybeArray,
  96:                                   final Class arrayType,
  97:                                   final int dims)
  98:   {
  99:     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: }