View Javadoc

    1   /*
    2    *  soapUI, copyright (C) 2004-2007 eviware.com
    3    *
    4    *  soapUI is free software; you can redistribute it and/or modify it under the
    5    *  terms of version 2.1 of the GNU Lesser General Public License as published by
    6    *  the Free Software Foundation.
    7    *
    8    *  soapUI is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
    9    *  even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    10   *  See the GNU Lesser General Public License for more details at gnu.org.
    11   */
    12  package com.eviware.soapui.model.util;
    13
    14  import java.util.ArrayList;
    15  import java.util.Arrays;
    16  import java.util.List;
    17
    18  import com.eviware.soapui.model.ModelItem;
    19
    20  /***
    21   * Utility for handling model item names.
    22   *
    23   * @author Lars Høidahl
    24   */
    25
    26  public class ModelItemNames<T extends ModelItem>
    27  {
    28     private List<T> elements;
    29
    30     public ModelItemNames(List<T> elements)
    31     {
    32     	this.elements = new ArrayList<T>( elements );
    33     }
    34
    35     public ModelItemNames(T[] elements)
    36     {
    37        // Create an ArrayList to make sure that elements is modifyable.
    38        this.elements = new ArrayList<T>( Arrays.asList(elements) );
    39     }
    40
    41     public String[] getNames()
    42     {
    43        ArrayList<String> list = getElementNameList();
    44        return list.toArray( new String[ list.size() ] );
    45     }
    46
    47     private ArrayList<String> getElementNameList()
    48     {
    49        ArrayList<String> elementNames = new ArrayList<String>();
    50        for(T element : elements)
    51        {
    52           elementNames.add(element.getName());
    53        }
    54        return elementNames;
    55     }
    56
    57     public T getElement( String name )
    58     {
    59        int index = getElementNameList().indexOf(name);
    60        return elements.get(index);
    61     }
    62
    63     public void addElement(T element)
    64     {
    65        elements.add(element);
    66     }
    67
    68  	public int getSize()
    69  	{
    70  		return elements.size();
    71  	}
    72
    73  	public String getNameAt( int i )
    74  	{
    75  		return elements.get( i ).getName();
    76  	}
    77  }