KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > picocontainer > defaults > AbstractComponentAdapter


1 /*****************************************************************************
2  * Copyright (C) PicoContainer Organization. All rights reserved. *
3  * ------------------------------------------------------------------------- *
4  * The software in this package is published under the terms of the BSD *
5  * style license a copy of which has been included with this distribution in *
6  * the LICENSE.txt file. *
7  * *
8  * Original code by *
9  *****************************************************************************/

10 package org.picocontainer.defaults;
11
12 import org.picocontainer.ComponentAdapter;
13 import org.picocontainer.PicoVisitor;
14
15 import java.io.Serializable JavaDoc;
16
17 /**
18  * Base class for a ComponentAdapter with general functionality.
19  * This implementation provides basic checks for a healthy implementation of a ComponentAdapter.
20  * It does not allow to use <code>null</code> for the component key or the implementation,
21  * ensures that the implementation is a concrete class and that the key is assignable from the
22  * implementation if the key represents a type.
23  *
24  * @author Paul Hammant
25  * @author Aslak Helles&oslash;y
26  * @author Jon Tirs&eacute;n
27  * @version $Revision: 1570 $
28  * @since 1.0
29  */

30 public abstract class AbstractComponentAdapter implements ComponentAdapter, Serializable JavaDoc {
31     private Object JavaDoc componentKey;
32     private Class JavaDoc componentImplementation;
33
34     /**
35      * Constructs a new ComponentAdapter for the given key and implementation.
36      * @param componentKey the search key for this implementation
37      * @param componentImplementation the concrete implementation
38      * @throws AssignabilityRegistrationException if the key is a type and the implementation cannot be assigned to.
39      */

40     protected AbstractComponentAdapter(Object JavaDoc componentKey, Class JavaDoc componentImplementation) throws AssignabilityRegistrationException {
41         if (componentImplementation == null) {
42             throw new NullPointerException JavaDoc("componentImplementation");
43         }
44         this.componentKey = componentKey;
45         this.componentImplementation = componentImplementation;
46         checkTypeCompatibility();
47     }
48
49     /**
50      * {@inheritDoc}
51      * @see org.picocontainer.ComponentAdapter#getComponentKey()
52      */

53     public Object JavaDoc getComponentKey() {
54         if (componentKey == null) {
55             throw new NullPointerException JavaDoc("componentKey");
56         }
57         return componentKey;
58     }
59
60     /**
61      * {@inheritDoc}
62      * @see org.picocontainer.ComponentAdapter#getComponentImplementation()
63      */

64     public Class JavaDoc getComponentImplementation() {
65         return componentImplementation;
66     }
67
68     protected void checkTypeCompatibility() throws AssignabilityRegistrationException {
69         if (componentKey instanceof Class JavaDoc) {
70             Class JavaDoc componentType = (Class JavaDoc) componentKey;
71             if (!componentType.isAssignableFrom(componentImplementation)) {
72                 throw new AssignabilityRegistrationException(componentType, componentImplementation);
73             }
74         }
75     }
76
77     /**
78      * @return Returns the ComponentAdapter's class name and the component's key.
79      * @see java.lang.Object#toString()
80      */

81     public String JavaDoc toString() {
82         return getClass().getName() + "[" + getComponentKey() + "]";
83     }
84
85     public void accept(PicoVisitor visitor) {
86         visitor.visitComponentAdapter(this);
87     }
88 }
89
Popular Tags