Array Converter : Array Convert « Collections Data Structure « Java






Array Converter

    
/**
 * 
 * LibFormula : a free Java formula library
 * 
 *
 * Project Info:  http://reporting.pentaho.org/libformula/
 *
 * (C) Copyright 2006-2007, by Pentaho Corporation and Contributors.
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation;
 * either version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
 * in the United States and other countries.]
 *
 *
 * ------------
 * $Id: ArrayConverter.java 3521 2007-10-16 10:55:14Z tmorgner $
 * ------------
 * (C) Copyright 2006-2007, by Pentaho Corporation.
 */

import java.util.ArrayList;
import java.util.List;
import java.lang.reflect.Array;

/**
 * Creation-Date: 08.10.2006, 17:37:50
 *
 * @author Thomas Morgner
 */
public class ArrayConverter
{
  private ArrayConverter()
  {
  }

  public static Object[] getAsList(final Object maybeArray,
                                   final Class arrayType)
  {
    if (maybeArray == null)
    {
      return null;
    }

    if (maybeArray.getClass().isArray() == false)
    {
      return new Object[]{maybeArray};
    }

    final ArrayList list = new ArrayList();
    ArrayConverter.addToList(list, maybeArray);
    final Object o = Array.newInstance(arrayType, list.size());
    return list.toArray((Object[]) o);
  }

  private static void addToList (final List list, final Object array)
  {
    final int length = Array.getLength(array);
    for (int i = 0; i < length; i++)
    {
      final Object value = Array.get(array, i);
      if (value == null)
      {
        list.add(null);
        continue;
      }

      if (value.getClass().isArray() == false)
      {
        list.add(value);
        continue;
      }

      ArrayConverter.addToList(list, value);
    }
  }


  /**
   * @param maybeArray
   * @param dimensions
   * @return
   */
  public static Object[] getArray(final Object maybeArray,
                                  final Class arrayType,
                                  final int dims)
  {
    if (maybeArray == null)
    {
      return null;
    }
    if (dims <= 0)
    {
      return null;
    }

    if (maybeArray.getClass().isArray() == false)
    {
      Object object = maybeArray;
      for (int i = 0; i < dims; i++)
      {
        final Object[] array = (Object[]) Array.newInstance(arrayType, 1);
        array[0] = object;
        object = array;
      }
      return (Object[]) object;
    }

    if (ArrayConverter.getDimensionCount(maybeArray.getClass()) < dims)
    {
      return null;
    }
    return (Object[]) maybeArray;
  }

  public static int getDimensionCount(Class arrayClass)
  {
    int count = 0;
    while (arrayClass != null && arrayClass.isArray())
    {
      count += 1;
      arrayClass = arrayClass.getComponentType();
    }
    return count;
  }

}

   
    
    
    
  








Related examples in the same category

1.Convert array to string
2.Convert byte array to Integer and Long
3.Return a new byte array containing a sub-portion of the source array
4.byte array to int array
5.int array to byte array
6.Turn an array of ints into a printable string.
7.Convert array to string (from c3p0)
8.Convert byte array to string
9.Converts a Array to an Enumeration and allows it to be serializedConverts a Array to an Enumeration and allows it to be serialized
10.Array helper
11.Return a String representation of the given two-dimensional object array
12.Return a string representation of the given native two-dimensional long array.
13.Concatenates the given int[] array into one String, inserting a delimiter between each pair of elements.
14.Concatenates the given long[] array into one String, inserting a delimiter between each pair of elements.
15.Converts an array of Strings to a comma-sperated-list.