KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.objectweb.kilim.description;
19
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.ArrayList JavaDoc;
23
24 import org.objectweb.kilim.KilimException;
25
26 /**
27  * @author horn
28  * Describes a slot, which is just a set of unbound ports
29  */

30 public class Slot extends TemplateElementImpl implements NamedElement {
31     
32     private static String JavaDoc[][] msgTexts = {
33         { "attempt to create a slot with a null name in template ", "" }, //0
34
{ "attempt to create a slot ", " in a null template" }, //1
35
{ "attempt to set a null name to a slot in template ", "" }, //2
36
{ "attempt to use an undefined status ", " for a slot " }, //3
37
{ "attempt to set a null name to a slot in template ", "" }, //4
38
{ "attempt ot add a null port in slot ", "" }, //5
39
{ "attempt to add an already added port ", " in slot " }, //6
40
{ "attempt to remove a null port from a slot ", "" }, //7
41
{ "attempt to remove a port from an empty slot ", "" }, //8
42
{ "attempt to add a null binding frin slot ", "" }, //9
43
{ "attempt to remove a null binding from a slot ", "" }, //10
44
{ "attempt to add an already added binding ", " in slot " }, //11
45
{ "attempt to remove a binding from an empty slot ", "" } //12
46
};
47         
48     private String JavaDoc slotName;
49     private int status;
50     private List JavaDoc ports;
51     
52     /**
53      * a public constructor for slots.
54      * @param aSlotName : the slot name
55      * @param aStatus : the status of the slot. It should be one KILIM.PRIVATE, KILIM.PROTECTED, KILIM.PUBLIC.
56      * @param aTemplate : the template in which the slot is defined.
57      * @throws KilimException : generated if aSlotName or aTemplate is null or if aStatus has an invalid value.
58      */

59     public Slot(String JavaDoc aSlotName, int aStatus, TemplateDescription aTemplate) throws KilimException {
60         if (aSlotName == null) {
61             throw new KilimException(msgTexts[0][0] + aTemplate);
62         }
63         if (aTemplate == null) {
64             throw new KilimException(msgTexts[1][0] + aSlotName + msgTexts[1][1]);
65         }
66         checkStatus(aStatus);
67         slotName = aSlotName;
68         status = aStatus;
69         setContainingTemplate(aTemplate);
70     }
71         
72     /**
73      * @see org.objectweb.kilim.description.NamedElement#getLocalName()
74      */

75     public String JavaDoc getLocalName() {
76         return slotName;
77     }
78
79     /**
80      * @see org.objectweb.kilim.description.NamedElement#setLocalName(String)
81      */

82     public void setLocalName(String JavaDoc aName) throws KilimException {
83         if (aName == null) {
84             throw new KilimException(msgTexts[2][0] + getContainingTemplate());
85         }
86
87         slotName = aName;
88     }
89     
90     /**
91      * @see org.objectweb.kilim.description.NamedElement#getStatus()
92      */

93     public int getStatus() {
94         return status;
95     }
96
97     /**
98      * @see org.objectweb.kilim.description.NamedElement#setStatus(int)
99      */

100     public void setStatus(int aStatus) throws KilimException {
101         checkStatus(aStatus);
102         status = aStatus;
103     }
104         
105     /**
106      * Method checkStatus.
107      * @param aStatus
108      * @throws KilimException
109      */

110     private void checkStatus(int aStatus) throws KilimException {
111         if (aStatus < 1 || aStatus > 3) {
112             throw new KilimException(msgTexts[3][0] + aStatus + msgTexts[3][1] + msgSuffix1());
113         }
114     }
115
116     /**
117      * Method getPorts returns as an iterator the ports defined in the slot.
118      * @return Iterator
119      */

120     public Iterator JavaDoc getPorts() {
121         if (ports == null) {
122             return KILIM.EMPTY_ITERATOR;
123         }
124         return ports.listIterator();
125     }
126     
127     /**
128      * Method addPort adds a new port in the slot.
129      * @param aPort : the port to be added.
130      * @throws KilimException : generated if aPort is null or if the slot already contains aPort.
131      */

132     public void addPort(Port aPort) throws KilimException {
133         if (aPort == null) {
134             throw new KilimException(msgTexts[4][0] + msgSuffix1());
135         }
136         if (ports == null) {
137             ports = new ArrayList JavaDoc();
138         }
139         
140         if (ports.contains(aPort)) {
141             throw new KilimException(msgTexts[6][0] + aPort.getLocalName() + msgTexts[6][1] + msgSuffix1());
142         }
143         ports.add(aPort);
144     }
145     
146     /**
147      * Method removePort.
148      * @param aPort : the port to be removed.
149      * @throws KilimException : generated if aPort is null or if no port have been defined in the slot.
150      */

151     public void removePort(Port aPort) throws KilimException {
152         if (aPort == null) {
153             throw new KilimException(msgTexts[7][0] + msgSuffix1());
154         }
155         
156         if (ports == null) {
157             throw new KilimException(msgTexts[8][0] + msgSuffix1());
158         }
159         ports.remove(aPort);
160     }
161         
162     private String JavaDoc msgSuffix1() {
163         return slotName + " in template " + getContainingTemplate().getName();
164     }
165 }
Popular Tags