KickJava   Java API By Example, From Geeks To Geeks.

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


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  * This class describes the binding between to bindable elements. Initially limited to describe bindings between ports, it has been extended to allow
29  * the binding of a port to any kind of value sources.
30  */

31 public class Binding extends TemplateElementImpl {
32     private String JavaDoc portName;
33     private List JavaDoc boundProviders;
34     
35     /**
36      * The public constructor of a binding object.
37      * @param aPort : reference of the port to be bound.
38      * @param aTemplate : reference of the containing template.
39      * @throws KilimException : generated when the port reference is null or when the template reference is null.
40      */

41     public Binding(String JavaDoc aPort, TemplateDescription aTemplate) throws KilimException {
42         if (aPort == null) {
43             if (aTemplate != null) {
44                 throw new KilimException("attempt to use a null name for a port in a bind in template " + aTemplate.getName());
45             } else {
46                 throw new KilimException("attempt to use a null name for a port in a bind in a null template " + aTemplate.getName());
47             }
48         }
49         if (aTemplate == null) {
50             throw new KilimException("attempt to bind port " + aPort + " in a null template ");
51         }
52         portName = aPort;
53         setContainingTemplate(aTemplate);
54     }
55         
56     /**
57      * returns the name of the bound port.
58      * @return String
59      */

60     public String JavaDoc getPortName() {
61         return portName;
62     }
63
64     /**
65      * sets the name of the bound port
66      * @param aPortName : the port name
67      */

68     public void setPortName(String JavaDoc aPortName) {
69         portName = aPortName;
70     }
71     
72     /**
73      * returns as an iterator the bound target providers. The iterator contains at most one element if the port is unary.
74      * It may contain several providers if the port is nary.
75      * @return Iterator : it returns KILIM.EMPTY_ITERATOR if the port is not bound to any source.
76      */

77     public Iterator JavaDoc getBoundProviders() {
78         if (boundProviders == null) {
79             return KILIM.EMPTY_ITERATOR;
80         }
81         return boundProviders.listIterator();
82     }
83     
84     /**
85      * adds the target bound port.
86      * @param aElement : the value source the port should be bound to.
87      * @throws KilimException : generated when the element is null.
88      */

89     public void bindProvider(BasicElement aElement) throws KilimException {
90         if (aElement == null) {
91             throw new KilimException("illegal null element in binding port " + portName);
92         }
93         if (boundProviders == null) {
94             boundProviders = new ArrayList JavaDoc();
95         }
96         boundProviders.add(aElement);
97     }
98
99     /**
100      * removes a previously bound source.
101      * @param aElement : the element to unbind.
102      * @throws KilimException : generated when the reference of the element is null or when the binding is empty.
103      */

104     public void unbindProvider(BasicElement aElement) throws KilimException {
105         if (aElement == null) {
106             throw new KilimException("illegal null element in unbinding port " + portName + " in template " + getContainingTemplate().getName());
107         }
108         
109         if (boundProviders == null) {
110             throw new KilimException("attempt to remove an element from an empty binding " + portName + " in template " + getContainingTemplate().getName());
111         }
112         boundProviders.remove(aElement);
113     }
114 }
115
Popular Tags