KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > ast > lib > ListBaseImpl


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Philippe Merle.
23 Contributor(s): Christophe Demarey.
24
25 ====================================================================*/

26
27 package org.objectweb.openccm.ast.lib;
28
29 /**
30  * ListBaseImpl is a wrapper base class for all lists of objects.
31  *
32  * This class encapsulates a list containing elements
33  * and provides some methods to access to the list.
34  *
35  * The goal of this class is to allow us in the future to change
36  * the used data structure (currently java.util.List) to another
37  * data structure, without rewriting all subclasses.
38  *
39  *
40  * Inherits from:
41  *
42  * - ObjectBase: The base class for all objects.
43  *
44  * - List: The base interface for all lists.
45  *
46  *
47  * @author <a HREF="mailto:Philippe.Merle@lifl.fr">Philippe Merle</a>
48  *
49  * @version 0.1
50  */

51
52 public class ListBaseImpl
53        extends ObjectBase
54        implements org.objectweb.openccm.ast.api.List
55 {
56     // ==================================================================
57
//
58
// Internal state.
59
//
60
// ==================================================================
61

62     /** The list containing elements. */
63     private java.util.List JavaDoc elements_;
64
65     // ==================================================================
66
//
67
// Constructor.
68
//
69
// ==================================================================
70

71     /**
72      * The default constructor.
73      */

74     public
75     ListBaseImpl()
76     {
77         // Call the ObjectBase constructor.
78
super();
79
80         // Init internal state, i.e. create the list.
81
elements_ = new java.util.ArrayList JavaDoc();
82     }
83
84     // ==================================================================
85
//
86
// Internal methods.
87
//
88
// ==================================================================
89

90     // ==================================================================
91
//
92
// Public methods.
93
//
94
// ==================================================================
95

96     /**
97      * Obtain an element of the list.
98      *
99      * @param index The index of the element.
100      */

101     public Object JavaDoc
102     get(int index)
103     {
104         return elements_.get(index);
105     };
106
107     /**
108      * Add an element if it is not null.
109      *
110      * @param element The element to add.
111      */

112     public void
113     addObject(Object JavaDoc element)
114     {
115         if (element != null)
116         {
117           elements_.add(element);
118         }
119     };
120
121     /**
122      * Obtain an array containing all the added elements.
123      *
124      * @param emptyArray An empty array providing the .
125      *
126      * @return An array containing all the added elements.
127      */

128     public Object JavaDoc[]
129     toArray(Object JavaDoc[] emptyArray)
130     {
131        return elements_.toArray(emptyArray);
132     };
133
134     /**
135      * Obtain a list containing all the added elements.
136      *
137      * @return A list containing all the added elements.
138      */

139     public java.util.List JavaDoc
140     toList()
141     {
142        return elements_;
143     };
144
145     /**
146      * Obtain a new iterator to elements of the list.
147      *
148      * @return The new iterator.
149      */

150     public java.util.Iterator JavaDoc
151     iterator()
152     {
153         return elements_.iterator();
154     }
155
156     /*
157      * Check if element is already in the list.
158      *
159      * @param obj - The element to find.
160      *
161      * @return True if element is present, else false.
162      */

163     public boolean
164     contains(Object JavaDoc obj)
165     {
166         for(int i=0; i<getSize(); i++)
167         {
168             Object JavaDoc obj_i = get(i);
169             if( obj_i == obj )
170                 return true;
171         }
172         return false;
173     }
174
175     /*
176      * Check if a same element of the list is present several times
177      *
178      * @return : -1 if all elements are distinct
179      * the index of element present several times.
180      */

181     public int
182     checkSameItem()
183     {
184         for(int i=0; i<getSize(); i++)
185         {
186             Object JavaDoc obj_i = get(i);
187
188             for(int j=i+1; j<getSize(); j++)
189             {
190                 Object JavaDoc obj_j = get(j);
191
192                 if( obj_i == obj_j )
193                     return i;
194             }
195         }
196         return -1;
197     }
198
199     // ==================================================================
200
//
201
// Methods for OMG IDL org.objectweb.openccm.ast.api.List
202
//
203
// ==================================================================
204

205     /**
206      * Obtain the size of the list.
207      *
208      * @return The size of the list.
209      */

210     public int
211     getSize()
212     {
213         return elements_.size();
214     };
215 }
216
Popular Tags