/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.apache.qpid.jms.test.testpeer;

import java.util.Arrays;

import org.apache.qpid.proton.amqp.DescribedType;

public abstract class ListDescribedType implements DescribedType
{
    private final Object[] _fields;

    public ListDescribedType(int numberOfFields)
    {
        _fields = new Object[numberOfFields];
    }

    @Override
    public Object getDescribed()
    {
        //Return a List containing only the 'used fields' (i.e up to the highest field used)
        int numUsedFields = 0;
        for(int i = 0; i < _fields.length; i++)
        {
            if(_fields[i] != null)
            {
                numUsedFields = i + 1;
            }
        }

        Object[] usedFields = new Object[numUsedFields];
        for(int j = 0; j < numUsedFields; j++)
        {
            usedFields[j] = _fields[j];
        }

        return Arrays.asList(usedFields);
    }

    protected Object[] getFields()
    {
        return _fields;
    }

    @Override
    public String toString()
    {
        return "ListDescribedType [descriptor=" + getDescriptor() + " fields=" + Arrays.toString(_fields) + "]";
    }
}
@@ -869,16 +869,68 @@
 .Arrays;
+%0Aimport java.util.LinkedList;%0Aimport java.util.List;
 %0A%0Aimport
@@ -1546,55 +1546,119 @@
     
-Object%5B%5D usedFields = new Object%5BnumUsedFields%5D
+// Create a list with the fields in the correct positions.%0A        List%3CObject%3E list = new LinkedList%3CObject%3E()
 ;%0A  
@@ -1728,24 +1728,17 @@
     
-usedFields%5Bj%5D = 
+list.add(
 _fie
@@ -1743,16 +1743,17 @@
 ields%5Bj%5D
+)
 ;%0A      
@@ -1776,33 +1776,12 @@
 urn 
-Arrays.asList(usedFields)
+list
 ;%0A  

remove intermediate array and build the list directly