|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
AbstractComponentAdapter.java | 100% | 100% | 100% | 100% |
|
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; | |
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øy | |
26 | * @author Jon Tirsén | |
27 | * @version $Revision: 1570 $ | |
28 | * @since 1.0 | |
29 | */ | |
30 | public abstract class AbstractComponentAdapter implements ComponentAdapter, Serializable { | |
31 | private Object componentKey; | |
32 | private Class 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 | 1510 | protected AbstractComponentAdapter(Object componentKey, Class componentImplementation) throws AssignabilityRegistrationException { |
41 | 1510 | if (componentImplementation == null) { |
42 | 2 | throw new NullPointerException("componentImplementation"); |
43 | } | |
44 | 1508 | this.componentKey = componentKey; |
45 | 1508 | this.componentImplementation = componentImplementation; |
46 | 1508 | checkTypeCompatibility(); |
47 | } | |
48 | ||
49 | /** | |
50 | * {@inheritDoc} | |
51 | * @see org.picocontainer.ComponentAdapter#getComponentKey() | |
52 | */ | |
53 | 7908 | public Object getComponentKey() { |
54 | 7908 | if (componentKey == null) { |
55 | 2 | throw new NullPointerException("componentKey"); |
56 | } | |
57 | 7906 | return componentKey; |
58 | } | |
59 | ||
60 | /** | |
61 | * {@inheritDoc} | |
62 | * @see org.picocontainer.ComponentAdapter#getComponentImplementation() | |
63 | */ | |
64 | 11552 | public Class getComponentImplementation() { |
65 | 11552 | return componentImplementation; |
66 | } | |
67 | ||
68 | 1508 | protected void checkTypeCompatibility() throws AssignabilityRegistrationException { |
69 | 1508 | if (componentKey instanceof Class) { |
70 | 1126 | Class componentType = (Class) componentKey; |
71 | 1126 | if (!componentType.isAssignableFrom(componentImplementation)) { |
72 | 14 | 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 | 2 | public String toString() { |
82 | 2 | return getClass().getName() + "[" + getComponentKey() + "]"; |
83 | } | |
84 | ||
85 | 194 | public void accept(PicoVisitor visitor) { |
86 | 194 | visitor.visitComponentAdapter(this); |
87 | } | |
88 | } |
|