-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

All,

Nathan Bubna wrote:
> On 4/10/07, Henning P. Schmiedehausen <[EMAIL PROTECTED]> wrote:
>> Actually, the primitive types are the problem. For Object arrays,
>> using asArray
>> is a piece of cake. However, for simple types, you can not cast to
>> Object [], so
>> Arrays.asArray is out of the question, but I tried this abomination:
>>
>>         if (Object [].class.isAssignableFrom(klass))
>>         {
>>             return Arrays.asList((Object []) obj);
>>         } else if (boolean [].class.isAssignableFrom(klass))
>>         {
>>             return Arrays.asList(ArrayUtils.toObject((boolean []) obj));
>>         } else [... all primitives to short ...]
>>         }

It might be easier to use java.lang.reflect.Array to access elements in
these arrays. I wrote a primitive array wrapper for an expression
evaluator that I wrote some years ago.

I'd be happy to attach this code to a Jira isssue in order to bless it
as something being given away freely (but I reserve the right to publish
it separately, since I will be releasing the evaluator sometime soon).

import java.lang.reflect.Array;
import java.util.AbstractCollection;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

/**
 * A class that wraps a primitive array with a Collection interface.
 *
 * @author Chris Schultz
 * @version $Revision: 1.2 $ $Date: 2006-04-14 19:40:41 $
 */
public class PrimitiveArrayWrapper
    extends AbstractCollection
{
    private Object _array;

    public PrimitiveArrayWrapper(Object array)
    {
        _array = array;
    }

    public Iterator iterator()
    {
        return new ArrayIterator(_array);
    }

    public int size()
    {
        return Array.getLength(_array);
    }

    private static class ArrayIterator
        implements Iterator
    {
        private Object _array;
        private int _current;
        private int _size;
        public ArrayIterator(Object array)
        {
            _array = array;
            _size = Array.getLength(_array);
            _current = 0;
        }

        public boolean hasNext()
        {
            return _current < _size;
        }

        public Object next()
        {
            if(!this.hasNext())
                throw new java.util.NoSuchElementException();

            return Array.get(_array, _current++);
        }

        public void remove()
        {
            throw new UnsupportedOperationException();
        }
    }
}

> if you can get it to work, i'm definitely open to other
> implementations.  my only concern is to enable this functionality (and
> more if possible).

If you need get() and set() capability (a la java.util.List), those
could easily be worked-in to the above class.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.7 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iD8DBQFGHl4A9CaO5/Lv0PARAsloAKCsC5ktQ35nlKQlG9CCnqgzouHdAACghVCo
K//I6AKwU2kNPk+ntALfbc4=
=lw+Q
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to