View Javadoc

1   /*
2    *  soapUI, copyright (C) 2006 eviware.com 
3    *
4    *  soapUI is free software; you can redistribute it and/or modify it under the 
5    *  terms of the GNU Lesser General Public License as published by the Free Software Foundation; 
6    *  either version 2.1 of the License, or (at your option) any later version.
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  public class ModelItemNames<T extends ModelItem>
26  {
27     private List<T> elements;
28  
29     public ModelItemNames(List<T> elements)
30     {
31     	this.elements = new ArrayList<T>( elements );
32     }
33     
34     public ModelItemNames(T[] elements)
35     {
36        // Create an ArrayList to make sure that elements is modifyable.
37        this.elements = new ArrayList<T>( Arrays.asList(elements) );
38     }
39     
40     public String[] getNames()
41     {
42        ArrayList<String> list = getElementNameList();
43        return list.toArray( new String[ list.size() ] );
44     }
45     
46     private ArrayList<String> getElementNameList()
47     {
48        ArrayList<String> elementNames = new ArrayList<String>();
49        for(T element : elements)
50        {
51           elementNames.add(element.getName());
52        }
53        return elementNames;
54     }
55     
56     public T getElement( String name )
57     {
58        int index = getElementNameList().indexOf(name);
59        return elements.get(index);
60     }
61  
62     public void addElement(T element)
63     {
64        elements.add(element);
65     }
66  
67  	public int getSize()
68  	{
69  		return elements.size();
70  	}
71  
72  	public String getNameAt( int i )
73  	{
74  		return elements.get( i ).getName();
75  	}
76  }