KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > description > ArraySource


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18
19 package org.objectweb.kilim.description;
20
21 import java.util.Iterator JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import org.objectweb.kilim.KilimException;
25
26 /**
27  * @author horn
28  * Describes an Array of source elements (i.e. elements which provide a value).
29  */

30 public class ArraySource extends InlinedElement {
31     private ArrayList JavaDoc arrayElements;
32     private String JavaDoc typeName;
33     
34     /**
35      * The public constructor of an array source.
36      * @param aTypeName : a type is defined here through its name (via a simple String and not via a Type).
37      * No semantical check is performed making it possible to use arbitrary types (java types, for example).
38      * A simple syntactical check is done however : name must be of the form : IDENT([])*
39      * examples of correct type names are : int[ ], toto[ ][ ].
40      * @param aTemplate : the template containing the array definition.
41      * @throws KilimException : generated in case of bad syntactical form.
42      */

43     public ArraySource(String JavaDoc aTypeName, TemplateDescription aTemplate) throws KilimException {
44         super(true, false, aTemplate);
45         checkTypeName(aTypeName);
46         typeName = aTypeName;
47     }
48     
49     /**
50      * performs a simple syntactical check of the type name:
51      * The name must be of the form : IDENT([])*. examples of correct type names are : int[], toto[][].
52      */

53     private void checkTypeName(String JavaDoc aTypeName) throws KilimException {
54         String JavaDoc name = aTypeName;
55         
56         while (name.endsWith("[]")) {
57             name = name.substring(0, name.length() - 2);
58         }
59         if (name.indexOf("[") != -1 || name.indexOf("]") != -1) {
60             throw new KilimException("syntax error in type declaration " + aTypeName + " of an array in template " + getContainingTemplate().getName());
61         }
62     }
63     
64     /**
65      * @see org.objectweb.kilim.description.BasicElement#getKind()
66      */

67     public int getKind() {
68         return KILIM.ARRAY;
69     }
70
71     /**
72      * returns the current size of the array.
73      * @return int.
74      */

75     public int getCurrentSize() {
76         if (arrayElements == null) {
77             return 0;
78         }
79         return arrayElements.size();
80     }
81     
82     /**
83      * extends an array by adding a new element in the array.
84      * @param value : must be a basic template element
85      * @throws KilimException : generated when the argument is null or when the argument does not provide
86      * a value.
87      */

88     public void addElement(BasicElement value) throws KilimException {
89         if (value == null) {
90             throw new KilimException("attempt to add a null value in an array in template " + getContainingTemplate().getName());
91         }
92         
93         if (!value.providesValue()) {
94             throw new KilimException("attempt to add a non provider to an array in template " + getContainingTemplate().getName());
95         }
96         if (arrayElements == null) {
97             arrayElements = new ArrayList JavaDoc();
98         }
99         arrayElements.add(value);
100     }
101     
102     /**
103      * returns the element stored at a given position.
104      * @param aIndex : index in the array
105      * @return BasicElement : the element at the indicated position.
106      * @throws KilimException : generated for illegal values of index (i.e. negative values, or values exceding the size of the array).
107      */

108     public BasicElement getElement(int aIndex) throws KilimException {
109         if (aIndex < 0) {
110             throw new KilimException("attempt to use an illegal index : " + aIndex + " in getting Element of an array in template " + getContainingTemplate().getName());
111         }
112         return (BasicElement) arrayElements.get(aIndex);
113     }
114     
115     /**
116      * returns the elements of the array as an iterator.
117      * @return Iterator
118      */

119     public Iterator JavaDoc getElements() {
120         if (arrayElements == null) {
121             return KILIM.EMPTY_ITERATOR;
122         }
123         return arrayElements.listIterator();
124     }
125     
126     /**
127      * @see org.objectweb.kilim.description.Array#getType()
128      */

129     public String JavaDoc getTypeName() {
130         return typeName;
131     }
132 }
Popular Tags